poj 2481 Cows

Problem: 2481		User: shu_dayang

Memory: 2112K		Time: 1094MS

Language: C++		Result: Accepted



#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

#define MAXN  100005

using namespace std;



struct Cow

{

    int x;

    int y;

    int order;

}a[MAXN];



bool cmp(Cow a, Cow b)

{

    if(a.y != b.y) return a.y > b.y;

    return a.x < b.x;

}



int c[MAXN],num[MAXN];

int n;



int lowbit(int x)

{

    return x & (-x);

}



void update(int x)

{

    while(x <= MAXN)

    {

        c[x]++;

        x += lowbit(x);

    }

}



int query(int x)

{

    int res = 0;

    while(x > 0)

    {

        res += c[x];

        x -= lowbit(x);

    }

    return res;

}



int main()

{

    int n;

    while( ~scanf("%d",&n) && n != 0)

    {

        for(int i = 1; i <= n; i++)

        {

            scanf("%d%d",&a[i].x,&a[i].y);

            a[i].x++;                                //防止出现0的情况而超时                                

            a[i].y++;

            a[i].order = i;

        }

        sort(a+1,a+1+n,cmp);

        memset(c,0,sizeof(c));

        memset(num,0,sizeof(num));

        update(a[1].x);

        num[1] = 0;

        for(int i = 2; i <= n; i++)

        {

            if(a[i].x == a[i-1].x && a[i].y == a[i-1].y)   //两头牛相同的情况

                num[a[i].order] = num[a[i-1].order];

            else

                num[a[i].order] = query(a[i].x);

            update(a[i].x);

        }

        for(int i = 1; i <= n; i++)

            printf("%d%c",num[i],(i==n)?'\n':' ');

    }

    return 0;

}

 

你可能感兴趣的:(poj)