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
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个数,四种操作,
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.
题解:直接主席树在线更新查询即可
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1e5*10;
struct node{
int l,r;
ll val,laz;
}tree[N*40+10];
int root[N];
int n,q,cnt;
int build(int l,int r)
{
int cur=cnt++;
tree[cur].laz=0;
tree[cur].val=0;
if(l==r)
{
scanf("%lld",&tree[cur].val);
return cur;
}
int mid=(r+l)>>1;
tree[cur].l=build(l,mid);
tree[cur].r=build(mid+1,r);
tree[cur].val=tree[tree[cur].l].val+tree[tree[cur].r].val;
//cout<<tree[cur].val<<endl;
return cur;
}
int update(int rt,int pl,int pr,int val,int l,int r)
{
int cur=cnt++;
tree[cur]=tree[rt];
tree[cur].val+=(ll)val*(min(pr,r)-max(pl,l)+1);
if(pl<=l&&r<=pr)
{
tree[cur].laz+=val;
return cur;
}
int mid=(r+l)>>1;
if(pl<=mid) tree[cur].l=update(tree[rt].l,pl,pr,val,l,mid);
if(pr>mid) tree[cur].r=update(tree[rt].r,pl,pr,val,mid+1,r);
return cur;
}
ll query(int cur,int pl,int pr,int l,int r)
{
if(pl<=l&&r<=pr)
{
return tree[cur].val;
}
int mid=(r+l)>>1;
ll res=tree[cur].laz*(min(pr,r)-max(l,pl)+1);
if(pl<=mid) res+=query(tree[cur].l,pl,pr,l,mid);
if(pr>mid) res+=query(tree[cur].r,pl,pr,mid+1,r);
return res;
}
int main()
{
int T=0;
while(~scanf("%d%d",&n,&q))
{
int now=0;
cnt=0;
root[now]=build(1,n);
char op[5];
int l,r,val;
while(q--)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d%d%d",&l,&r,&val);
now++;
root[now]=update(root[now-1],l,r,val,1,n);
}
else if(op[0]=='Q')
{
scanf("%d%d",&l,&r);
printf("%lld\n",query(root[now],l,r,1,n));
}
else if(op[0]=='H')
{
scanf("%d%d%d",&l,&r,&val);
printf("%lld\n",query(root[val],l,r,1,n));
}
else
{
scanf("%d",&val);
now=val;
}
}
}
return 0;
}