-
C - A Simple Problem with Integers
- POJ - 3468
-
#include<iostream> #include<stdio.h> using namespace std; #define maxn 211111 #define ll long long struct node { ll l,r; long long sum,lazy; } tree[maxn*4]; void init(ll l,ll r,ll root) { tree[root].l=l; tree[root].r=r; tree[root].lazy=0; if(l==r) { tree[root].sum=0; return ; } ll mid=(l+r)/2; init(l,mid,2*root); init(mid+1,r,2*root+1); tree[root].sum=tree[root*2].sum+tree[root*2+1].sum; } void pushdown(ll root) { if(tree[root].lazy!=0) { tree[root*2].sum+=tree[root].lazy*(tree[2*root].r-tree[2*root].l+1); tree[root*2+1].sum+=tree[root].lazy*(tree[2*root+1].r-tree[2*root+1].l+1); tree[root*2].lazy+=tree[root].lazy; tree[root*2+1].lazy+=tree[root].lazy; tree[root].lazy=0; } } void updata(ll l,ll r,ll add,ll root) { tree[root].sum+=(r-l+1)*add; if(tree[root].l==l&&tree[root].r==r) { tree[root].lazy+=add; return ; } pushdown(root); ll mid=(tree[root].l+tree[root].r)/2; if(r<=mid) updata(l,r,add,root*2); else if(l>mid) updata(l,r,add,root*2+1); else { updata(l,mid,add,root*2); updata(mid+1,r,add,root*2+1); } } ll query(ll l,ll r,ll root) { if(tree[root].l==l&&tree[root].r==r) return tree[root].sum; pushdown(root); ll mid=(tree[root].l+tree[root].r)/2; if(r<=mid) return query(l,r,root*2); else if(l>mid) return query(l,r,root*2+1); else return query(l,mid,root*2)+query(mid+1,r,root*2+1); } int main() { ios::sync_with_stdio(false); ll q,x,y,z,i,a,n; char h; cin>>n>>q; init(1,n,1); for (i=1; i<=n; i++) { cin>>a; updata(i,i,a,1); } for (i=1; i<=q; i++) { cin>>h>>x>>y; if (h=='C') { cin>>z; updata(x,y,z,1); } else { cout<<query(x,y,1)<<endl; } } return 0; }
C - A Simple Problem with Integers-线段树lazy标记(模板)
最新推荐文章于 2021-08-09 18:54:27 发布