HDU 1541 Stars(树状数组简单运用)

原题
根据题意 后加入的星星的纵坐标一定比前一个大或相等,所以在它之前所有横坐标小于等于它的星星一定在它的左下方。
注意数据范围!
AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[50000],c[50000],n,maxn;
ll ask(ll x)
{
    ll ans=0;
   for(;x;x-=x&-x)
     ans+=c[x];
     return ans;
}
void add(ll x,ll y)
{
    for(;x<32005;x+=x&-x)
    {
       c[x]+=y;
    }
}
int main()
{
    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        memset(c,0,sizeof(c));
    for(ll i=1;i<=n;i++)
    {
       ll x,y;
       scanf("%lld%lld",&x,&y);
       x++;
       a[ask(x)]++;
       add(x,1);
    }
    for(ll i=1;i<=n;i++)
    {
       cout<<a[i-1]<<endl;
    }
    }
return 0;
}

猜你喜欢

转载自www.cnblogs.com/Pecoz/p/12405950.html
今日推荐