Hdu 1166 tree array line segment tree

Source: Internet
Author: User
Tags tidy

Hdu 1166 tree array line segment tree
Enemy deployment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 51177 Accepted Submission (s): 21427

Problem Description C's dead enemy country A is conducting military exercises during this time, So 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.

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

Author Windbreaker tree array... I forgot about it and read it again...
// Knowledge point: Tree array line segment tree, # include
 
  
# Include
  
   
# Define maxn 50010int a [maxn]; char s [10]; int lowbit (int x) {// return x &-x; return x & (x ^ (x-1);} void insert (int x, int add) {while (x
   
    
{A [x] + = add; x + = lowbit (x); // from a subset containing x to another subset} int sum (int n) {int sum1 = 0; while (n> 0) // The number of contained n must contain the last a [n] From the back to the front, that is, add {sum1 + = a [n]; n-= lowbit (n ); // transfer from one subset and one subset to another} return sum1;} int main () {int t; int count = 1; scanf ("% d", & t ); while (t --) {int I, j, n, m; scanf ("% d", & n); memset (a, 0, sizeof ()); for (I = 1; I <= n; ++ I) {scanf ("% d", & m); insert (I, m );} printf ("Case % d: \ n", count ++); int pos, num; getchar (); while (1) // you can You can add a while (1) loop for multiple times {// gets (s); // gets cannot be used here. Khan scanf ("% s", s); if (s [0] = 'A') {scanf ("% d", & pos, & num ); insert (pos, num);} if (s [0] = 's') {scanf ("% d", & pos, & num ); insert (pos,-num);} if (s [0] = 'q') {scanf ("% d", & pos, & num ); printf ("% d \ n", sum (num)-sum (pos-1);} if (s [0] = 'E') break ;}} return 0 ;}
   
  
 


Related Article

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.