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 {Ai | 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 {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | 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 ≤ 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.
A1 A2 ... An
... (here following the m operations. )
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
55
9
15
0
1
/*
唐代高蟾
《金陵晚望》 曾伴浮云归晚翠,犹陪落日泛秋声。
世间无限丹青手,一片伤心画不成。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
typedef long long ll;
int lson[MAXN], rson[MAXN];
int data[MAXN], add[MAXN];
ll sum[MAXN];
int tot; int insert(int t, int ll, int rr, int d, int l, int r){
int now = ++tot;
lson[now] = lson[t];
rson[now] = rson[t];
add[now] = add[t];
sum[now] = sum[t];
sum[now] += (long long)(d * (rr - ll + ));
if(ll == l && rr == r){
add[now] += d;
return now;
}
int mid = (l + r)>>;
if(rr <= mid) lson[now] = insert(lson[t], ll, rr, d, l, mid);
else if(ll > mid) rson[now] = insert(rson[t], ll, rr, d, mid + , r);
else{
lson[now] = insert(lson[t], ll, mid, d, l, mid);
rson[now] = insert(rson[t], mid + , rr, d, mid + , r);
}
return now;
}
//在t的根内查询[ll, rr]区间的值
long long query(int t, int ll, int rr, int l, int r){
long long Ans = (long long)(add[t] * (rr - ll + ));
if (ll == l && rr == r) return sum[t];
int mid = (l + r)>>;
if (rr <= mid) Ans += query(lson[t], ll, rr, l, mid);
else if (ll > mid) Ans += query(rson[t], ll, rr, mid + , r);
else {
Ans += query(lson[t], ll, mid, l, mid);
Ans += query(rson[t], mid + , rr, mid + , r);
}
return Ans;
}
int build(int ll, int rr){
int now = ++tot;
add[now] = ;
if (ll == rr){
scanf("%lld", &sum[now]);
lson[now] = rson[now] = ;
return now;
}
int mid = (ll + rr)>>;
lson[now] = build(ll, mid);
rson[now] = build(mid + , rr);
sum[now] = sum[lson[now]] + sum[rson[now]];
return now;
} int n ,m;
void work(){
tot = ;
data[] = build(,n);
int now = ;
for (int i = ; i <= m; i++){
char str[];
scanf("%s", str);
if (str[] == 'Q'){
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(data[now], l, r, , n));
}else if(str[] == 'C'){
int l, r, d;
scanf("%d%d%d", &l, &r, &d);
data[now+] = insert(data[now], l, r, d, , n);
now++;
}else if(str[] == 'H'){
int l, r, t;
scanf("%d%d%d", &l, &r, &t);
printf("%lld\n", query(data[t], l, r, , n));
}else scanf("%d", &now);
}
printf("\n");
} int main(){ while (scanf("%d%d", &n, &m) != EOF){
//scanf("%d%d", &n, &m);
work();
}
return ;
}