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.
题解:有n头牛,每头牛对应一个区间[Si,Ei],如果牛j 的区间是牛i 的区间的真子集(即Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj),那么就说牛i 比牛j 强壮。要你依次输出比第i头牛强壮的牛数目。如果将所有牛的E区间按从大到小排序(如果E相同,则S小的排在前面)的话,那当前读取到第i个牛的Si和Ei,那么之前(假设任意牛的区间不会完全相同)的牛的Sj(j<=i-1)<=Si的这些牛就都比i号牛强壮了。可以理解为之前那道star的题,固定一个维度,剩下的就是看剩下的那一个维度了。
注意:这里有可能有两个区间一模一样的,那么就直接赋值即可了
代码如下:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 32007
#define N 200005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
using namespace std;
typedef long long ll;
int c[N];
int n,max1;
struct node
{
int l,r,id;
} a[N];
int cmp(node a,node b)
{
if(a.r!=b.r)
return a.r>b.r;
return a.l<b.l;
}
void add(int k,int d)
{
while(k<=max1+1)
{
c[k]+=d;
k+=lowbit(k);
}
}
int sum(int k)
{
int ret=0;
while(k>0)
{
ret+=c[k];
k-=lowbit(k);
}
return ret;
}
int ans[N];
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
memset(c,0,sizeof(c));
memset(ans,0,sizeof(ans));
max1=-1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].id=i;
max1=max(max1,a[i].r);
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
{
if(a[i].l==a[i-1].l&&a[i].r==a[i-1].r)
{
ans[a[i].id]=ans[a[i-1].id];
}
else
{
ans[a[i].id]=sum(a[i].l+1);
}
add(a[i].l+1,1);
}
for(int i=1; i<=n; i++)
{
printf("%d",ans[i]);
if(i!=n)
printf(" ");
}
printf("\n");
}
return 0;
}