1. 程式人生 > >A Simple Problem with Integers(區間更新)

A Simple Problem with Integers(區間更新)

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+10;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll sum[N<<2];
ll add[N<<2];// 記錄節點每個數值該增加多少
struct Node
{
    int l,r;
    int mid(){
    return (l+r)>>1;
    }
}tree[N<<2];
void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int m)
{
    if(add[rt])
    {
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        sum[rt<<1]+=add[rt]*(m-(m>>1));
        sum[rt<<1|1]+=add[rt]*(m>>1);
        add[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    add[rt]=0;
    if(l==r)
    {
        scanf("%I64d",&sum[rt]);
        return;
    }
    int m=tree[rt].mid();
    build(lson);
    build(rson);
    PushUp(rt);
}
void updata(int c,int l,int r,int rt)
{
    if(tree[rt].l==l&&tree[rt].r==r)
    {
        add[rt]+=c;
        sum[rt]+=(ll)c*(r-l+1);
        return;
    }
    if(tree[rt].l==tree[rt].r) return;
    PushDown(rt,tree[rt].r-tree[rt].l+1);
    int m=tree[rt].mid();
    if(r<=m) updata(c,l,r,rt<<1);
    else if(l>m) updata(c,l,r,rt<<1|1);
    else {
        updata(c,l,m,rt<<1);
        updata(c,m+1,r,rt<<1|1);
    }
    PushUp(rt);
}
ll query(int l,int r,int rt)
{
    if(l==tree[rt].l&&r==tree[rt].r) return sum[rt];
    PushDown(rt,tree[rt].r-tree[rt].l+1);
    int m=tree[rt].mid();
    ll res=0;
    if(r<=m) res+=query(l,r,rt<<1);
    else if(l>m) res+=query(l,r,rt<<1|1);
    else
    {
        res+=query(l,m,rt<<1);
        res+=query(m+1,r,rt<<1|1);
    }
    return res;
}
int main()
{
    int n,m;;
    while(~scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        while(m--)
        {
            char ch[2];
            scanf("%s",ch);
            int a,b,c;
            if(ch[0]=='Q')
            {
                scanf("%d%d",&a,&b);
                printf("%I64d\n",query(a,b,1));
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                updata(c,a,b,1);
            }
        }
    }
}
其實不太懂,就當先撂這個模板吧

相關推薦

A Simple Problem with Integers區間更新

#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long l

POJ3468 A Simple Problem with Integers區間更新

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 106587 Accepted: 33254 Case Time Limit: 2

POJ 3468 A Simple Problem with Integers (Splay 區間更新區間求和)

題目連結 題目大意 線段樹區間更新的模板題,用Splay寫一遍作為用Splay處理區間問題的模板題。 分析 程式碼 #include <iostream> #include <cstdio> #include <c

A Simple Problem with Integers 區間更新和查詢

You have N integers, A1, A2, ... ,AN. You need to deal with two kinds of operations. One type of operation is to add some given number t

bzoj3212: Pku3468 A Simple Problem with Integers線段樹

while printf scan font geo main post align sim 3212: Pku3468 A Simple Problem with Integers 題目:傳送門 題解:    感謝Rose_max大佬的傾情相

POJ 3468 A Simple Problem with Integers線段樹

truct fin clas define class 基本 open urn display 題目鏈接:A Simple Problem with Integers 題意:N個數字,M次操作;操作分為兩種,第一種將$[L,R]$區間內的每個數都加上C,第二種為求$[L,

A Simple Problem with Integerspoj 3468 (lazy標記)

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 130621 Accepted: 40554 Case

POJ 3468 A Simple Problem with Integers線段樹 單點更新+區間求和

names || log shu 更新 can pro struct sim 題目鏈接:http://poj.org/problem?id=3468 題意:單點更新,區間求和。 題解:裸題 1 //POJ 3468 A Simple Problem with

POJ 3468 A Simple Problem with Integers線段樹模板之區間增減更新 區間求和查詢

return string ali accept numbers other map nts contain A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 13107

A Simple Problem with Integers線段樹區間更新

【題意】 給定長度為 NNN 的序列 AAA,有 QQQ次操作,操作分兩種 CabcC \ a \ b \ cCabc 表示將序列中 A[a,b]A[a,b]A[a,b]所有元素都加上一個數字 ccc QabQ \ a \ bQab 表示查詢序列 A[a,b]

A Simple Problem with Integers 線段樹,區間求和+區間更新

A Simple Problem with Integers 連結:傳送門 You have NN integers, A1,A2,⋯,ANA1,A2,⋯,AN. You need to deal with two kinds of operations. O

【Poj】-A Simple Problem with Integers線段樹,區間更新

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 100877 Accepted: 31450 Case Time Limit: 2

POJ 3468 A Simple Problem with Integers線段樹區間更新區間查詢

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 Accepted: 28818 Case Time Limit: 20

poj 3468 A Simple Problem with Integers原來是一道簡單的線段樹區間修改用來練練splay

long 兩個 可能 style push ios stream 區間 pan 題目鏈接:http://poj.org/problem?id=3468 題解:splay功能比線段樹強大當然代價就是有些操作比線段樹慢,這題用splay實現的比線段樹慢上一倍。線段樹用l

BZOJ3212 Pku3468 A Simple Problem with Integers線段樹區間求和、區間加模板

題目大意: 你有N個整數,A1,A2,.,An。你需要處理兩種操作。一種操作是在給定的區間內向每個數字加上一個給定的數字。另一種是求給定區間內的數字之和。   題解: 線段樹的基本操作。 lazy標記。 附上程式碼: #include<cstdio> #includ

poj 3468 A Simple Problem with Integers區間修改+區間查詢樹狀陣列

參考下面部落格的公式: 需要注意的是,輸入初始數列的時候要 add 兩次, L = R, add (L, x), add (R+1, -x) #include <iostream>

A Simple Problem with Integers 線段樹應用型別三區間增加 區間查詢】【模板基礎題】

題目連結:http://poj.org/problem?id=3468 參考部落格:https://blog.csdn.net/u013480600/article/details/22202711 Description You have N integers,

poj3511--A Simple Problem with Integers線段樹求和

poj pac style som can com onos roman miss A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K

POJ-3468 A Simple Problem with Integers線段樹、段變化+段查詢、模板

sum .org miss numbers ... bsp wid scanf accepted A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Su

poj3468 A Simple Problem with Integers 樹狀數組做法

align style cee arch puppet ret 定義 pre tab 題目傳送門 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K