hdu 1541 Stars(树状数组)

#include <stdio.h>
#include <string.h>
#include <algorithm>

 

using namespace std;

#define MAXN 32005

int n,c[MAXN],num[MAXN],inx[MAXN],sorted[MAXN],xhash[MAXN];

inline int lowbit(int x)
{
    return x&(-x);
}
inline void update(int x,int delta)
{
    while(x<=n)
    {
        c[x]+=delta;
        x+=lowbit(x);
    }
}
inline int getSum(int x)
{
    int s=0;
    while(x>0)
    {
        s+=c[x];
        x-=lowbit(x);
    }
    return s;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("tdata.txt","r",stdin);
#endif
    int i,idx,x,y;
    while(scanf("%d",&n)!=EOF)
    {
        memset(c,0,sizeof(*c)*(n+1));
        memset(num,0,sizeof(*num)*(n+1));
        idx = 1;
        for(i=0; i<n; i++)
        {
            scanf("%d %d",&inx[i],&y);
            sorted[i]=inx[i];
        }
        sort(sorted,sorted+n);
        xhash[ sorted[0] ] = idx++;
        for(i=1; i<n; i++)
        {
            if(sorted[i] != sorted[i-1])
            {
                xhash[ sorted[i] ] = idx++;
            }
        }
        for(i=0; i<n; i++)
        {
            x = xhash[ inx[i] ];
            num[getSum(x)]++;
            update(x,1);
        }
        for(i=0; i<n; i++) printf("%d\n",num[i]);
    }
    return 0;
}

posted @ 2010-09-06 23:56  菜到不得鸟  阅读(262)  评论(0)    收藏  举报