• POJ-2352 Stars【树状数组】


    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


    树状数组的线段树都可以做,每输入一个x,统计小于x的数量,即前缀和。


    Code:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 const int maxn = 32000 + 5;
     7 int C[maxn], cnt[maxn], n;
     8 
     9 int lowbit(int x) {return x & -x;}
    10 
    11 int sum(int x) {
    12     int ret = 0;
    13     while (x) {
    14         ret += C[x]; x -= lowbit(x);
    15     }
    16     return ret;
    17 }
    18 
    19 void add(int x, int d) {
    20     while (x <= maxn) {
    21         C[x] += d; x += lowbit(x);
    22     }
    23 }
    24 
    25 int main() {
    26     int x, y;
    27     while(~scanf("%d", &n)) {
    28         memset(C, 0, sizeof(C));
    29         memset(cnt, 0, sizeof(cnt));
    30         for (int i = 0; i < n; ++i) {
    31             scanf("%d%d", &x, &y);
    32             ++x;
    33             ++cnt[sum(x)];
    34             add(x, 1);
    35         }
    36         for (int i = 0; i < n; ++i)
    37             printf("%d
    ", cnt[i]);
    38     }
    39 
    40     return 0;
    41 }
  • 相关阅读:
    软件实施工程师是一个什么样的工作?他的具体工作内容是什么?发展前景怎样?
    做金融(基金、证券)方面的软件实施工程师有没有发展前途?职业发展空间如何。
    做软件实施工程师的一点建议
    系统实施工程师主要工作职则
    软件实施工程师
    UE编辑器编译和运行java设置
    猜数字
    猜数字
    Problem G
    Problem G
  • 原文地址:https://www.cnblogs.com/robin1998/p/6511488.html
Copyright © 2020-2023  润新知