D-Enemy soldiers
crawling in process ... crawling failed time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u
Description
Lily is particularly fond of growing flowers, but because of her special flowers, it is not easy to take care of the flowers. She put her flowers in line, each potted plant has a beautiful value. If Lily takes care of a potted flower, the aesthetic value of the potted plant will rise, and if the care is not good, the beauty of the potted plant will fall. Sometimes Lily wants to know the sum of the beauty of a continuous flower, but Lily's arithmetic is not very good, can you tell her the result quickly?
Input
The first line is an integer t, which indicates that there is a T group of test data.The first behavior of each set of test data is a positive integer N (n<=50000), which means Lily has n potted plants. Next there are n positive integers, and the first positive integer AI represents the initial aesthetic value of the potted plant (1<=ai<=50). Next, there is a command in each line, the command has 4 forms:(1) Add i J, I and J are positive integers, indicating that the first potted flower is cared for good, aesthetic value increases J (j<=30)(2) Sub i J, I and J are positive integers, Indicates that the first potted flower is cared for is not good, beauty value reduction J (j<=30)(3) Query I J, I and J is a positive integer, i<=J, said to ask the first flower to the J potted Flower aesthetic value of the sum (4) End, indicating the end, This command does not have more than 40,000 commands per set of data at the end of each set of data
Output
For group I data, the first output is "case I:" and a carriage return. for Each "Query i J" command, outputs the sum of the aesthetic values of the potted flower to the J.
Sample Input
19
7 9 8 4 4 5 4 2 7
Query 7 9
ADD 4 9
Query 3 6
Sub 9 6
Sub 3 3
Query 1 9
End
Sample Output
Case 1:13
30
50 problem Analysis: The first direct two for loop "attempt" brute force, but tle blow, followed by a tree-shaped array to solve the problem.
1#include"Cstdio"2#include"string.h"3 intflower[50005];4 intN;5 voidAddintAintb)6 {7 while(A <=N)8 {9Flower[a] + =b;TenA + = A &-A; One } A } - intSuminta) - { the ints=0; - while(a>0) - { -s + =Flower[a]; +A-= A &-A; - } + returns; A } at intMain () - { - intt,a,b; - Chars[ A]; -scanf ("%d",&t); - for(intj=1; j<=t;j++) in { -scanf"%d",&n); tomemset (Flower,0,sizeof(flower)); + for(intI=1; i<=n;i++) - { thescanf ("%d",&b); * Add (i,b); $ }Panax Notoginsengprintf"Case %d:\n", j); - while(SCANF ("%s", s) && s[0] !='E') the { +scanf ("%d%d",&a,&b); A if(s[0] =='A') the Add (A, b); + if(s[0] =='S') -Add (a,-b); $ if(s[0] =='Q') $printf ("%d\n", sum (b)-sum (A-1)); - } - } the return 0; -}
View Code
Summer Camp (2) Fourth bomb-----Enemy soldiers (hdu1166)