POJ 2352 - Stars


—— by A Code Rabbit


Description

在直角坐标系上有几个星星。

星星的等级等于在它左下方的星星的数量(含左方和下方)。

计算各等级的星星数。


Type

Advanced Data Structures :: Segment Tree

Advanced Data Structures :: Binary Indexed Tree


Analysis

由于题目的输入,是排序好的。

就不需要我们排序了。

否则我们要做的第一步,就是按 y 排序。

因此输入中的 y 是没有用的。


一颗星星的等级就等于在它之前,x坐标小于等于它的星星有几个。

用树状数组数组记录下 x 坐标的星星数,即可轻松解决。


要注意,数据范围 0 <= x <= 32000。

而树状数组不能有 0 下标,因此需要将所有 x 坐标 + 1。

且需要开大小为 32001 的数状数组。


Solution

// POJ 2352
// Stars
// by A Code Rabbit

#include <cstdio>
#include <cstring>

const int MAXN = 32002;

struct Bit {
    int c[MAXN], n;
    void Init(int x) { memset(c, 0, sizeof(c)); n = x; }
    void Add(int x, int y) {
        while (x <= n) { c[x] += y; x += x & -x; }
    }
    int Sum(int x) {
        int res = 0;
        while (x > 0) { res += c[x]; x -= x & -x; }
        return res;
    }
};

int n;
int x, y;
Bit bit;
int cnt[15002];

int main() {
    while (scanf("%d", &n) != EOF) {
        memset(cnt, 0, sizeof(cnt));
        bit.Init(32001);
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &x, &y);
            cnt[bit.Sum(x + 1)]++;
            bit.Add(x + 1, 1);
        }
        for (int i = 0; i < n; i++) {
            printf("%d\n", cnt[i]);
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值