Ryuji doesn't want to study-線段樹應用
阿新 • • 發佈:2018-12-10
#include<bits/stdc++.h> using namespace std; #define maxn 500000 #define ll long long struct node { int l,r,sum; } root[maxn],tree[maxn]; void creat(node *t,ll l,ll r,ll c) { t[c].l=l; t[c].r=r; if(l==r) { return ; t[c].sum=0; } t[c].sum=t[c*2].sum+t[c*2+1].sum; ll mid=(l+r)/2; creat(t,mid+1,r,c*2+1); creat(t,l,mid,c*2); } void updata(node *t,ll l,ll ad,ll c) { if(t[c].l==l&&t[c].r==l) { t[c].sum=ad; return ; } t[c].sum=t[c*2].sum+t[c*2+1].sum; ll mid=(t[c].l+t[c].r)/2; if(l<=mid) updata(t,l,ad,c*2); else updata(t,l,ad,c*2+1); } ll query(node *t,ll l,ll r,ll c) { if(l==t[c].l&&r==t[c].r) return t[c].sum; ll mid=(t[c].l+t[c].r)/2; if(r<=mid) return query(t,l,r,c*2); else if(l>mid) return query(t,l,r,c*2+1); else { return query(t,l,mid,c*2)+query(t,mid,r,c*2+1);; } } ll n,q,u,v,w; int main() { cin>>n>>q; creat(root,1,n,1); creat(tree,1,n,1); for(int i=1; i<=n; i++) { cin>>w; updata(root,i,w,1); updata(tree,i,w*(n-i+1),1); } while(q--) { cin>>u>>v>>w; if(u==2) { updata(root,v,w,1); updata(tree,v,w*(n-v+1),1); } else cout<<query(tree,v,w,1)-(n-w+1)*query(root,v,w,1)<<endl; } return 0; }