http://acm.hdu.edu.cn/showproblem.php?pid=4348
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的数组,总共四种操作,其中还有一个时间T,每次更新区间的时候时间T+1,更新操作就是把lr区间全部加上d查询操作是一个查询当前时间lr的和,另一个是在第t秒的时候,查询lr区间的和,最后一种操作是把时间退回到第t秒,并且不能再询问t秒以后的时间。刚开始的时候T等于0.
思路:关键点是lazy标记不下传,区间修改直接新开一棵线段树。
详情参见:https://blog.csdn.net/kirito16/article/details/47266801
#include<bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
struct node
{
int ls,rs;//左子树编号 右子树编号
ll sum,tag;//区间和 懒标记
}tree[maxn*30];
int n,m,tot;//n个数 m个询问 tot代表节点个数
int rt[maxn*30];//主席树各根节点编号
char op[15];//指令
int build(int l,int r)//建树
{
int root=++tot;
tree[root].tag=tree[root].tag=0;
if(l==r)
{
scanf("%lld",&tree[root].sum);
return root;
}
else
{
int mid=(l+r)>>1;
tree[root].ls=build(l,mid);
tree[root].rs=build(mid+1,r);
}
tree[root].sum=tree[tree[root].ls].sum+tree[tree[root].rs].sum;
return root;
}
ll query(int root,int x,int y,int l,int r,ll add)//查询 因为没有push_down操作 所以要用add记录 懒标记 的和
{
if(l==x&&r==y)
return tree[root].sum+add*(r-l+1);
int mid=(l+r)>>1;
if(y<=mid)
return query(tree[root].ls,x,y,l,mid,add+tree[root].tag);
else if(x>mid)
return query(tree[root].rs,x,y,mid+1,r,add+tree[root].tag);
else
return query(tree[root].ls,x,mid,l,mid,add+tree[root].tag)+query(tree[root].rs,mid+1,y,mid+1,r,add+tree[root].tag);
}
void update(int &root,int x,int y,int l,int r,ll v)//新建一棵树
{
tree[++tot]=tree[root];
root=tot;
tree[root].sum+=(y-x+1)*v;
if(x==l&&y==r)
{
tree[root].tag+=v;
return ;
}
int mid=(l+r)>>1;
if(x>mid)
update(tree[root].rs,x,y,mid+1,r,v);
else if(y<=mid)
update(tree[root].ls,x,y,l,mid,v);
else
{
update(tree[root].rs,mid+1,y,mid+1,r,v);
update(tree[root].ls,x,mid,l,mid,v);
}
}
int main()
{
int times=0;
while(~scanf("%d %d",&n,&m))
{
if(times)
putchar('\n');
++times;
tot=0;
rt[0]=build(1,n);
int len=0;
int x,y,z;
for(int i=0;i<m;i++)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d %d %d",&x,&y,&z);
rt[++len]=rt[len-1];
update(rt[len],x,y,1,n,z);
}
else if(op[0]=='Q')
{
scanf("%d %d",&x,&y);
printf("%lld\n",query(rt[len],x,y,1,n,0));
}
else if(op[0]=='H')
{
scanf("%d %d %d",&x,&y,&z);
printf("%lld\n",query(rt[z],x,y,1,n,0));
}
else
{
scanf("%d",&x);
tot=rt[x+1];
len=x;
}
}
}
return 0;
}