HDU 1541(树状数组)

题目链接:HDU 1541

题目大意:求二维偏序.给n个星星,每个星星按y坐标从小到大,y一样x从小到大输入,然后每个星星的做下区域每包含一个星星(不包括自己),该星星就升一级.最后求等级0~n-1的星星的个数。

 

 

分析:可以排序降维(不过这题已经按y排好了),然后用树状数组求逆序对的方法就好了.

另本题需注意以下2点

  • x和y的值可以是0
  • 多组输入

 

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

#define Fi first
#define Se second
#define ll long long
#define inf 0x3f3f3f3f
#define lowbit(x) (x&-x)
#define PLL pair<ll,ll>
#define PII pair<int,int>
#define in(b) scanf("%d",&b)
#define l_inf 0x3f3f3f3f3f3f3f
#define mmin(a,b,c) min(a,min(b,c))
#define mmax(a,b,c) max(a,max(b,c))
#define debug(a) cout<<#a<<"="<<a<<endl;
#define debug2(a,b) cout<<#a<<"="<<a<<" "<<#b<<"="<<b<<endl;
using namespace std;
const int N=32010;
int s[N];
void update(int p)
{
    while(p<N)
    {
        s[p]++;
        p+=lowbit(p);
    }
}
int query(int p)
{
    int ans=0;
    while(p)
    {
        ans+=s[p];
        p-=lowbit(p);
    }
    return ans;
}
int cnt[N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int ans=0;
        memset(s,0,sizeof s);
        memset(cnt,0,sizeof cnt);
        for(int i=1;i<=n;i++)
        {
            int x,y;
            in(x);in(y);
            x++;y++;
            update(x);
            cnt[query(x)]++;
    //        ans+=query(x);
        }
        for(int i=1;i<=n;i++)
        {
            printf("%d\n",cnt[i]);
        }
    //    cout<<ans<<endl;
    }

    return 0;
}
/*
1000 1111
1010 1011
*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值