1112-curious Robin Hood
|
PDF (中文版) |
Statistics |
Forum |
Time Limit: 1 second (s) |
Memory Limit: Up to MB |
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 nsacks where he keeps this money. The sacks is numbered from 0 to n-1.
Now all time he can he can do one of the three tasks.
1) Give All the money from 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 and he seeks your help.
Input
Input starts with an integer T (≤5), denoting the number of test cases.
Each case contains the integers n (1≤n≤105) and Q (1≤q≤50000). The next line contains n space separated integers in the range [0, +]. The ithinteger 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 in 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 1and 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 |
1 5 6 3 2 1) 4 5 1 4 2 3 4 3 0 3 1 2 3 0 4 1 1 |
Case 1: 5 14 1 13 2 |
Notes
The Dataset is huge, and the use faster I/O methods.
Test Instructions: give you n number and m times query, query divided into three kinds
One, 1 I represents the value of the query I, and the value at I is changed to 0.
Two, 2 I v means the value of I is increased by V.
three, 3 x y the value of the query interval [x, y].
Water problem, directly with a tree-like array can be, there is no need to use line tree.
AC Code: Note that the subscript is starting from 0 to N-1.
#include <cstdio> #include <cstring> #include <queue> #include <stack> #include <vector># Include <cmath> #include <cstdlib> #include <algorithm> #define MAXN 100000+10#define MAXM 60000+10# Define INF 1000000#define EPs 1e-8using namespace std;int N, m;int c[maxn<<1];int k = 1;int lowbit (int x) {return X & (-X);} int sum (int x) {int s = 0; while (x > 0) {s + = c[x]; X-= Lowbit (x); } return s;} void update (int x, int d) {while (x <= N) {c[x] + = D; x + = Lowbit (x); }}void solve () {int x, Y, D, op; printf ("Case%d:\n", k++); while (m--) {scanf ("%d", &op); if (op = = 1) {scanf ("%d", &x); x + +; printf ("%d\n", sum (x)-sum (x-1)); int t = SUM (x)-sum (x-1); Update (x,-T); } else if (op = = 2) {scanf ("%d%d", &x, &d); x + +; Update (x, D); } else {scanf ("%d%d", &x, &y); x + +, y++; printf ("%d\n", Sum (y)-sum (x-1)); }}}int Main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n, &m); memset (c, 0, sizeof (c)); int A; for (int i = 1; I <= N; i++) {scanf ("%d", &a); Update (I, a); } solve (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Lightoj 1112-curious Robin Hood "single point modification + single point, Interval Query" "Tree-like array water problem"