Hdu-1556-Color the ball (tree array is the easiest to use)

Source: Internet
Author: User

Color the ball

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


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 Input

 
31, 12, 23, 331, 11, 21, 30


Sample output

1 1 13 2 1
# Include <iostream> # include <cstring> using namespace STD; int tree [100100]; int maxn; void Update (int K, int v) {While (k <= maxn) {tree [k] + = V; int T = tree [k]; // printf ("tree [% d] = % d \ n", K, T); k + = K &-K; // K = K * 2; // printf ("k = % d \ n", k); // printf ("\ n") ;}} int read (int K) // çóçø ¼ ä1 µ½ K µä° íöμ {int sum = 0; while (k> 0) {// printf ("k111111 = % d \ n", k); sum + = tree [k]; // printf ("summmmmm = % d \ n ", sum); k-= K &-K; // Printf ("k2222222 = % d \ n", k); // printf ("\ n");} return sum;} int main () {int I, a, B, temp; while (scanf ("% d", & maxn) {temp = maxn; memset (tree, 0, sizeof (tree )); while (temp --) {scanf ("% d", & A, & B); Update (A, 1); Update (B + 1,-1 );} for (I = 1; I <= maxn; I ++) {printf ("% d", read (I); if (I! = Maxn) printf (""); else printf ("\ n") ;}} return 0 ;}

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.