HDU 1166 enemy deployment (line segment tree)

Source: Internet
Author: User
A-enemy troops Time limit:1000 ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit status

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 10 query 1 3add 3 6 query 2 7sub 10 2Add 6 3 query 3 10end

Sample output

Case 1: getting started with the 63359 line tree, you can learn how to create a line tree, update the line tree, and query the line tree.
1 # include <cstdio> 2 # include <cstring> 3 # include <string> 4 # include <stdlib. h> 5 # include <iostream> 6 # include <algorithm> 7 using namespace STD; 8 struct node // node struct 9 {10 int L, R; 11 int num; 12} A [250000]; 13 int ans; 14 void contree (int l, int R, int now) // create a line segment tree 15 {16 A [now]. L = L; 17 A [now]. R = r; 18 if (L = r) // if l = r, it indicates that there are no subnodes, so this is a single camp, enter 19 {20 scanf ("% d", & A [now]. num); 21 return; 22} 23 int mid = (a [now]. L + A [now]. r)/2; 24 contree (L, mid, now <1); // recursion left subtree 25 contree (Mid + 1, R, now <1 | 1 ); // recursion right subtree 26 A [now]. num = A [now <1]. num + A [now <1 | 1]. num; 27} 28 void addtree (int l, int R, int now, int vis, int Val) // update the line segment tree 29 {30 if (L = R & l = vis) 31 {32 A [now]. num + = val; 33 return; 34} 35 int mid = (a [now]. L + A [now]. r)/2; 36 IF (vis> mid) 37 addtree (Mid + 1, R, now <1 | 1, VIS, Val ); 38 If (vis <= mid) 39 addtree (L, mid, now <1, VIS, Val); 40 A [now]. num = A [now <1]. num + A [now <1 | 1]. num; // each time a value is changed, the value of the number to be searched must also be updated. This is very important 41} 42 void sumtree (int l, int R, int now, int L, int R) // query the line segment tree 43 {44 If (L <= L & R <= r) 45 {46 ans + = A [now]. num; 47 return; 48} 49 int mid = (a [now]. L + A [now]. r)/2; 50 if (L> mid) 51 sumtree (Mid + 1, R, now <1 | 1, L, R ); 52 else if (r <= mid) 53 sumtree (L, mid, now <1, L, R); 54 else55 {56 sumtree (L, mid, now <1, L, R); 57 sumtree (Mid + 1, R, now <1 | 1, L, R); 58} 59} 60 int main () 61 {62 int Kase, N, CNT = 0; 63 scanf ("% d", & Kase); 64 While (Kase --) 65 {66 scanf ("% d ", & N); 67 contree (1, n, 1); 68 printf ("case % d: \ n", ++ CNT); 69 while (1) 70 {71 getchar (); 72 string STR; 73 CIN> STR; 74 int X, Y; 75 ans = 0; 76 if (STR = "end ") 77 break; 78 scanf ("% d", & X, & Y); 79 if (STR = "query") 80 {81 sumtree (1, n, 1, x, y); 82 printf ("% d \ n", ANS); 83} 84 else if (STR = "add") 85 addtree (1, n, 1, x, y); 86 else87 addtree (1, n, 1, x,-y); 88} 89} 90 return 0; 91}
View code

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.