Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1166
Enemy army deployment
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 43689 accepted submission (s): 18539
Problem descriptionc's dead enemy country A is conducting military exercises during this time, so Derek and tidy of country C are busy again. Country A has deployed n barracks along the coastline. Derek and tidy are responsible for monitoring the activities of these barracks. Some advanced monitoring means have been adopted, so the number of people in each engineer camp is clearly understood by country C. The number of people in each engineer camp may change, and the number of workers may increase or decrease, but none of these can escape the surveillance of country C.
The Central Intelligence Agency needs to study what tactics the enemy actually exercises, so tidy must report to Derek at any time the total number of people in a continuous barracks. For example, Derek asked: "tidy, report on the total number of people from 3rd camps to 10th camps!" Tidy is about to calculate the total number of people in this section and report it immediately. However, the number of people in the enemy barracks often changes, and Derek asks for different segments each time. Therefore, tidy has to go to the camp one by one each time and is exhausted soon, derek is getting less and more dissatisfied with tidy's computing speed: "You are a fat boy, it's so slow. I'll fry you!" Tidy thought, "you can calculate it yourself. This is really a tiring job! I wish you fired my squid !" In desperation, tidy had to call windbreaker, a computer expert, for help. Windbreaker said: "Fat Boy, I want you to do more ACM questions and read more algorithm books. Now I have a bitter taste !" Tidy said, "I know the error... "However, windbreaker is disconnected. Tidy is very upset. In this case, he will actually crash. Smart readers, can you write a program to help him complete the job? However, if your program is not efficient enough, tidy will still be scolded by Derek.
The first line of input is an integer T, indicating that T groups of data exist.
The first line of each group of data is a positive integer n (n <= 50000), indicating that the enemy has n barracks, followed by n positive integers, the I positive integer AI represents an AI individual (1 <= AI <= 50) at the beginning of the I barracks ).
Next, each line contains a command in four forms:
(1) Add I j, I and j are positive integers, indicating that J individuals are added to camp I (J cannot exceed 30)
(2) sub I j, I and j are positive integers, indicating that J individuals are reduced in camp I (J cannot exceed 30 );
(3) query I j, I and j are positive integers. I <= J indicates the total number of camp I to J;
(4) end indicates the end. This command appears at the end of each group of data;
Each group of data can contain a maximum of 40000 commands.
Output for group I data, first output "case I:" And press enter,
For each query, output an integer and press enter to indicate the total number of the queried segments, which is kept within Int.
Sample input1101 2 3 4 5 6 7 8 9 10 query 1 3 add 3 6 query 2 7 sub 10 2 Add 6 3 query 3 10 end
Sample outputcase 33 59 question: I will give you a set of numbers, and then choose to increase or decrease a bit Based on the question, or ask how many people are there in a certain range? You can use a tree-like tree for this question, which is convenient and fast.
1 # include <iostream> 2 # include <cstring> 3 # include <algorithm> 4 5 using namespace STD; 6 7 int sum [50005]; 8 int N; 9 10 int lowbit (int x) // obtain the nth bit 1 of X. For example, 4 is returned. For example, 5 is returned, 111 {12 Return X & (-x ); 13} 14 15 void Update (int I, int Val) // Add the I-th element to the ancestor of val16 {17/I to add val18 while (I <= N) 19 {20 21 Sum [I] + = val; 22 I + = lowbit (I ); // complete the binary non-bitwise of I to get its ancestor 23} 24} 25 26 int sum (int I) // calculate the sum of the first I items and 27 {28 int S = 0; 29 // segment the first I items 30 While (I> 0) 31 {32 33 S + = sum [I]; 34 I-= lowbit (I ); // remove the binary of I. The last 35 36} 37 return s; 38} 39 40 41 int main () 42 {43 int T, I, j, X, A, B; 44 char STR [6]; 45 CIN> T; 46 for (I = 1; I <= T; I ++) 47 {48 printf ("case % d: \ n ", I); 49 50 memset (sum, 0, sizeof (SUM); 51 52 CIN> N; 53 54 for (j = 1; j <= N; j ++) 55 {56 CIN> X; 57 Update (J, x); 58} 59 60 while (scanf ("% s ", str )! = EOF & strcmp (STR, "end ")! = 0) 61 {62 scanf ("% d", & A, & B); 63 64 switch (STR [0]) 65 {66 case 'q ': printf ("% d \ n", sum (B)-sum (A-1); break; 67 case's ': Update (A,-B); break; 68 case 'A': Update (a, B); break; 69} 70 71} 72} 73 return 0; 74}View code