To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 5687 Accepted Submission(s): 1293
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.
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. )
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
/*可以将标记留在节点上,查询的时候累加就行了,
这样避免了可持久化线段树打标记内存吃紧的窘态*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MX = 3e6 + 5;//开个30倍吧
LL sum[MX];
int ls[MX], rs[MX], o[MX], add[MX], tot;
void PushUP(int rt, int m) {
sum[rt] = sum[ls[rt]] + sum[rs[rt]] + (LL)m * add[rt];
}
void build(int l, int r, int &rt) {
rt = ++tot;
add[rt] = 0;
if (l == r) {
scanf("%lld", &sum[rt]);
return;
}
int m = (l + r) >> 1;
build(l, m, ls[rt]);
build(m + 1, r, rs[rt]);
PushUP(rt, r - l + 1);
}
void update(int L, int R, int c, int l, int r, int prt, int &rt) {
rt = ++tot;
sum[rt] = sum[prt];
add[rt] = add[prt];
ls[rt] = ls[prt];
rs[rt] = rs[prt];
if (L <= l && R >= r) {
sum[rt] += (LL)c * (r - l + 1);
add[rt] += c;
return;
}
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, l, m, ls[prt], ls[rt]);
if (R > m) update(L, R, c, m + 1, r, rs[prt], rs[rt]);
PushUP(rt, r - l + 1);
}
LL query(int L, int R, int l, int r, int ad, int &rt) {
if (L <= l && R >= r) {
return sum[rt] + (LL)ad * (r - l + 1);
}
ad += add[rt];
int m = (l + r) >> 1;
LL ret = 0;
if (L <= m) ret += query(L, R, l, m, ad, ls[rt]);
if (R > m) ret += query(L, R, m + 1, r, ad, rs[rt]);
return ret;
}
int main () {
int n, m; char op[2];
while (~scanf("%d%d", &n, &m)) {
int time = 0, l, r, d, t; tot = 0;
build(1, n, o[time]);
while (m--) {
scanf("%s", op);
if (op[0] == 'C') { //更新
scanf("%d%d%d", &l, &r, &d);
++time;
update(l, r, d, 1, n, o[time - 1], o[time]);
} else if (op[0] == 'Q') { //查询当前time
scanf("%d%d", &l, &r);
printf("%lld\n", query(l, r, 1, n, 0, o[time]));
} else if (op[0] == 'H') { //查询历史t
scanf("%d%d%d", &l, &r, &t);
printf("%lld\n", query(l, r, 1, n, 0, o[t]));
} else scanf("%d", &time); //回到历史
}
}
return 0 ;
}