码迷,mamicode.com
首页 > 编程语言 > 详细

POJ 2352 Stars(树状数组)

时间:2018-02-18 23:03:41      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:min   表示   you   分享   查看   平面   nes   sample   而且   

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(树状数组)

标签:min   表示   you   分享   查看   平面   nes   sample   而且   

原文地址:https://www.cnblogs.com/sineagle/p/8453323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!