HDU 1556 Color the ball-from lanshui_Yang

Source: Internet
Author: User

Problem Description
N balloons are arranged in a row, numbered 1, 2, 3... from left to right .... 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 I number indicates the total number of times that the I balloon is colored.


Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0

Sample Output
1 1 1
3 2 1

This is a typical one-dimensional tree array deformation. The common one-dimensional tree array is used for single-point update and interval evaluation. This question is another use of the tree array: Interval Update, single-point evaluation. The principle is as follows:

Assume that the elements of the original array are a [1], a [2],… A [n], then d [n] = a [1] + a [2] + ...... + A [n] calculates the sum of the First n items. This is the first purpose of the tree array: single-point update, interval summation.

Then, make some changes. Assume that the elements of the original array are a [1]-0, a [2]-a [1], a [3]-a [2], ......, A [n]-a [n-1], then the first n items and d [n] = a [n], that is, now the first n items and d [n] of the original array are equal to the single point value a [n]. do you understand this?

Next, If You Want To [a [m]... If all values in a [n] + Val, you only need to add Val to the m entry (a [m]-a [m-1]) of the original array, and subtract Val from n + 1 (a [n + 1]-a [n]), so that when m <= I <= n,

The first I and:

D [I] = (a [1]-0) + (a [2]-a [1]) + (a [3]-a [2]) + ...... + (A [m]-a [m-1] + val) + (a [m + 1]-a [m]) + ...... + (A [I]-a [I-1]) = a [I] + val.

Similarly, when I> n, d [I] is equal to the original a [I]. If you can see this, you will be very open. Note that a [1]... The initial values of a [n] are 0 !!

See the code below:

 

#include<iostream>#include<cstring>#include<string>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>using namespace std ;const int MAXN = 1e5 + 5 ;int C[MAXN] ;int n ;int lowbit (int x){    return x & -x ;}void add(int x , int d){    while(x <= n)    {        C[x] += d ;        x += lowbit(x) ;    }}int sum(int x){    int sumt = 0 ;    while (x > 0)    {        sumt += C[x] ;        x -= lowbit(x) ;    }    return sumt ;}int main(){    while (scanf("%d" , &n) != EOF)    {        if(n == 0 )            break ;        memset(C , 0 , sizeof(C)) ;        int t = n ;        int i ;        while ( t-- )        {            int a , b ;            scanf("%d%d", &a , &b) ;            add(a , + 1) ;            add(b + 1 , -1) ;        }        for(i = 1 ; i <= n ; i ++)        {            printf("%d" , sum(i)) ;            if(i < n)                printf(" ") ;        }        puts("") ;    }    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.