To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 6186 Accepted Submission(s): 1421
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.
A1 A2 ... An
... (here following the m operations. )
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1e5+50;
struct Tree
{
long long num;
int add,lson,rson;
}tree[maxn<<5];
int cnt,root[maxn<<5],t;
void init()
{
cnt=0;
t=0;
}
void pushup(int rt)
{
tree[rt].num=tree[tree[rt].lson].num+tree[tree[rt].rson].num;
}
int bulidtree(int l,int r)
{
int v=++cnt;
tree[v].add=0;
if(l==r)
{
scanf("%lld",&tree[v].num);
tree[v].lson=0,tree[v].rson=0;
return v;
}
int mid=(l+r)>>1;
tree[v].lson=bulidtree(l,mid);
tree[v].rson=bulidtree(mid+1,r);
pushup(v);
return v;
}
int update(int p,int l,int r,int L,int R,int d)
{
int v=++cnt;
tree[v]=tree[p];
tree[v].num+=(R-L+1)*d;
if(L<=l&&R>=r)
{
tree[v].add+=d;
return v;
}
int mid=(l+r)>>1;
if(R<=mid)
{
tree[v].lson=update(tree[p].lson,l,mid,L,R,d);
}
else if(L>mid)
{
tree[v].rson=update(tree[p].rson,mid+1,r,L,R,d);
}
else
{
tree[v].lson=update(tree[p].lson,l,mid,L,mid,d);
tree[v].rson=update(tree[p].rson,mid+1,r,mid+1,R,d);
}
return v;
}
long long query(int p,int l,int r,int L,int R)
{
long long ans=(R-L+1)*tree[p].add;
if(L<=l&&R>=r)
{
return tree[p].num;
}
int mid=(l+r)>>1;
if(R<=mid)
{
ans+=query(tree[p].lson,l,mid,L,R);
}
else if(L>mid)
{
ans+=query(tree[p].rson,mid+1,r,L,R);
}
else
{
ans+=query(tree[p].lson,l,mid,L,mid);
ans+=query(tree[p].rson,mid+1,r,mid+1,R);
}
return ans;
}
int main()
{
int n,m,flag=0;
while(scanf("%d%d",&n,&m)==2)
{
if(flag)
printf("\n");
flag=1;
init();
root[0]=bulidtree(1,n);
for(int i=0;i<m;i++)
{
char a[2];
scanf("%s",a);
if(a[0]=='C')
{
int l,r,d;
scanf("%d%d%d",&l,&r,&d);
root[++t]=update(root[t-1],1,n,l,r,d);
//printf("! %d\n",tree[root[t]].num);
}
else if(a[0]=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",query(root[t],1,n,l,r));
}
else if(a[0]=='H')
{
int l,r,nt;
scanf("%d%d%d",&l,&r,&nt);
printf("%lld\n",query(root[nt],1,n,l,r));
}
else
{
int a;
scanf("%d",&a);
t=a;
cnt=root[t+1]; //空間優化
}
}
}
}