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 cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Input
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
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
/*先按e值从大到小,相同就x从小到大
排好后由于前面的E值已经比当前点大,只要找出S值比当前小的就满足
注意坐标+1,处理相同的,s,e
建模是关键*/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#define MAX 100005
using namespace std;
struct node{
int e,s;
int id;
}cow[MAX];
int N;
int c[MAX];
int a[MAX];
int cmp(const node &a,const node &b)
{
if(a.e==b.e)
return b.s > a.s;
else
return a.e > b.e;
}
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int sum=;
while(x>)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
void updata(int x,int d)
{
while(x<=MAX)
{
c[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int i;
while(scanf("%d",&N)!=EOF)
{
if(!N)
break;
memset(c,,sizeof(c));
memset(a,,sizeof(a));
for(int i=;i<N;i++)
{
scanf("%d %d",&cow[i].s,&cow[i].e);
cow[i].s++;
cow[i].e++;
cow[i].id=i;
}
sort(cow,cow+N,cmp);
a[cow[].id]=;
updata(cow[].s,);
for( i=;i<N;i++)
{
if(cow[i].s==cow[i-].s&&cow[i].e==cow[i-].e)
a[cow[i].id]=a[cow[i-].id];
else
a[cow[i].id]=getsum(cow[i].s);
updata(cow[i].s,);
}
printf("%d",a[]);
for(int i=;i<N;i++)
printf(" %d",a[i]);
printf("\n");
}
return ;
}