poj2481--Cows

Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12257   Accepted: 4050

Description

Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. 

Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E]. 

But some cows are strong and some are weak. Given two cows: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j

For each cow, how many cows are stronger than her? Farmer John needs your help!

Input

The input contains multiple test cases. 
For each test case, the first line is an integer N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge. 

The end of the input contains a single 0.

Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cow i

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU
题意:n头牛,每头牛有两个值s和e,如果i牛强于j牛,那么si <= sj && ej <= ei && ei-si >= ej-sj,问对于每头牛来说,有多少比他更强壮

要对每一头牛都求出一个值,使用树状数组,树状数组可以统计由1到当前的总和,按照题目要求,只要 si <= sj && ej <= ei,那么 ei-si >= ej-sj一定符合,所以可以用e由大到小排列,e相同时s有小到大,那么如果求第i头牛,比他强壮的一定都在<1,i>之中

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
    int id , s , e ;
} p[100010];
int a[100010] , c[100010] ;
int lowbit(int x)
{
    return x & -x ;
}
int sum(int i)
{
    int num = 0 ;
    while( i )
    {
        num += c[i] ;
        i -= lowbit(i) ;
    }
    return num ;
}
void add(int i,int m)
{
    while( i <= m )
    {
        c[i]++ ;
        i += lowbit(i);
    }
}
bool cmp(node a,node b)
{
    return a.e > b.e || ( a.e == b.e && a.s < b.s ) ;
}
int main()
{
    int i , s , e , n , m , num ;
    while(scanf("%d", &n) && n)
    {
        memset(a,0,sizeof(s));
        memset(c,0,sizeof(a));
        m = -1 ;
        for(i = 0 ; i < n ; i++)
        {
            scanf("%d %d", &s, &e);
            s++ ;e++ ;
            p[i].id = i ;
            p[i].s = s ;
            p[i].e = e ;
            if( m < s )
                m = s ;
        }
        sort(p,p+n,cmp) ;
        s = -1 ; e = -1 ;
        num = 0 ;
        for(i = 0 ; i < n ; i++)
        {
            if( s == p[i].s && e == p[i].e )
                num++ ;
            else
            {
                num = 0 ;
                s = p[i].s ; e = p[i].e ;
            }
            a[ p[i].id ] = sum(s) - num ;
            add(s,m) ;
        }
        for(i = 0 ; i < n ; i++)
        {
            if(i == n-1)
                printf("%d\n", a[i]);
            else
                printf("%d ", a[i]);
        }
    }
    return 0;
}


你可能感兴趣的:(poj2481--Cows)