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.
Astronomy often investigates the star map, which represents all the stars on a plane, and each star has Cartesian coordinates. The star level is the total number of stars that are not above it and are not on the right. Astronomers want to know the level of each star.
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.
For example, look at the figure above. The number of stars on the 5th is 3 (derived from the numbers 1, 2, and 4 ). The level of stars 2 and 4 is 1. in this shard, there is only one star with a level of 0, two stars with a level of 1, one star with a level of 2 and one star with a level of 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Your task is to calculate the total number of stars at each level
Input
The first line of the input 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.
The first row contains N (1 <=n <= 15000 ). Next, N rows describe the corresponding stars (each line contains two integers x and y, 0 <= X, Y <= 32000) separated by spaces ). Each coordinate on a plane can have at most one star. Stars are arranged in ascending order of Y, and Y is equal to Y in ascending order of X.
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.
The output contains N rows, one in each row. The first row contains the total number of stars whose level is 0, the second row is the total number of stars whose level is 1, and so on, the last row is the total number of stars whose level is N-1.
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample output
1
2
1
1
0
Analysis:
I use tree array. For each vertex, we first sort the vertices in ascending order of X, and X in ascending order of Y, in this way, we can ensure that each subsequent vertex must be on the right of the previous vertex, so that the computation of each vertex will not be affected, then we compress and compress our boss (the stars it affects) for each point, so our method will come out: the first step we need to do for each point: first, let's look at the number of stars (not including ourselves) below it. Code: getsum (Y + 1) because we start from the very bottom point, then we can determine which level the current star belongs to. Then we can use the unit of the level to ++ and change its boss with our own value, because it is not counted into itself, but for all its superiors, it will be increased by a level, so it should be counted in the scope of its superiors, code: Modify (y) in this way, we can calculate the level of each star. Note: 0 is very dangerous, because lowbit (0) will have an infinite loop, so each of my vertical (y) coordinates is raised by a grid, and then above or above, results are not affected.
Algorithm superiority: (1): This eliminates the need to calculate left-hand statistics every time. Each of them automatically counts stars lower than itself to calculate their own level, and finally calculates their own Y coordinate ++, then, you only need to look down. (save time) (2): After each left-side sorting, it is ensured that each subsequent vertex will not appear before the previous vertex, ensuring the correctness of the algorithm.
[Reference Program ]:
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Struct Node
{
LongX, Y;
} Point [2, 15002];
Long C [2, 32002], level [2, 32002];
LongN;
Ong lowbit (longX)
{
Return x ^ (X & (x-1));
}
Void modify (longX)
{
While (x <= 32001)
{
C [x] ++;
X + = lowbit(X );
}
}
Long getsum (longX)
{
Long S = 0;
While(X)
{
S + = C [x];
X-= lowbit(X );
}
ReturnS;
}
Int CMP (const void * s, const void *T)
{
Node I = * (node *) s, j = * (node *) T;
If (I. X! = J. X) return I. x-j.x;
Else return I. y-j.y;
}
IntMain ()
{
Scanf ("% d ",&N );
For (INT I = 1; I <= N; I ++)
Scanf ("% d", & point [I]. X, & point [I]. Y);
Qsort (point + 1, n, sizeof(Node), CMP );
Memset (C, 0, sizeof(C ));
Memset (Level, 0, sizeof(Level ));
LongK;
For (INT I = 1; I <= N; I ++)
{
K = getsum (point [I]. Y + 1); // prevents the negative effect of 0
Level [k] ++;
Modify (point [I]. Y + 1);
}
For (INT I = 0; I <= n-1; I ++)
Printf ("% d/N", level [I]);
Return 0;
}