HDU 1166 enemy deployment (line segment tree)

Source: Internet
Author: User
Enemy army deployment Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 45437 accepted submission (s): 19314


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 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
 
Authorwindbreaker

The basic questions of the line tree, start to brush the questions of the line tree, understand the data structure of the line tree, the tree stores this line segment;

Single point Update (spof) is mainly used to master the achievements, queries, updates, and operations in it. All operations are implemented through recursion;

The following code is used:

# Include <cstdio> # include <cstring> using namespace STD; const int maxn = 50050; struct node/line segment Tree Structure {int L, R, Val ;} T [maxn * 3]; int A [maxn]; void build (INT root, int L, int R) // build {int m; t [root]. L = L; t [root]. R = r; If (L = r) {T [root]. val = A [l]; return;} M = (L + r)/2; build (root * 2, l, m); Build (root * 2 + 1, m + 1, R); t [root]. val = T [root * 2]. val + T [root * 2 + 1]. val;} int query (INT root, int L, int R) // query, recursive query requires the value {int m; If (T [root]. L = L & T [root]. R = r) return T [root]. val; M = (T [root]. L + T [root]. r)/2; If (r <= m) return query (root * 2, L, R); else if (L> m) return query (root * 2 + 1, l, R); else return query (root * 2, l, m) + query (root * 2 + 1, m + 1, R);} void Update (INT root, int ID, int num) // update {If (T [root]. L = T [root]. r) {T [root]. val + = num; // update the return;} else {T [root]. val + = num; If (ID <= T [root * 2]. r) Update (root * 2, ID, num); else Update (root * 2 + 1, ID, num) ;}} int main () {int t, n, i, ID, num; char s [10]; int K = 1; scanf ("% d", & T); While (t --) {scanf ("% d", & N); for (I = 1; I <= N; I ++) scanf ("% d ", & A [I]); Build (1,1, n); printf ("case % d: \ n", K ++); While (1) {scanf ("% s", S); If (strcmp (S, "end") = 0) break; scanf ("% d", & ID, & num); If (strcmp (S, "query") = 0) {printf ("% d \ n", query (1, ID, num ));} if (strcmp (S, "add") = 0) {update (1, ID, num);} If (strcmp (S, "sub") = 0) {update (1, ID,-num) ;}} return 0 ;}


HDU 1166 enemy deployment (line segment tree)

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.