【HDU4348】【主席树】To the moon
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 {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.
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.
Input
n m
A1 A2 ... An
... (here following the m operations. )
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
Author
HIT
Source
【分析】
简单题,唯一要注意的是内存大小...
写指针各种被卡....无奈最后写数组...
看到有人用可持久化树状数组做...我开始算了一下好像会超内存,然后就没写了。。Orzzz
1 /* 2 唐代高蟾 3 《金陵晚望》 4 5 曾伴浮云归晚翠,犹陪落日泛秋声。 6 世间无限丹青手,一片伤心画不成。 7 */ 8 #include <iostream> 9 #include <cstdio> 10 #include <algorithm> 11 #include <cstring> 12 #include <vector> 13 #include <utility> 14 #include <iomanip> 15 #include <string> 16 #include <cmath> 17 #include <queue> 18 #include <assert.h> 19 #include <map> 20 #include <ctime> 21 #include <cstdlib> 22 #include <stack> 23 #define LOCAL 24 const int MAXN = 3000000 + 10; 25 const int MAXM = 1000000 + 10; 26 const int INF = 100000000; 27 const int SIZE = 450; 28 const int maxnode = 0x7fffffff + 10; 29 using namespace std; 30 typedef long long ll; 31 int lson[MAXN], rson[MAXN]; 32 int data[MAXN], add[MAXN]; 33 ll sum[MAXN]; 34 int tot; 35 36 37 int insert(int t, int ll, int rr, int d, int l, int r){ 38 int now = ++tot; 39 lson[now] = lson[t]; 40 rson[now] = rson[t]; 41 add[now] = add[t]; 42 sum[now] = sum[t]; 43 sum[now] += (long long)(d * (rr - ll + 1)); 44 if(ll == l && rr == r){ 45 add[now] += d; 46 return now; 47 } 48 int mid = (l + r)>>1; 49 if(rr <= mid) lson[now] = insert(lson[t], ll, rr, d, l, mid); 50 else if(ll > mid) rson[now] = insert(rson[t], ll, rr, d, mid + 1, r); 51 else{ 52 lson[now] = insert(lson[t], ll, mid, d, l, mid); 53 rson[now] = insert(rson[t], mid + 1, rr, d, mid + 1, r); 54 } 55 return now; 56 } 57 //在t的根内查询[ll, rr]区间的值 58 long long query(int t, int ll, int rr, int l, int r){ 59 long long Ans = (long long)(add[t] * (rr - ll + 1)); 60 if (ll == l && rr == r) return sum[t]; 61 int mid = (l + r)>>1; 62 if (rr <= mid) Ans += query(lson[t], ll, rr, l, mid); 63 else if (ll > mid) Ans += query(rson[t], ll, rr, mid + 1, r); 64 else { 65 Ans += query(lson[t], ll, mid, l, mid); 66 Ans += query(rson[t], mid + 1, rr, mid + 1, r); 67 } 68 return Ans; 69 } 70 int build(int ll, int rr){ 71 int now = ++tot; 72 add[now] = 0; 73 if (ll == rr){ 74 scanf("%lld", &sum[now]); 75 lson[now] = rson[now] = 0; 76 return now; 77 } 78 int mid = (ll + rr)>>1; 79 lson[now] = build(ll, mid); 80 rson[now] = build(mid + 1, rr); 81 sum[now] = sum[lson[now]] + sum[rson[now]]; 82 return now; 83 } 84 85 int n ,m; 86 void work(){ 87 tot = 0; 88 data[0] = build(1,n); 89 int now = 0; 90 for (int i = 1; i <= m; i++){ 91 char str[3]; 92 scanf("%s", str); 93 if (str[0] == 'Q'){ 94 int l, r; 95 scanf("%d%d", &l, &r); 96 printf("%lld\n", query(data[now], l, r, 1, n)); 97 }else if(str[0] == 'C'){ 98 int l, r, d; 99 scanf("%d%d%d", &l, &r, &d); 100 data[now+1] = insert(data[now], l, r, d, 1, n); 101 now++; 102 }else if(str[0] == 'H'){ 103 int l, r, t; 104 scanf("%d%d%d", &l, &r, &t); 105 printf("%lld\n", query(data[t], l, r, 1, n)); 106 }else scanf("%d", &now); 107 } 108 printf("\n"); 109 } 110 111 int main(){ 112 113 while (scanf("%d%d", &n, &m) != EOF){ 114 //scanf("%d%d", &n, &m); 115 work(); 116 } 117 return 0; 118 }