1166-enemy troops, 1166-enemy soldiers
Description: The enemy of country C 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.
Input: the first line is an integer T, indicating that there are T groups of data.
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, "Case I:" And press enter are output first. For each Query, an integer is output and press enter to indicate the total number of users in the Query segment, the value must be within int.
Input:
1 10 1 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 output: Case 1: 6 33 59 analysis: the difficulty of this question lies in the problem of summation of a large amount of data. If we use cyclic summation every time, it will definitely time out. Here we need to use a tree array, which is specially used for processing a large amount of data. Another blog post will describe the tree array in detail, where only the code is provided.
1 # include <iostream> 2 # include <stdio. h> // reading with scanf is more efficient. Using cin still times out. 3 # include <string. h> 4 using namespace std; 5 6 static int a [50014], B [50014], N; 7 int lowbit (int t) // calculate the jurisdiction of this point 8 {9 return t & (-t); 10} 11 void upDate (int x, int num) // modify the tree array 12 {13 while (x <= N) 14 {15 B [x] + = num; 16 x + = lowbit (x ); 17} 18} 19 int getSum (int x) // calculate the sum of 0-x and 20 {21 int s = 0; 22 while (x> 0) 23 {24 s + = B [x]; 25 x-= lowbit (x); 26} 27 return s; 28} 29 30 int main () 31 {32 int n, num; 33 scanf ("% d", & n); 34 for (int I = 0; I <n; I ++) 35 {36 cout <"Case" <I + 1 <":" <endl; 37 scanf ("% d", & num); N = num; 38 for (int I = 1; I <= num; I ++) 39 B [I] = 0; 40 for (int I = 1; I <= num; I ++) // create a tree array B [] 41 {42 scanf ("% d", & a [I]); 43 upDate (I, a [I]); 44} 45 char str [10]; 46 while (scanf ("% s", str) & (strcmp (str, "End "))) 47 {48 int I, j; 49 scanf ("% d", & I, & j); 50 switch (str [0]) 51 {52 case 'A': upDate (I, j); break; 53 case 's': upDate (I,-j); break; 54 case 'q ': cout <getSum (j)-getSum (I-1) <endl; break; 55} 56} 57} 58 return 0; 59}