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:
- 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.
- Q l r: Querying the current sum of {Ai | l <= i <= r}.
- H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
- 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.
Input
n m
A1 A2 … An
… (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
分析
这道题的关键在于 update 操作
由于懒标记的存在,传统的线段树是有 pushup 和 pushdown 操作的。
但是如果对于可持久化线段树,每次更新都 pushdown,就要开多很多个节点,会使空间和时间大大增加。
于是我们用到标记永久化思想!
就是,我们每次将标记覆盖区间后,再也不下传了。
每次查询的时候,对查询的区间加上查询路径上标记的贡献就可以了。(因为查询路径上的标记对区间的和是有贡献的)
代码如下
#include <bits/stdc++.h>
#define LL long long
#define N 100005
#define lson l, m, lch[rt]
#define rson m + 1, r, rch[rt]
using namespace std;
int t, root[N], lch[N * 25], rch[N * 25], tag[N * 25], cnt;
LL sum[N * 25], z = 1;
char o[3];
void build(int l, int r, int &rt){
rt = ++cnt;
if(l == r){
scanf("%lld", &sum[rt]);
return;
}
int m = l + r >> 1;
build(lson);
build(rson);
sum[rt] = sum[lch[rt]] + sum[rch[rt]];
}
void update(int l, int r, int &rt, int a, int b, int c, int las){
rt = ++cnt;
sum[rt] = sum[las] + z * c * (min(r, b) - max(a, l) + 1);
tag[rt] = tag[las];
lch[rt] = lch[las];
rch[rt] = rch[las];
if(l >= a && r <= b){
tag[rt] += c;
return;
}
int m = l + r >> 1;
if(a <= m) update(lson, a, b, c, lch[las]);
if(b > m) update(rson, a, b, c, rch[las]);
}
LL query(int l, int r, int rt, int a, int b){
if(l >= a && r <= b) return sum[rt];
int m = l + r >> 1;
LL ans = z * tag[rt] * (min(r, b) - max(a, l) + 1);
if(a <= m) ans += query(lson, a, b);
if(b > m) ans += query(rson, a, b);
return ans;
}
int main(){
int i, j, n, m, a, b, c;
while(scanf("%d%d", &n, &m) != EOF){
t = cnt = 0;
memset(sum, 0, sizeof(sum));
memset(root, 0, sizeof(root));
memset(lch, 0, sizeof(lch));
memset(rch, 0, sizeof(rch));
memset(tag, 0, sizeof(tag));
build(1, n, root[0]);
for(i = 1; i <= m; i++){
scanf("%s%d", o, &a);
if(o[0] == 'B') t = a;
else if(o[0] == 'C'){
scanf("%d%d", &b, &c);
t++;
update(1, n, root[t], a, b, c, root[t - 1]);
}
else if(o[0] == 'H'){
scanf("%d%d", &b, &c);
printf("%lld\n", query(1, n, root[c], a, b));
}
else{
scanf("%d", &b);
printf("%lld\n", query(1, n, root[t], a, b));
}
}
}
return 0;
}