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 }
原文:http://www.cnblogs.com/hoskey/p/4335958.html