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.
Input
n m
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
题意:给出n,m;然后n个数,接着m个操作,C代表a,b,区间加上值k,并且这个操作时间t加一,时间从0开始,这个操作的时间为t+1,H表示求t时刻a,b区间的和,Q表示求区间a,b,的和,B表示将时间退回到t,并且t后面的操作全部消掉;
思路:一开始的时候初始化一颗线段树,就普通线段树一样,因为H要加上add,现在主要的问题是怎么进行区间更新,因为可持久化树的思想就是要利用前面的树,并且不能修改,只能引用,所以按普通的区间更新的话,每次更新就相当于要建一颗r-l+1长的线段树,空间肯定不够,考虑普通的线段树的down操作是把懒惰标记往下传,并更新子树,那么可不可以不更新子树了,当然是可以的,我们开一个数组记录add的值,直接把当前区间的懒惰标记用一个参数add传下去,并用数组add记录;然后找到要求和的区间时,直接把从上到下的懒惰标记累加和乘以区间长度再加上这段区间原本的和就可以了。然后这时候主席树里面每颗线段树的sum记录的就是这段区间跟新过得和,因为在新建的一颗主席树上更新了l-r区间,所以所有有覆盖l-r的区间的sum都是可以跟新的因为没有用到前面一颗树的sum,是另开的空间,所以保证了查询的时候只查到这一段就可以直接返回sum了。至于add当然就是等于前面一颗树的add加上C。另外当时间回到t的时候可以回收空间,就是t到T的这一段内存已经用不到了,这时可以直接把tot = T[t+1]-1;
代码:
#include<cstdio>
#include<algorithm>
#define maxn 100005
using namespace std;
__int64 sum[maxn*30],add[maxn*30];
int root[maxn*30];
int ls[maxn*30],rs[maxn*30];
int tot;
int A,B;
void build(int l,int r,int &st)
{
st=++tot;
int temp=(l+r)/2;
if(l==r)
{
scanf("%I64d",&sum[st]);
return;
}
build(l,temp,ls[st]);
build(temp+1,r,rs[st]);
sum[st]=sum[ls[st]]+sum[rs[st]];
}
__int64 query(int l,int r,int ss,__int64 add1)
{
if(A<=l&&r<=B)
{
return sum[ss]+(add1+add[ss])*(r-l+1);
}
__int64 ans=0;
int temp=(l+r)/2;
if(A<=temp)
ans=ans+query(l,temp,ls[ss],add1+add[ss]);
if(B>temp)
ans=ans+query(temp+1,r,rs[ss],add1+add[ss]);
return ans;
}
void update(int l,int r,__int64 k,int last,int &now)
{
now=++tot;
add[now]=add[last];
if(A<=l&&r<=B)
{
sum[now]=sum[last];
ls[now]=ls[last];
rs[now]=rs[last];
add[now]=add[last]+k;
return;
}
int temp=(l+r)/2;
if(A<=temp)
update(l,temp,k,ls[last],ls[now]);
else
ls[now]=ls[last];
if(B>temp)
update(temp+1,r,k,rs[last],rs[now]);
else
rs[now]=rs[last];
int len=r-l+1;
sum[now]=sum[ls[now]]+sum[rs[now]]+(len-len/2)*add[ls[now]]+len/2*add[rs[now]];
}
int main()
{
int n,m;
int T=0;
while(~scanf("%d%d",&n,&m))
{
if(T)
printf("%d\n");
T++;
tot=0;
int t=0;
build(1,n,root[0]);
char c[10];
for(int i=1;i<=m;i++)
{
scanf("%s",c);
if(c[0]=='Q')
{
scanf("%d%d",&A,&B);
printf("%I64d\n",query(1,n,root[t],0));
}
else if(c[0]=='C')
{
__int64 k;
scanf("%d%d%I64d",&A,&B,&k);
update(1,n,k,root[t],root[t+1]);
t++;
}
else if(c[0]=='H')
{
int tt;
scanf("%d%d%d",&A,&B,&tt);
printf("%I64d\n",query(1,n,root[tt],0));
}
else
{
int tt;
scanf("%d",&tt);
t=tt;
tot=root[t+1]-1;
}
}
}
}