Enemy army deployment
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 31250 Accepted Submission (s): 13398
Problem Description
Country C's rival Country A is conducting military exercises during this time, so the country C's spy chief Derek and his Tidy 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, 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 Input
1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 10 2Add 6 3Query 3 10End
Sample Output
Case 1:63359
Code:
It's basically the same as NY's 116 soldiers (2)
# Include "stdio. h "# include" stdlib. h "# include <string. h> int n, m; int sum [1000000]; int ans; int minpower (int x) // min power {return x & (-x );} void update (int I, int add) {while (I <= n) {sum [I] + = add; I + = minpower (I );}} int getsum (int a, int B, int ans) // evaluate the range ~ Total number of enemies killed by B's soldiers {int j = minpower (B); int sub = 0; ans + = sum [B]; if (B-j + 1)> a) {getsum (a, B-j, ans);} else if (B-j + 1) = a) return ans; else {return (ans-getsum (B-j + 1, A-1, sub) ;}} int main () {char order [6]; int I, j, k, a, B, t, ca; scanf ("% d", & t); for (ca = 1; ca <= t; ca ++) {scanf ("% d", & n); sum [0] = 0; for (I = 1; I <= n; I ++) {scanf ("% d", & sum [I]); j = minpower (I); for (k = I-1; k> = I-j + 1 ;) {sum [I] + = sum [k]; k = k-minpower (k) ;}} printf ("Case % d: \ n", ca ); while (1) {ans = 0; scanf ("% s", order); if (strcmp (order, "End") = 0) break; scanf ("% d", & a, & B); switch (order [0]) {case 'q': printf ("% d \ n ", getsum (a, B, ans); break; case 'A': update (A, B); break; case's ': update (a,-B ); break; }}return 0 ;}