1. 程式人生 > >POJ 2352 Stars(樹狀數組)

POJ 2352 Stars(樹狀數組)

src for cst mem scribe table desc integer scan

Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 51114 Accepted: 22049

Description

Astronomers 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. Astronomers 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 should 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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 天文學家經常檢查恒星地圖,其中恒星由平面上的點表示,每顆恒星都有笛卡爾坐標。讓恒星的水平是恒星的數量,而恒星並不高於恒星的右邊。天文學家想知道恒星的分布。
例如,查看上圖中顯示的地圖。星號5的水平等於3(它由三顆星形成,數字為1,2和4)。
而且,編號為2和4的恒星的等級為1.在該地圖上,等級0中只有一顆恒星,等級1的兩顆恒星,等級2的一顆恒星和等級3的一顆恒星。

你要編寫一個程序來計算給定地圖上每個級別星星的數量。

輸入文件的第一行包含許多恒星N(1 <= N <= 15000)。以下N行描述了恒星的坐標(由空格隔開的兩行整數X和Y,0 <= X,Y <= 32000)。飛機一點上只能有一顆星。星號按照Y坐標的升序排列。 Y坐標相等的星號按照X坐標的升序排列。
輸出應該包含N行,每行一個數字。第一行包含0級星的數量,第二行包含第一級星的數量等,最後一行包含第N-1級的星的數量
 1 //2018年2月18日22:05:32
 2 #include <iostream>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 const int maxn = 15001;
 7 const int maxx = 32001;
 8 int n, x, y;
 9 int c[maxx];
10 int cnt[maxn];
11 
12 inline int lowbit(int x){
13     return x & (-x);
14 }
15 
16 void add(int x, int k){
17     for(int i=x; i<=maxx; i+=lowbit(i)) c[i] += k;
18 }
19 int sum(int x){
20     int res = 0;
21     for(int i=x; i; i-=lowbit(i)) res += c[i];
22     return res;
23 }
24 
25 int main(){
26     scanf("%d", &n);
27     for(int i=1;i<=n;i++){
28         scanf("%d%d", &x, &y);
29         int tmp = sum(x+1);
30         cnt[tmp]++;
31         add(x+1, 1);
32     }
33     for(int i=0; i<n; i++)
34         printf("%d\n", cnt[i]);
35 
36     return 0;
37 }

POJ 2352 Stars(樹狀數組)