HDU 1556: color the ball (second type tree array-Interval Update, vertex summation)

Source: Internet
Author: User
Color the ball

Time Limit: 9000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 8984 accepted submission (s): 4594


Problem descriptionn balloons are arranged in a row, numbered 1, 2, 3 .... n. given two integers a B (A <= B) each time, Lele colors each balloon one time from balloon a to balloon B, riding his "little pigeon" electric car. But after N times, Lele has forgotten how many times the I-th balloon has been painted. Can you help him figure out how many times each balloon has been painted?

 

Input the first behavior of each test instance is an integer N, (n <= 100000 ). next n rows, each row contains two integers, a B (1 <= A <= B <= N ).
When n = 0, the input ends.

 

Output each test instance outputs a row, which contains N integers. The number of I represents the total number of times that the I balloon is colored.

 

Sample input31 12 23 331 11 21 30

 

Sample output1 1 13 2 1

 

Author8600

 

Sourcehdu 2006-12 Programming Contest

 

Recommendll | we have carefully selected several similar problems for you: 1542 1394 1255 2795 3397 Category 2 Tree array, Interval Update, vertex sum. Because we usually use the vertex update and interval sum questions, when I know that this question can be done using a tree array, I am shocked. The original tree array can be used like this! Amazing! I really raised my mind--|, although I have not understood this method thoroughly. For more information about tree array classification, see --> tree array summary. For more information about the practices of line tree, see right --> HDU 1556: color the ball (line tree, Interval Update, classic question) Question: Color the balloon in the specified range once, apply N times, and finally calculate the number of times each balloon has been coated. Ideas: In my understanding, the tree array is understood as a line segment tree. Each node represents an interval, and the value of each interval represents the elements and within the jurisdiction of this interval, the interval of the previous layer contains the values of the interval of the next layer. The values of the top nodes represent the sum of all elements in the entire array. So how to update the value of range [a, B? Here we need to add the value of range [a, B] to 1. The Code is as follows:
    Update(a,1);        Update(b+1,-1);

Note that the value of update a is equivalent to the value of update range [A, N] (n is the limit/because a wants to update the values of all subsequent intervals ), the value for updating B + 1 is [B + 1, N] (the value of all intervals after updating B ).

Therefore, the range [A, N] value is + 1, and then the value of [B + 1, N] is-1. Finally, the range [, b] value + 1.

 

The following are two ways to implement the second type of tree array:

  Code 1 (update up, sum down):

1 # include <iostream> 2 using namespace STD; 3 4 int N; // limit of 5 Int C [100010]; 6 7 int lowbit (int x) 8 {9 return X &-X; 10} 11 12 Void Update (int x, int v) // update the interval [x, n] value 13 {14 While (x <= N) {15 C [x] + = V; 16 x + = lowbit (X ); 17} 18} 19 20 int sum (int x) // evaluate the sum before X and 21 {22 int sum = 0; 23 while (x> 0) {24 sum + = C [X]; 25 x-= lowbit (x); 26} 27 return sum; 28} 29 30 int main () 31 {32 int, b, I; 33 while (scanf ("% d", & N )! = EOF) {34 memset (C, 0, sizeof (c); 35 if (n = 0) break; 36 for (I = 0; I <N; I ++) {37 scanf ("% d", & A, & B); 38 update (A, 1); // set the interval to [, the value of N] is + 1, and then the value of the irrelevant range [B + 1, N] is-1, that is, the value of the range [a, B] is + 139 Update (B + 1, -1); 40} 41 for (I = 1; I <= N; I ++) {42 printf ("% d", sum (I )); // calculate a single vertex and directly calculate sum43 if (I <n) 44 printf (""); 45} 46 printf ("\ n"); 47} 48 return 0; 49}

 

  Code 2 (update down, sum up):

1 # include <iostream> 2 using namespace STD; 3 4 int N; // limit of 5 Int C [100010]; 6 7 int lowbit (int x) 8 {9 return X &-X; 10} 11 12 Void Update (int x, int v) // update the interval [x, n] value 13 {14 While (x> 0) {15 C [x] + = V; 16 x-= lowbit (x); // change, + changed to-, and updated 17} 18} 19 20 int sum (int x) Down. // evaluate the sum before X and 21 {22 int sum = 0; 23 while (x <= N) {24 sum + = C [X]; 25 x + = lowbit (x); // change,-changed to +, sum up 26} 27 return sum; 28} 29 30 int main () 31 {32 int A, B, I; 33 While (scanf ("% d", & N )! = EOF) {34 memset (C, 0, sizeof (c); 35 if (n = 0) break; 36 for (I = 0; I <N; I ++) {37 scanf ("% d", & A, & B); 38 Update (A-1,-1); // set range [1, a-1] value-1, then the irrelevant range [1, B] value + 1, that is, the range [a, B] value + 139 Update (B, 1 ); 40} 41 for (I = 1; I <= N; I ++) {42 printf ("% d", sum (I )); // calculate a single vertex and directly calculate sum43 if (I <n) 44 printf (""); 45} 46 printf ("\ n"); 47} 48 return 0; 49}

 

Freecode: www.cnblogs.com/yym2013

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.