1112 - Curious Robin Hood
Now each time he can he can do one of the three tasks.Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.
1) Give all the money of the ith sack to the poor, leaving the sack empty.
2) Add new amount (given in input) in the ith sack.
3) Find the total amount of money from ith sack to jth sack.
Since he is not a programmer, he seeks your help.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ith integer denotes the initial amount of money in the ith sack (0 ≤ i < n).
Each of the next q lines contains a task in one of the following form:
1 i Give all the money of the ith (0 ≤ i < n) sack to the poor.
2 i v Add money v (1 ≤ v ≤ 1000) to the ith (0 ≤ i < n) sack.
3 i j Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).
Output
For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.
Sample Input |
Output for Sample Input |
15 63 2 1 4 51 4
2 3 4 3 0 3 1 2 3 0 4 1 1 |
Case 1:5141
13 2 |
Notes
Dataset is huge, use faster I/O methods.
题目类型:线段树
算法分析:线段树简单的单点更新和区间求和问题,直接建树,更新和查询即可
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
#include <iostream> #include <fstream> #include <sstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const int maxn = 100000 +66; int sum[maxn<<2]; int giveval; void PushUp (int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void Build (int rt, int l, int r) { if (l == r) { scanf ("%d", &sum[rt]); return; } int m = (l + r) >> 1; Build (lson); Build (rson); PushUp (rt); } void UpDate (int rt, int l, int r, int val, int num) { if (l == r) { sum[rt] += num; return ; } int m = (l + r) >> 1; if (val <= m) UpDate (lson, val, num); else UpDate (rson, val, num); PushUp (rt); } void Give (int rt, int l, int r, int val) { if (l == r) { giveval = sum[rt]; sum[rt] = 0; return ; } int m = (l + r) >> 1; if (val <= m) Give (lson, val); else Give (rson, val); PushUp (rt); } int Query (int rt, int l, int r, int L, int R) { if (L <= l && r <= R) return sum[rt]; int m = (l + r) >> 1; int ans = 0; if (L <= m) ans += Query (lson, L, R); if (R > m) ans += Query (rson, L, R); return ans; } int main() { // freopen ("aaa.txt", "r", stdin); int t, flag =1; scanf ("%d", &t); while (t--) { printf ("Case %d:\n", flag++); int n, q; scanf ("%d%d", &n, &q); Build (1, 1, n); int i, oper, val_a, val_b; for (i = 0; i < q; i++) { scanf ("%d", &oper); if (oper == 1) { scanf ("%d", &val_a); val_a++; Give (1, 1, n, val_a); printf ("%d\n", giveval); } else if (oper == 2) { scanf ("%d%d", &val_a, &val_b); val_a++; UpDate (1, 1, n, val_a, val_b); } else if (oper == 3) { scanf ("%d%d", &val_a, &val_b); val_a++, val_b++; printf ("%d\n", Query (1, 1, n, val_a, val_b)); } } } return 0; } |
- « 上一篇:lightoj1088
- lightoj1113:下一篇 »