Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 4786 | Accepted: 1478 |
Description
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
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
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
Hint
Source
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct t
{
int x,y,num,value;
}e[100003];
int tree[100003];
bool cmp(t a,t b)
{
if(a.y!=b.y) return a.y<b.y;
return a.x<b.x;
}
bool cmp1(t a,t b)
{
return a.num<b.num;
}
inline int Lowbit(int x)
{
return x&(-x);
}
void Update(int x)
{
for(int i=x;i<100002;i+=Lowbit(i))
tree[i]++;
}
int Getsum(int x)
{
int temp=0;
for(int i=x;i>0;i-=Lowbit(i))
temp+=tree[i];
return temp;
}
int main()
{
int n;
while(((scanf("%d",&n),n),n)!=0)
{
memset(tree,0,sizeof(tree));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&e[i].x,&e[i].y);
e[i].y=100001-e[i].y;
e[i].num=i;
e[i].value=0;
}
sort(e+1,e+1+n,cmp);
int k=0;
for(int i=1;i<=n;i++)
{
e[i].value=Getsum(e[i].x+1);
if(i>1&&e[i].x==e[i-1].x&&e[i].y==e[i-1].y)
{
k++;
e[i].value-=k;
}
else
k=0;
Update(e[i].x+1);
}
sort(e+1,e+1+n,cmp1);
for(int i=1;i<n;i++)
printf("%d ",e[i].value);
printf("%d/n",e[n].value);
}
return 0;
}