poj-2481-Cows(排序树状数组+逆序数)

题目链接:http://poj.org/problem?id=2481

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 cowi is 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

3
1 2
0 3
3 4
0

Sample Output

1 0 0

Hint

Huge input and output,scanf and printf is recommended.

题目大意:n头牛,每头牛都有一个领地(s,e),有些牛的领地可能重合,覆盖在一起的领地,有大有小,如果满足条件:

 Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj,

大意为一头牛的领地包含另一头牛的领地,则领地大的那个牛更强,现在给出n个牛的领地,对于每个牛,求出比他强壮的牛的个数,顺序输出。

类似于poj2352||poj3067

先对牛的结尾降序排序,这样,对于后面的牛来说,只要后面的领地肯定被包括包括,只用找前面的被包括的领地的个数就行了,然后按照num储存到res(答案)数组中,顺序输出即可;

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 1000000007
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
struct node{
	int num;//记录编号 
	int s,e;
}range[100100];
int tree[100100];
int res[100100];
int n;
bool cmp(node a,node b)
{
	if(a.e==b.e)
		return a.s<b.s;
	return a.e>b.e;
}

int lowbit(int i)
{
	return i&(-i);
}

void updata(int i)
{
	while(i<=n)
	{
		tree[i]++;
		i=i+lowbit(i);
	}
}

int Query(int i)
{
	int res=0;
	while(i>0)
	{
		res=res+tree[i];
		i=i-lowbit(i);
	}
	return res;
}

bool cmp_num(node a,node b)
{
	return a.num<b.num;
}

int main()
{
	while(scanf("%d",&n)&&n!=0)
	{
		clean(tree,0);
		clean(res,0);
		clean(range,0);
		for(int i=1;i<=n;++i)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			range[i].num=i;
			range[i].e=++b;
			range[i].s=++a;
		}
		sort(range+1,range+n+1,cmp);
		for(int i=1;i<=n;++i)
		{
//			updata(range[i].s);//先加就是小于等于 要减去一个1(它本身) 
			if(range[i].e==range[i-1].e&&range[i].s==range[i-1].s)
				res[range[i].num]=res[range[i-1].num];
			else
				res[range[i].num]=Query(range[i].s);
			updata(range[i].s);//后加是小于 
//			cout<<res[range[i].num]<<" ";
		}
		printf("%d",res[1]);
		for(int i=2;i<=n;++i)
			printf(" %d",res[i]);
		printf("\n");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81233988
今日推荐