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!
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.
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.
题意:每次看到John就知道又是关于牛的。这次也不例外,John的牛要比美。第一行输入n,n==0结束,接着输入n组数据,每头牛都有两个参数s[]和e[]。如果满足Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj我们就说i牛比j牛牛逼,最后输出有多少牛比第i个牛牛逼。
思路:和星星那个题差不多。
ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int d1[1000000],maxn,d[1000000]; //d1[]为树状数组,d[]存比当前牛强壮的牛的个数
struct node
{
int g,h,y;
}t[1000000];
int cmp(struct node a,struct node b) //排序以g从大到小排序
{
if(a.h==b.h)
return a.g<b.g;
return a.h>b.h;
}
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int res=0;
while(x>0)
{
res+=d1[x];
x-=lowbit(x);
}
return res;
}
void add(int x)
{
while(x<=maxn)
{
d1[x]++;
x+=lowbit(x);
}
}
int main()
{
int a,b,i,j,n;
while(scanf("%d",&n)&&n)
{
maxn=0;
memset(d1,0,sizeof(d1));
memset(d,0,sizeof(d));
for(i=0;i<n;i++)
{
scanf("%d%d",&t[i].g,&t[i].h);
t[i].y=i;
t[i].g++,t[i].h++; //避免0进入树状数组
if(t[i].g>maxn)
maxn=t[i].g;
}
sort(t,t+n,cmp); //排完序后就和星星那道题像了
d[t[0].y]=sum(t[0].g);
add(t[0].g);
for(i=1;i<n;i++)
{
if(t[i].g==t[i-1].g&&t[i].h==t[i-1].h) //如果当前点与前一个店相同,比他强壮的牛数就和去前面的牛一样
d[t[i