Hdu 1541 Stars (basic tree array)

Source: Internet
Author: User

Stars

 

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission (s): 1287 Accepted Submission (s): 503

Problem Description

Astequalmers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. let the level of a star be an amount of the stars that are not higher and not to the right of the given star. astpolicmers want to know the distribution of the levels of the stars.

 

For example, look at the map shown on the figure above. level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4 ). and the levels of the stars numbered by 2 and 4 are 1. at this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.

 

You are to write a program that will count the amounts of the stars of each level on a given map.

 

Input

The first line of the input file contains a number of stars N (1 <= N <= 15000 ). the following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0 <= X, Y <= 32000 ). there can be only one star at one point of the plane. stars are listed in ascending order of Y coordinate. stars with equal Y coordinates are listed in ascending order of X coordinate.

Output

The output shoshould contain N lines, one number per line. the first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.

Sample Input

5

1 1

5 1

7 1

3 3

5 5

 

Sample Output

1

2

1

1

0

The order in which values increase by y and values do not change by x indicates the number of points at the bottom left of these points ~ How many levels of N-1? This is a typical array of vertices and update points. It is amazing to use a tree array.

Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1541

Code:

Cpp Code

# Include <iostream>

# Include <stdio. h>

# Include <memory. h>

# Include <cmath>

Using namespace std;

 

Int n, a [32005], val [32005];

 

Void update (int p, int k)

{

While (p <= 32005)

{

A [p] + = k;

P + = p & (-p );

}

}

 

Int sum (int x)

{

Int total = 0;

While (x> 0)

{

Total + = a [x];

X-= x & (-x );

}

Return total;

}

 

Int main ()

{

Int I, x, y;

While (scanf ("% d", & n )! = EOF)

{

Memset (a, 0, sizeof ());

Memset (val, 0, sizeof (val ));

For (I = 0; I <n; I ++)

{

Scanf ("% d", & x, & y );

X ++; // note! Cause of x ++: If x = 0 is updated, an endless loop will occur.

Val [sum (x)] ++;

Update (x, 1 );

}

For (I = 0; I <n; I ++)

{

Printf ("% d \ n", val [I]);

}

}

 

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.