Time Limit: 1000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 1166
64-bit integer Io format: % i64d Java class name: main country C's rival Country A is conducting military exercises during this time, so Derek and tidy from 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 members in the query segment. The maximum number is 1000000.
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
Solution: Line Segment tree... Try zkw line segment tree...
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 50005;18 int tree[maxn<<2],M;19 void build(int n){20 M = 1<<((int)log2(n)+1);21 for(int i = M + 1; i <= M + n; i++)22 scanf("%d",tree+i);23 for(int i = M - 1; i > 0; --i)24 tree[i] = tree[i<<1] + tree[i<<1|1];25 }26 void add(int n,int val){27 for(tree[n += M] += val,n >>= 1; n; n >>= 1)28 tree[n] = tree[n<<1] + tree[n<<1|1];29 }30 int query(int s,int t){31 int ans = 0;32 s += M - 1;33 t += M + 1;34 while(s^t^1){35 if(~s&1) ans += tree[s^1];36 if(t&1) ans += tree[t^1];37 s >>= 1;38 t >>= 1;39 }40 return ans;41 }42 int main() {43 int t,n,x,y,cs = 1;44 char s[12];45 scanf("%d",&t);46 while(t--){47 scanf("%d",&n);48 memset(tree,0,sizeof(tree));49 build(n);50 printf("Case %d:\n",cs++);51 while(true){52 scanf("%s",s);53 if(s[0] == ‘E‘) break;54 scanf("%d %d",&x,&y);55 if(s[0] == ‘Q‘)56 printf("%d\n",query(x,y));57 else if(s[0] == ‘A‘)58 add(x,y);59 else if(s[0] == ‘S‘)60 add(x,-y);61 }62 }63 return 0;64 }
View code
HDU 1166 enemy army deployment