poj2481 Cows

原创 2015年11月21日 17:02:37

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Cows POJ - 2481

题目链接:点我 Farmer John's cows have discovered that the clover growing along the ridge of the hill (w...
  • cccruel
  • cccruel
  • 2017年07月21日 13:35
  • 64

poj 2481 Cows

我表示对树状数组很没感觉。也许是刚学到缘故吧。很可能随着时候的增长,知识的积累,经验的积累,会很好的掌握的。 我想说的 此题,跟poj 2352没有多大的区别。但是,我没有做好这个题目,关键还是对 l...

poj 2481 cows 树状数组

poj 2481 cows 树状数组
  • k_l_c_
  • k_l_c_
  • 2016年07月08日 14:38
  • 171

POJ 2481 Cows(树状数组)

题目链接: POJ 2481 Cows 题意: 有n个区间,定义区间[a,b]大于区间[c,d]的条件为:a=d且b-a>d-c。 对每个区间输出大于这个n个区间中大于这个区间的区间个数。 ...
  • Ramay7
  • Ramay7
  • 2016年03月30日 13:35
  • 186

POJ - 2481 - Cows (树状数组+排序!!)

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13304   Accepted...

poj 2481 Cows 线段树

题目 Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 19518 Accepted: 6...

POJ--2481--Cows【线段树】

链接 :http://poj.org/problem?id=2481 题意:一条直线上长满了三叶草,有n头牛,每头牛都有喜欢的一段三叶草区间 [ si , ei ] ,如果一头牛喜欢的区间包含了另一...
  • zzzz40
  • zzzz40
  • 2014年07月25日 22:19
  • 900

poj 2481 Cows(哇咔咔,树状数组^ ^)

这题今天上午软件工程课想了会儿,觉得没错,中午放学回来敲,群上在说群赛神马的,明晚群赛^ ^ 等敲完该上课了,= =,测下样例,不对,无语了。 下午算法课,课上想了会儿这个题,上午想的是,把 s 排序...

POJ 2481 Cows 线段树

POJ 2481 Cows
  • NNDXNM
  • NNDXNM
  • 2015年04月08日 20:50
  • 299

POJ2481 Cows 树状数组

这题的意思是求区间覆盖问题(Si
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2481 Cows
举报原因:
原因补充:

(最多只允许输入30个字)