HDU4348 To The Moon <带修主席树>

Problem

【HDU4348】To The Moon

Time  Limit:  4000/2000  MS  (Java/Others)\mathrm{Time\;Limit:\;4000/2000\;MS\;(Java/Others)}
Memory  Limit:  65536/65536  KB  (Java/Others)\mathrm{Memory\;Limit:\;65536/65536\;KB\;(Java/Others)}

Description

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 NN integers A[1],A[2],...,A[N]A[1], A[2],..., A[N]. On these integers, you need to implement the following operations:

  1. CC ll rr dd: Adding a constant d for every {Ailir}\{A_i | l \le i \le r\}, and increase the time stamp by 11, this is the only operation that will cause the time stamp increase.
  2. QQ ll rr: Querying the current sum of {Ailir}\{A_i | l \le i \le r\}.
  3. HH ll rr tt: Querying a history sum of {Ailir}\{A_i | l \le i \le r\} in time tt.
  4. BB tt: Back to time tt. And once you decide return to a past, you can never be access to a forward edition anymore.

N,M105N, M \le 10^5, A[i]109|A[i]|\le 10^9, 1lrN1\le l\le r\le N, d104|d|\le 10^4. The system start from time 00, and the first modification is in time 11, t0t\ge 0, and won’t introduce you to a future state.

Input

1
2
3
n m
A1 A2 ... An
... (here following the m operations. )

Output

1
... (for each query, simply print the result. )

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
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

1
2
3
4
5
6
4
55
9
15
0
1

标签:带修主席树

Translation

维护一个数据结构,使得其可有四种操作:区间修改,区间求和,某时间的区间和,返回某时间。

Solution

很明显这是一道主席树的板子题。不过此题要带区间修改。
普通区间修改需要加tagtag,并不断下传。而对于主席树,下传意味着新建节点,可能会MLEMLE。所以这里我们暴力一点,直接标记永久化,这样写起来简洁,而且省空间。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstdio>
#define MAX_N 100000
using namespace std;
struct node {int ls, rs; long long val, tag;} tr[MAX_N*50+5];
int n, m;
int cnt, now, root[MAX_N+5];
void updata(int v, int s, int t) {tr[v].val = tr[tr[v].ls].val+tr[tr[v].rs].val+(long long)(t-s+1)*tr[v].tag;}
void build(int v, int s, int t) {
tr[v].ls = tr[v].rs = tr[v].tag = tr[v].val = 0;
if (s == t) {
scanf("%I64d", &tr[v].val);
return;
}
tr[v].ls = ++cnt, tr[v].rs = ++cnt;
int mid = s+t>>1;
build(tr[v].ls, s, mid);
build(tr[v].rs, mid+1, t);
updata(v, s, t);
}
void modify(int v, int o, int s, int t, int l, int r, long long x) {
tr[v] = tr[o];
if (s >= l && t <= r) {
tr[v].tag += x;
tr[v].val += (long long)(t-s+1)*x;
return;
}
int mid = s+t>>1;
if (l <= mid) modify(tr[v].ls = ++cnt, tr[o].ls, s, mid, l, r, x);
if (r >= mid+1) modify(tr[v].rs = ++cnt, tr[o].rs, mid+1, t, l, r, x);
updata(v, s, t);
}
long long query(int v, int s, int t, int l, int r, long long tot) {
if (s >= l && t <= r) return tr[v].val+(long long)(t-s+1)*tot;
tot += tr[v].tag;
int mid = s+t>>1;
long long ret = 0;
if (l <= mid) ret += query(tr[v].ls, s, mid, l, r, tot);
if (r >= mid+1) ret += query(tr[v].rs, mid+1, t, l, r, tot);
return ret;
}
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
cnt = now = 0;
root[now] = ++cnt;
build(root[now], 1, n);
while (m--) {
char ch;
cin >> ch;
if (ch == 'C') {
int l, r;
long long d;
scanf("%d%d%I64d", &l, &r, &d);
now++;
root[now] = ++cnt;
modify(root[now], root[now-1], 1, n, l, r, d);
}
if (ch == 'Q') {
int l, r;
scanf("%d%d", &l, &r);
printf("%I64d\n", query(root[now], 1, n, l, r, 0LL));
}
if (ch == 'H') {
int l, r, t;
scanf("%d%d%d", &l, &r, &t);
printf("%I64d\n", query(root[t], 1, n, l, r, 0LL));
}
if (ch == 'B') {
int t;
scanf("%d", &t);
now = t;
}
}
}
return 0;
}