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 n sacks 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 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 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 a set of numbers, perform the three kinds of actions given by the topic, and output the desired results.
Exercises
The most basic problem of line segment tree, only need single point update and interval query
Long time did not do line tree, did not expect to be able to 1 A .... This is the topic of how much water ....
/*http://blog.csdn.net/liuke19950717*/#include <cstdio> #include <cstring> #include <algorithm> Using namespace Std;typedef long long ll;const int maxn=1e5+5;int x[maxn],sumtree[maxn*4],kase=0;void build (int rt,int L, int r) {if (l==r) {Sumtree[rt]=x[l];return;} int mid= (L+R) >>1,tp=rt<<1;build (Tp,l,mid); build (tp|1,mid+1,r); Sumtree[rt]=sumtree[tp]+sumtree[tp|1] ;} void update (int rt,int l,int r,int a,int b) {if (l==r) {Sumtree[rt]=x[l]=b;return;} int mid= (L+R) >>1,tp=rt<<1;if (mid>=a) {update (TP,L,MID,A,B);} Else{update (tp|1,mid+1,r,a,b);} SUMTREE[RT]=SUMTREE[TP]+SUMTREE[TP|1];} int find (int rt,int l,int r,int a,int b) {if (l>=a&&r<=b) {return sumtree[rt];} int mid= (L+R) >>1,tp=rt<<1,ans=0;if (mid>=a) {ans+=find (tp,l,mid,a,b);} if (mid<b) {ans+=find (tp|1,mid+1,r,a,b);} return ans;} int main () {//freopen ("Shuju.txt", "R", stdin), int t;scanf ("%d", &t), while (t--) {int N,m;memset (sumtree,0,sizeof ( Sumtree)); scanf ("%d%d", &n,&m); for (int i=1;i< =n;++i) {scanf ("%d", &x[i]);} Build (1,1,n);p rintf ("Case%d:\n", ++kase), while (m--) {int num;scanf ("%d", &num), if (num==1) {int a;scanf ("%d", &a);p rintf ("%d\n", x[a+1]); update (1,1,n,a+1,0);} else if (num==2) {int a,b;scanf ("%d%d", &a,&b); update (1,1,N,A+1,X[A+1]+B);} Else{int a,b;scanf ("%d%d", &a,&b);p rintf ("%d\n", Find (1,1,n,a+1,b+1));}}} return 0;}
Light OJ 1112-curious Robin Hood "single point Update"