Ural - 1028. Stars(二维树状数组)

博客介绍了如何利用二维树状数组解决Ural 1028问题,该问题要求计算每个点的level(即坐标小于它的点数量),并给出了解决方案和AC代码。
摘要由CSDN通过智能技术生成

题目链接;http://acm.timus.ru/problem.aspx?space=1&num=1028

题意:给定N个点,定义一个点的level为x,y坐标都比他小的点的数量,求level为0,1……n-1的点的数量。

题解:按x坐标排序,然后对y坐标进行树状数组统计即可。需要离散化。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
#define _for(i,a,b) for(int i=a;i<=b;i++)
const int maxn = 33000;
struct node
{
    int x,y;
    bool operator<(const node& a)const
    {
        if(x==a.x)return y<a.y;
        return x<a.x;
    }
}a[maxn];
int n,c[maxn],ans[maxn],b[maxn];
int lowbit(int x)
{
    return x&-x;
}
void insert(int x)
{
    while(x<=n)
    {
        c[x]++;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int now = 0;
    while(x>0)
    {
        now+=c[x];
        x-=lowbit(x);
    }
    return now;
}
int main(int argc, char const *argv[])
{
    scanf("%d",&n);
    _for(i,1,n)
    {
        scanf("%d%d",&a[i].x,&a[i].y);
        b[i]=a[i].y;
    }
    sort(b+1,b+1+n);
    int cnt = unique(b+1,b+1+n)-b;
    sort(a+1,a+1+n);
    _for(i,1,n)
    {
        int k = lower_bound(b+1,b+1+cnt,a[i].y)-b;
        int now = sum(k);
        ans[now]++;
        insert(k);
    }
    _for(i,0,n-1)printf("%d\n",ans[i]);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值