| ||||||||||
To the moonTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2516 Accepted Submission(s): 498
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 {A i | 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 {A i | l <= i <= r}. 3. H l r t: Querying a history sum of {A i | 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 ≤ 10 5, |A [i]| ≤ 10 9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10 4 .. 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
A 1 A 2 ... A n ... (here following the m operations. )
Output
... (for each query, simply print the result. )
Sample Input
Sample Output
Author
HIT
Source
|
思路:主席树保存过去的状态,注释sum和add的更新
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=200010;
const int maxm=maxn*50;
int N,M,num;
int a[maxn];
LL sum[maxm],add[maxm];
int T[maxm],lson[maxm],rson[maxm];
int build(int l,int r)
{
int root=num++;
sum[root]=add[root]=0;
if(l==r)
{
sum[root]=a[l];
return root;
}
int mid=(l+r)>>1;
lson[root]=build(l,mid);
rson[root]=build(mid+1,r);
sum[root]=sum[lson[root]]+sum[rson[root]];
return root;
}
void maintain(int newroot,int root)
{
lson[newroot]=lson[root];
rson[newroot]=rson[root];
add[newroot]=add[root];
sum[newroot]=sum[root];
}
int update(int root,int l,int r,int q1,int q2,int d)
{
int newroot=num++;
maintain(newroot,root);
sum[newroot]+=(min(q2,r)-max(q1,l)+1)*(LL)d;
if(q1<=l&&r<=q2)
{
add[newroot]+=d;
return newroot;
}
int mid=(l+r)>>1;
if(q1<=mid)lson[newroot]=update(lson[root],l,mid,q1,q2,d);
if(q2>mid)rson[newroot]=update(rson[root],mid+1,r,q1,q2,d);
return newroot;
}
LL query(int root,int l,int r,int q1,int q2)
{
LL ans=(min(q2,r)-max(q1,l)+1)*add[root];
if(q1<=l&&r<=q2)return sum[root];
int mid=(l+r)>>1;
if(q1<=mid)ans+=query(lson[root],l,mid,q1,q2);
if(q2>mid)ans+=query(rson[root],mid+1,r,q1,q2);
return ans;
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
for(int i=1;i<=N;i++)scanf("%d",&a[i]);
int cur=0;
num=0;
T[0]=build(1,N);
while(M--)
{
char op[5];
int l,r,d,t;
scanf("%s%d",op,&l);
if(op[0]=='C')
{
scanf("%d%d",&r,&d);
T[cur+1]=update(T[cur],1,N,l,r,d);
cur++;
}
else if(op[0]=='Q')
{
scanf("%d",&r);
printf("%I64d\n",query(T[cur],1,N,l,r));
}
else if(op[0]=='H')
{
scanf("%d%d",&r,&t);
printf("%I64d\n",query(T[t],1,N,l,r));
}
else cur=l;
}
}
return 0;
}