首页 > 代码库 > poj 2481

poj 2481

Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13128 Accepted: 4360

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: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowiis stronger than cowj

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 <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) 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 cowi

Sample Input

31 20 33 40

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.

Source

POJ Contest,Author:Mathematica@ZSU
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<cstdlib>#include<queue>#include<vector>using namespace std;#define N 1000010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int n,num[N*4],sum[N*4];struct line{    int x,y,nt;}e[N];bool cmp(line a,line b){    if(a.x==b.x)            return a.y>b.y;    return a.x<b.x;}int update(int l,int r,int rt,int L,int R){    if(L<=l&&r<=R)        return sum[rt];    int m=(l+r)>>1;    int ret=0;    if(L<=m)        ret+=update(lson,L,R);    if(R>m)        ret+=update(rson,L,R);    return ret;}void push(int l,int r,int rt,int x){    sum[rt]++;    if(l==r)        return ;    int m=(l+r)>>1;    if(m>=x) push(lson,x);    else push(rson,x);}int main(){    while(scanf("%d",&n),n)    {        memset(sum,0,sizeof(sum));        memset(num,0,sizeof(num));        for(int i=1;i<=n;i++)        {            scanf("%d%d",&e[i].x,&e[i].y);            e[i].nt=i;        }        sort(e+1,e+1+n,cmp);        for(int i=1;i<=n;i++)        {            if(i!=1&&e[i].x==e[i-1].x&&e[i].y==e[i-1].y)                num[e[i].nt]=num[e[i-1].nt];            else num[e[i].nt]=update(1,N,1,e[i].y,N);            push(1,N,1,e[i].y);        }        for(int i=1;i<=n-1;i++)            printf("%d ",num[i]);        printf("%d\n",num[n]);    }    return 0;}

  

poj 2481