POJ 2352-Stars(树状数组-星系)

落日映苍穹つ 2022-06-17 03:28 335阅读 0赞

Stars














Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 46418   Accepted: 20020

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.
2352_1.jpg
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

  1. 5
  2. 1 1
  3. 5 1
  4. 7 1
  5. 3 3
  6. 5 5

Sample Output

  1. 1
  2. 2
  3. 1
  4. 1
  5. 0

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

Source

Ural Collegiate Programming Contest 1999

题目意思:

给出N个星星的坐标,在45°方向上能连成一条线的星星为一个星系的话,如果有M颗星星连成一线则最右上方星星的level是M-1。

例如图中level=0的星星是1,level=1的星星是2和4,level=2的星星是3,level=3的星星是5。

解题思路:

因为y坐标顺序给定,所以按输入处理x坐标,每次计算并记录其左边的星星的个数,然后对其右边的所有点更新增一。

注意x可能为0,而树状数组下标从1 开始。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<iomanip>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<cstring>
  7. #include<map>
  8. #include<algorithm>
  9. #include<vector>
  10. #include<queue>
  11. using namespace std;
  12. #define INF 0xfffffff
  13. #define MAXN 32005
  14. int n;
  15. int c[MAXN],cnt[MAXN];
  16. int lowbit(int x)
  17. {
  18. return x&(-x);
  19. }
  20. void update(int i,int x)
  21. {
  22. while(i<MAXN)
  23. {
  24. c[i]+=x;
  25. i+=lowbit(i);
  26. }
  27. }
  28. int sum(int x)
  29. {
  30. int i,res=0;//res是权值和
  31. for(i=x; i>0; i-=lowbit(i))
  32. res+=c[i];
  33. return res;
  34. }
  35. int main()
  36. {
  37. #ifdef ONLINE_JUDGE
  38. #else
  39. freopen("G:/cbx/read.txt","r",stdin);
  40. //freopen("G:/cbx/out.txt","w",stdout);
  41. #endif
  42. while(~scanf("%d",&n))
  43. {
  44. memset(c,0,sizeof(c));
  45. memset(cnt,0,sizeof(cnt));
  46. for(int i=0; i<n; i++)
  47. {
  48. int x,y;
  49. scanf("%d%d",&x,&y);
  50. ++x;//避免横坐标为0的情况
  51. int t=sum(x);
  52. ++cnt[t];
  53. update(x,1);//c[x]之后的全部+1
  54. }
  55. for(int i=0; i<n; i++)//输出
  56. printf("%d\n",cnt[i]);
  57. }
  58. return 0;
  59. }

发表评论

表情:
评论列表 (有 0 条评论,335人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ 2352 stars

    树状数组,给的数据已按以Y为主序,x为次序排列,因此只需要按所给顺序,统计其前面有几个x小于等于其本身,该值就是该点的阶数 include <cs

    相关 poj2352_II和III

    这个题的第二个版本: 同2299\_II,也是压缩到1--n范围内,找出已知序列的相对顺序,然后构建树状数组。这里也需要一个辅助的数组。 代码: ![Contracted