题意:题目给你n个数,m个操作,接下来一行给你这n个数,接下的几行给出m个操作,Q a b 表示查询区间[a,b]里的数和和。U a b c 表示把区间[a,b]里的数都加上c。
同样使用延迟标记add。当add不等于0的时候,表示它以下所有区间都需要更新。因为我们没有去改变原始查询区间的区间端点,所以当找到一个区间小于等于查询区间的时候,标记它的延迟标记,返回。注意,因为有可能多次对同一个区间更新值,如果我们直接把标记add=增加值,就不能反应这种情况,所以是add+=增加值,还有,延迟标记也会超int。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define LL(x) (x<<1) #define RR(x) (x<<1|1) #define MID(a,b) (a+((b-a)>>1)) const int N=100005; typedef long long LL; struct node { int lft,rht; LL sum,add; int mid(){return MID(lft,rht);} void fun(LL tmp) { add+=tmp; sum+=(rht-lft+1)*tmp; } }; int y[N]; struct Segtree { node tree[N*4]; void relax(int ind) { if(tree[ind].add) { tree[LL(ind)].fun(tree[ind].add); tree[RR(ind)].fun(tree[ind].add); tree[ind].add=0; } } void build(int lft,int rht,int ind) { tree[ind].lft=lft; tree[ind].rht=rht; tree[ind].sum=0; tree[ind].add=0; if(lft==rht) tree[ind].sum=y[lft]; else { int mid=tree[ind].mid(); build(lft,mid,LL(ind)); build(mid+1,rht,RR(ind)); tree[ind].sum=tree[LL(ind)].sum+tree[RR(ind)].sum; } } void updata(int st,int ed,int ind,int add) { int lft=tree[ind].lft,rht=tree[ind].rht; if(st<=lft&&rht<=ed) tree[ind].fun(add); else { relax(ind); int mid=tree[ind].mid(); if(st<=mid) updata(st,ed,LL(ind),add); if(ed> mid) updata(st,ed,RR(ind),add); tree[ind].sum=tree[LL(ind)].sum+tree[RR(ind)].sum; } } LL query(int st,int ed,int ind) { int lft=tree[ind].lft,rht=tree[ind].rht; if(st<=lft&&rht<=ed) return tree[ind].sum; else { relax(ind); int mid=tree[ind].mid(); LL sum1=0,sum2=0; if(st<=mid) sum1=query(st,ed,LL(ind)); if(ed> mid) sum2=query(st,ed,RR(ind)); return sum1+sum2; } } }seg; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { char str[5]; int a,b,c; for(int i=1;i<=n;i++) scanf("%d",&y[i]); seg.build(1,n,1); while(m--) { scanf("%s",str); if(str[0]=='Q') { scanf("%d%d",&a,&b); printf("%lld\n",seg.query(a,b,1)); } else { scanf("%d%d%d",&a,&b,&c); seg.updata(a,b,1,c); } } } return 0; }
/*代码风格更新前*/ #include <iostream> #include <cstdio> using namespace std; const int N=100005; int y[N]; struct node { int left,right; long long sum,co; int mid(){return left+(right-left)/2;} int dis(){return right-left+1;} void change(long long a) { co+=a;sum+=(dis()*a); } }; struct Segtree { node tree[N*4]; void build(int left,int right,int r) { tree[r].left=left; tree[r].right=right; tree[r].sum=tree[r].co=0; if(left==right) { tree[r].sum=y[left]; } else { int mid=tree[r].mid(); build(left,mid,r*2); build(mid+1,right,r*2+1); tree[r].sum=tree[r*2].sum+tree[r*2+1].sum; } } void updata(int be,int end,int r,int co) { if(be<=tree[r].left&&tree[r].right<=end) { tree[r].change(co); } else { if(tree[r].co!=0) { tree[r*2].change(tree[r].co); tree[r*2+1].change(tree[r].co); tree[r].co=0; } int mid=tree[r].mid(); if(be<=mid) updata(be,end,r*2,co); if(end>mid) updata(be,end,r*2+1,co); tree[r].sum=tree[r*2].sum+tree[r*2+1].sum; } } long long query(int be,int end,int r) { if(be<=tree[r].left&&tree[r].right<=end) { return tree[r].sum; } else { if(tree[r].co!=0) { tree[r*2].change(tree[r].co); tree[r*2+1].change(tree[r].co); tree[r].co=0; } int mid=tree[r].mid(); long long sum1=0,sum2=0; if(be<=mid) sum1=query(be,end,r*2); if(end>mid) sum2=query(be,end,r*2+1); return sum1+sum2; } } }seg; int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&y[i]); seg.build(1,n,1); for(int i=0;i<m;i++) { char cmd[5]; int a,b,c; scanf("%s",cmd); if(cmd[0]=='Q') { scanf("%d%d",&a,&b); printf("%lld\n",seg.query(a,b,1)); } else { scanf("%d%d%d",&a,&b,&c); seg.updata(a,b,1,c); } } return 0; }