POJ 2352 Stars 树状数组

本文介绍了一道经典的树状数组应用题目,通过解析题目需求和实现思路,使用树状数组来快速计算星星的水平,即左下方星星的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:http://poj.org/problem?id=2352

题意:给出一些星星的坐标,按y坐标升序给出,若y坐标相等按照x升序给出。每个星星有一个水平,就是这颗星星左下方的星星数目。输出所有水平的个数

思路:因为是按y坐标升序给出的,所以当前星星的左下方星星一定给出过了,忽略y坐标,也是小于等于当前星星x坐标的星星个数,可以用树状数组轻松求出

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

typedef long long ll;
const int N = 50010;
int bit[N], level[N], n;
void add(int i, int x)
{
    while(i <= 50000)
        bit[i] += x, i += i & -i;
}
int sum(int i)
{
    int s = 0;
    while(i > 0)
        s += bit[i], i -= i & -i;
    return s;
}
int main()
{
    int a, b;
    while(~ scanf("%d", &n))
    {
        memset(bit, 0, sizeof bit);
        memset(level, 0, sizeof level);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &a, &b);
            a++, b++;
            add(a, 1);
            int res = sum(a) - 1; //因为先更新后查询,所以其实把当前星星也算了进去,所以减去1
            level[res]++;
        }
        for(int i = 0; i < n; i++)
            printf("%d\n", level[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值