POJ 2481 Cows

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


Cows

Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 16528
Accepted: 5521

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.

Source

POJ Contest,Author:Mathematica@ZSU

思路:这道题和POJ 2352 Stars类似,只不过这题要先按照需求排一个序,求和方向也恰恰相反。可以选择看一下我的POJ 2352 Stars的博文:http://blog.csdn.net/silenceneo/article/details/47414403。这题有两个写法,线段树和树状数组。思路是一样的。下面讲一下我的一个学长对这题的解答。我就是按照他的思路各写了一个版本。

/* 对于这种二维属性的区间求个数问题,如果其属性满足简单的大小关系,我们可以先对一维属性进行排序,然后按照该顺序,在另一维上进行计数。
就这题而言,如果我们先按S属性的小大顺序排序,那么后插入线段树的牛只受到先插入牛的影响,这是我们期望的。
然后我们分别用叶子代表E的值,维护区间牛的数量,那我们每次在[Ei,10^5]范围内查找当前牛的个数即可。
两个注意点:
1. 在按S属性排序的时候,如果两者S值相等,那我们优先把E值大的放在前面,因为我们总是希望后插入的只受先插入的影响。
2. 在查询区间和的时候需要判断在它之前是否有和它属性一模一样的牛,因为它并不受和它属性一样的牛的影响,但是[Ei,10^5]统计的时候会把它算进去。解决方法是直接把之前的答案复制给他即可,能理解吗? */


附上AC代码:

// 树状数组版本,代码简洁,效率比线段树稍好一些

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 100005;
struct node{
	int x, y, id;
	bool operator < (const node & p) const {
		if (x == p.x)
			return y > p.y;
		return x < p.x;
	}
} cow[maxn];
int tree[maxn], ans[maxn];
int n;

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

void add(int p){
	while (p > 0){
		++tree[p];
		p -= lowbit(p);
	}
}

int sum(int p){
	int ans = 0;
	while (p < maxn){
		ans += tree[p];
		p += lowbit(p);
	}
	return ans;
}

int main(){
	while (~scanf("%d", &n) && n){
		for (int i=0; i<n; ++i){
			scanf("%d%d", &cow[i].x, &cow[i].y);
			cow[i].id = i;
			++cow[i].x, ++cow[i].y;
		}
		sort(cow, cow+n);
		memset(tree, 0, sizeof(tree));
		for (int i=0; i<n; ++i){
			if (i>0 && cow[i].x==cow[i-1].x && cow[i].y==cow[i-1].y)
				ans[cow[i].id] = ans[cow[i-1].id];
			else
				ans[cow[i].id] = sum(cow[i].y);
			add(cow[i].y);
		}
		for (int i=0; i<n; ++i)
			printf("%d%c", ans[i], i!=n-1 ? ' ' : '\n');
	}
	return 0;
}

// 线段树版本,代码比较冗长

#include <cstdio>
#include <algorithm>
#define lrt rt<<1
#define rrt rt<<1|1
#define lson l, m, lrt
#define rson m+1, r, rrt
using namespace std;
const int maxn = 100005;
struct node{
	int x, y, id;
	bool operator < (const node & p) const {
		if (x == p.x)
			return y > p.y;
		return x < p.x;
	}
} cow[maxn];
int sumv[maxn<<2], ans[maxn];
int n;

void push_up(int rt){
	sumv[rt] = sumv[lrt]+sumv[rrt];
}

void build(int l, int r, int rt){
	sumv[rt] = 0;
	if (l == r)
		return ;
	int m = (l+r)>>1;
	build(lson);
	build(rson);
	push_up(rt);
}

void update(int p, int l, int r, int rt){
	if (l == r){
		++sumv[rt];
		return ;
	}
	int m = (l+r)>>1;
	if (p <= m)
		update(p, lson);
	else
		update(p, rson);
	push_up(rt);
}

int query(int ql, int qr, int l, int r, int rt){
	if (ql<=l && r<=qr)
		return sumv[rt];
	int m = (l+r)>>1;
	int sumr = 0;
	if (ql <= m)
		sumr += query(ql, qr, lson);
	if (qr > m)
		sumr += query(ql, qr, rson);
	return sumr;
}

int main(){
	while (~scanf("%d", &n) && n){
		build(0, maxn-1, 1);
		for (int i=0; i<n; ++i){
			scanf("%d%d", &cow[i].x, &cow[i].y);
			cow[i].id = i;
		}
		sort(cow, cow+n);
		for (int i=0; i<n; ++i){
			// 后面区间与前面完全相同,它的答案与前面一样,
			// 这样做是为了避免求和时,将与它相同的也求进去了
			if (i>0 && cow[i].x==cow[i-1].x && cow[i].y==cow[i-1].y)
				ans[cow[i].id] = ans[cow[i-1].id];
			else
				ans[cow[i].id] = query(cow[i].y, maxn-1, 0, maxn-1, 1);
			update(cow[i].y, 0, maxn-1, 1);
		}
		for (int i=0; i<n; ++i)
			printf("%d%c", ans[i], i!=n-1 ? ' ' : '\n');
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值