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
背景:
To The Moon是一款2011年11月发布的独立游戏,它是一款由RPG Maker提供支持的角色扮演冒险游戏。
到月球的前提是基于一种技术,使我们能够永久地重建对垂死的人的记忆。在这个问题上,我们会给你一个机会,来实现幕后的逻辑。
你已经得到了N个整数A [1],A [2],…,A [N]。在这些整数上,您需要执行以下操作:
1. C l r d:为每个{A i |增加一个常数d l <= i <= r},并将时间戳增加1,这是导致时间戳增加的唯一操作。
2.Q l r:查询{A i |的当前总和l <= i <= r}。
3. H l r t:查询{A i |的历史总和在时间t内,l <= i <= r}。
B t:回到时间t。而一旦你决定回到过去,你永远不可能获得前进版本了。
N,M≤10 5,| A [i] | ≤10^9,1≤l≤r≤N,| d | ≤104,系统从时间0开始,第一次修改在时间1,t≥0,不会引入你未来的状态。
输入:
n
A 1 A 2 … A n
…(这里是下面的操作)
输出:
…(对于每个查询,只需打印结果。)
题解:一道看起来特别和谐友好的可持续化线段树模板题目(๑•́ ₃ •̀๑),然后我卡了一个晚上的内存°(°ˊДˋ°) °,在线段树懒标记下传的时候,,,会有海量的新节点生成 (๑>m<๑) ,然后内存就“bong”,看过题解之后发现了某种神奇的黑科技。。(⁄•⁄ω⁄•⁄)可以将标记留在节点上一直不下传,查询的时候累加就行了,只需sum[x]=sum[lson]+sum[rson]+add[lson] * len1+add[rson] * len2。同时每次B操作后可将tot的值赋值为rt[t+1]因为t后面的状态已经没用啦(๑Ő௰Ő๑)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#define mid ((l+r)>>1)
#define lson(x) tree[x].lc
#define rson(x) tree[x].rc
using namespace std;
typedef long long ll;
const int N=100000+10;
inline void getint(int&num){
char c;bool flag=0;num=0;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=1;
while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
if(flag) num=-num;
}
inline void getint(ll&num){
char c;bool flag=0;num=0;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=1;
while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
if(flag) num=-num;
}
struct node{
int lc,rc;ll sum,add;
}tree[N*30];
int n,T,l,r,K,tot,tim,rt[N];char op[3];
void pushup(int x,int l,int r){
int len1=mid-l+1,len2=r-mid;
tree[x].sum=tree[lson(x)].sum+tree[rson(x)].sum+tree[lson(x)].add*len1+tree[rson(x)].add*len2;
}
void insert(int&x,int l,int r,int ql,int qr,ll val){
if(!x||l>qr||r<ql) return ;
tree[++tot]=tree[x],x=tot;
if(l>=ql&&r<=qr) tree[x].add+=val;
else{
insert(tree[x].lc,l,mid,ql,qr,val);
insert(tree[x].rc,mid+1,r,ql,qr,val);
pushup(x,l,r);
}
}
ll getsum(int x,int l,int r,int ql,int qr,ll now){
if(!x||l>qr||r<ql) return 0;
else if(l>=ql&&r<=qr)
return tree[x].sum+(now+tree[x].add)*(r-l+1);
else return getsum(tree[x].lc,l,mid,ql,qr,now+tree[x].add)
+getsum(tree[x].rc,mid+1,r,ql,qr,now+tree[x].add);
}
int build(int l,int r){
int x=++tot;
tree[x]=(node){0,0,0,0};
if(l==r) getint(tree[x].sum);
else{
tree[x].lc=build(l,mid);
tree[x].rc=build(mid+1,r);
pushup(x,l,r);
}
return x;
}
int main(){
getint(n),getint(T);
rt[1]=rt[0]=build(1,n);
while(T--){
scanf("%s",op);
if(op[0]=='C'){
tim++;
getint(l),getint(r),getint(K);
insert(rt[tim]=rt[tim-1],1,n,l,r,K);
rt[tim+1]=tot;
}
else if(op[0]=='Q'){
getint(l),getint(r);
cout<<getsum(rt[tim],1,n,l,r,0)<<'\n';
}
else if(op[0]=='H'){
getint(l),getint(r),getint(K);
cout<<getsum(rt[K],1,n,l,r,0)<<'\n';
}
else if(op[0]=='B'){
getint(K);
tim=K;
tot=rt[tim+1];
}
}
printf("%d\n",tot);
}
/*
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
*/