• 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 }
  • 相关阅读:
    【SQL触发器】类型 FOR 、AFTER、 Instead of到底是什么鬼
    Oracle两种临时表的创建与使用详解
    oracle 临时表(事务级、会话级)
    oracle存储过程游标的使用(批号分摊)
    delphi FastReport快速入门
    Vue 表情包输入组件的实现代码
    一个基于 JavaScript 的开源可视化图表库
    浅淡Webservice、WSDL三种服务访问的方式(附案例)
    记录一下遇到的问题 java将json数据解析为sql语句
    Oracle词汇表(事务表(transaction table)”)
  • 原文地址:https://www.cnblogs.com/robin1998/p/6511488.html
Copyright © 2020-2023  润新知