poj2481 Cows


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.


分析:
     本题和poj2352 Stars有很大的相似之处。都是用树状数组维护坐标。Stars是给定一系列的点(并按照纵坐标从小到大依次排好序),求每一个点左下角有多少个点。本题是给出无序的坐标然后求每一个点的左上角有多少个点。由于是左上角,可以先按照纵坐标的从大到小排序,然后依次求每个点左上角的点数目,更新每一个点。(利用每个点之前的点一定比当前点高)。



代码:

#include<iostream>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
#define lowbit(i) (i&(-i))
using namespace std;
const int maxn=1e5+5;

int n,bit[maxn],ans[maxn];
struct node{
	int x,y,id;			//(x,y)表示每一个点的坐标,id排序时用来记录每个点原来顺序 
}a[maxn];

bool cmp(node a,node b){
	return (a.y==b.y?a.x<b.x:a.y>b.y);   //按照纵坐标从高到低排序,相同时按照横坐标由小到大排序 
}

void add(int i,int x){
	while(i<=maxn){
		bit[i]+=x;
		i+=lowbit(i);
	}
}

int query(int i){
	int sum=0;
	while(i>0){
		sum+=bit[i];
		i-=lowbit(i);
	}
	return sum;
}

int main(){
	while(~scanf("%d",&n) && n){
		memset(bit,0,sizeof(bit));
		memset(ans,0,sizeof(ans));
		for(int i=0;i<n;i++){
			scanf("%d%d",&a[i].x,&a[i].y);	
			a[i].id=i;
			a[i].x++;a[i].y++;		//树状数组下标从1开始,输入的点横纵坐标都++ 
		}
		sort(a,a+n,cmp);
		ans[a[0].id]=0;		
		add(a[0].x,1);
		for(int i=1;i<n;i++){
			if(a[i].x==a[i-1].x && a[i].y==a[i-1].y)	//若两个连续点重复,则当前点的值等于前一个点的值 
				ans[a[i].id]=ans[a[i-1].id];		
			else
				ans[a[i].id]=query(a[i].x);		//否则看当前点之前(左边)的点(都比当前点高)的和(个数) 
			add(a[i].x,1);			//更新当前点(横坐标x,个数1) 
		}
		for(int i=0;i<n;i++){                   //按输入顺序输出每个点的值
			if(i) printf(" ");
			printf("%d",ans[i]);
		}
		cout<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值