标签:
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; const int N = 5e6+10, M = 1e3+10, mod = 1000000, inf = 1e9+1000; typedef long long ll; int n,m; int l[N],r[N],root[N],add[N],tot = 0; ll sum[N]; int build(int s,int t) { int now = ++tot; add[now] = 0; if(s==t) { scanf("%lld",&sum[now]); l[now] = r[now] = 0; return now; } int mid = (s+t)>>1; l[now] = build(s,mid); r[now] = build(mid+1,t); sum[now] = sum[l[now]]+sum[r[now]]; return now; } //查询在以t为根内[ll,rr]区间的值 ll query(int k,int lll,int rr,int s,int t) { ll ans = (add[k]*(rr-lll+1)); if(lll==s&&rr==t) return sum[k]; int mid = (s+t)>>1; if(rr<=mid) ans+=query(l[k],lll,rr,s,mid); else if(lll>mid) ans+=query(r[k],lll,rr,mid+1,t); else { ans+=query(l[k],lll,mid,s,mid); ans+=query(r[k],mid+1,rr,mid+1,t); } return ans; } int update(int k,int lll,int rr,int d,int s,int t) { int now = ++tot; l[now] = l[k]; r[now] = r[k]; add[now] = add[k]; sum[now] = sum[k]; sum[now]+=(ll) (d*(rr-lll+1)); if(lll==s&&rr==t) { add[now]+=d; return now; } int mid = (s+t)>>1; if(rr<=mid) l[now] = update(l[k],lll,rr,d,s,mid); else if(lll>mid) r[now] = update(r[k],lll,rr,d,mid+1,t); else { l[now] = update(l[k],lll,mid,d,s,mid); r[now] = update(r[k],mid+1,rr,d,mid+1,t); } return now; } void solve() { tot = 0; root[0] = build(1,n); int now = 0; for(int i=1;i<=m;i++) { char ch[3]; scanf("%s",ch); if(ch[0]==‘Q‘) { int a,b; scanf("%d%d",&a,&b); printf("%lld\n",query(root[now],a,b,1,n)); } else if(ch[0]==‘C‘) { int a,b,d; scanf("%d%d%d",&a,&b,&d); root[now+1] = update(root[now],a,b,d,1,n); now++; } else if(ch[0]==‘H‘) { int a,b,t; scanf("%d%d%d",&a,&b,&t); printf("%lld\n",query(root[t],a,b,1,n)); } else scanf("%d",&now); } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { solve(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zxhl/p/5469263.html