hdu 4348 To the moon 主席树区间更新
To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
Background
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we'll give you a chance, to implement the logic behind the scene.
You‘ve been given N integers A[1], A[2],..., A[N]. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we'll give you a chance, to implement the logic behind the scene.
You‘ve been given N integers A[1], A[2],..., A[N]. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.
Input
n m
A1 A2 ... An
... (here following the m operations. )
A1 A2 ... An
... (here following the m operations. )
Output
... (for each query, simply print the result. )
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
2 4
0 0
C 1 1 1
C 2 2 -1
Q 1 2
H 1 2 1
Sample Output
4
55
9
15
0
1
Author
HIT
Source
给你一个数组,让你维护,m次操作
询问当前时刻一个区间的和
询问在t时刻的一个区间的和
回到t时刻
时间+1,在此时刻+1下更新一个区间的值
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<endl; const int N=5e5+30010,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; ll a[N]; struct Chairmantree { int rt[N*20],ls[N*20],rs[N*20]; ll sum[N*20],lazy[N*20]; int tot; void init() { tot=0; } void pushup(int l,int r,int pos) { sum[pos]=sum[ls[pos]]+sum[rs[pos]]+1LL*(r-l+1)*lazy[pos]; } void build(int l,int r,int &pos) { pos=++tot; lazy[pos]=0; sum[pos]=0; if(l==r) { sum[pos]=a[l]; return; } int mid=(l+r)>>1; build(l,mid,ls[pos]); build(mid+1,r,rs[pos]); pushup(l,r,pos); } void update(int L,int R,ll c,int pre,int l,int r,int &pos) { pos=++tot; ls[pos]=ls[pre]; rs[pos]=rs[pre]; sum[pos] = sum[pre]; lazy[pos]=lazy[pre]; if(L==l&&r==R) { sum[pos]+=1LL*(r-l+1)*c; lazy[pos]+=c; return; } int mid=(l+r)>>1; if(R<=mid) update(L,R,c,ls[pre],l,mid,ls[pos]); else if(L>mid) update(L,R,c,rs[pre],mid+1,r,rs[pos]); else { update(L,mid,c,ls[pre],l,mid,ls[pos]); update(mid+1,R,c,rs[pre],mid+1,r,rs[pos]); } pushup(l,r,pos); } ll query(int L,int R,int l,int r,int pos) { if(L==l&&r==R) return sum[pos]; int mid=(l+r)>>1; ll ans=1LL*lazy[pos]*(R-L+1); if(R<=mid) ans+=query(L,R,l,mid,ls[pos]); else if(L>mid) ans+=query(L,R,mid+1,r,rs[pos]); else { ans+=query(L,mid,l,mid,ls[pos]); ans+=query(mid+1,R,mid+1,r,rs[pos]); } return ans; } }; Chairmantree tree; char s[10]; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=1; i<=n; i++) scanf("%lld",&a[i]); tree.init(); tree.build(1,n,tree.rt[0]); int now=0; while(m--) { scanf("%s",s); if(s[0]=='C') { int l,r; ll c; scanf("%d%d%lld",&l,&r,&c); tree.update(l,r,c,tree.rt[now],1,n,tree.rt[now+1]); now++; } else if(s[0]=='Q') { int l,r; scanf("%d%d",&l,&r); printf("%lld\n",tree.query(l,r,1,n,tree.rt[now])); } else if(s[0]=='H') { int l,r,t; scanf("%d%d%d",&l,&r,&t); printf("%lld\n",tree.query(l,r,1,n,tree.rt[t])); } else { int x; scanf("%d",&x); now=x; } } } return 0; }