poj3468线段树标记永久化

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=100000+10,maxn=400000+10,inf=0x3f3f3f3f;

ll value[N<<2],lazy[N<<2];
void build(int l,int r,int rt)
{
    lazy[rt]=0;
    if(l==r)
    {
        scanf("%lld",&value[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(ls);
    build(rs);
    value[rt]=value[rt<<1]+value[rt<<1|1];
}
void update(int L,int R,ll c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        lazy[rt]+=c;
        //value[rt]+=c*(r-l+1);
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,ls);
    if(m<R)update(L,R,c,rs);
    value[rt]=value[rt<<1]+value[rt<<1|1]+lazy[rt<<1]*(m-l+1)+lazy[rt<<1|1]*(r-m);
}
ll query(int L,int R,ll add,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return (add+lazy[rt])*(r-l+1)+value[rt];
    int m=(l+r)>>1;
    ll ans=0;
    if(L<=m)ans+=query(L,R,add+lazy[rt],ls);
    if(m<R)ans+=query(L,R,add+lazy[rt],rs);
    return ans;
}
char s[10];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        while(m--)
        {
            scanf("%s",s);
            if(s[0]=='Q')
            {
                int l,r;
                scanf("%d%d",&l,&r);
                printf("%lld\n",query(l,r,0,1,n,1));
            }
            else
            {
                int l,r;ll c;
                scanf("%d%d%lld",&l,&r,&c);
                update(l,r,c,1,n,1);
                //printf("%d\n",query(1,n,0,1,n,1));
            }
        }
    }
    return 0;
}
/********************
5
10000
1 1 1 1 1
w 1 3 2
w 2 4 3
w 1 5 1
********************/
标记永久化

 

posted @ 2018-01-25 16:49  walfy  阅读(208)  评论(0编辑  收藏  举报