POJ 2481 Cows(树状数组)

题目来源:http://poj.org/problem?id=2481

问题描述

Cows

Time Limit: 3000MS

 

Memory Limit: 65536K

Total Submissions: 22461

 

Accepted: 7586

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

------------------------------------------------------------

题意

给定n个奶牛和它们吃草的区间,如果奶牛A的区间严格被包含于奶牛B的区间,则称奶牛B“强壮于”奶牛A。对于每头奶牛,计算输出比它强壮的奶牛数量。

------------------------------------------------------------

思路

树状数组。在HDU 1541 Stars(树状数组)的基础上多加了两次排序的过程,核心算法(“求和-更新”两部曲,代码如下)一致。略有不同的是这里区间完全相同的奶牛互相不比对方强壮,所以对区间完全相同的情况不用特殊处理,而Stars那题输入保证了不出现坐标完全相同的情况。

if (i>0 && cow[i].s==cow[i-1].s && cow[i].e==cow[i-1].e)
{
	myans[i].res = myans[i-1].res;
}
else
{
	myans[i].res = getsum(cow[i].s);
}
modify(cow[i].s, 1);
myans[i].ind = cow[i].ind;

首先将输入的奶牛区间按终点从大到小排序,这样比某个奶牛强壮的奶牛一定在这头奶牛前面,如此一来,树状数组的“求和-更新”两部曲就可以使用了。最后输出的时候再将结果数组按原来奶牛的编号排序输出。

------------------------------------------------------------

代码

#include<cstdio>
#include<algorithm>
#include<cstring>

const int NMAX = 100005;
const int XMAX = 100000;

int c[NMAX] = {};			// 树状数组 

// 树状数组3函数
// lowbit: k=n的二进制末尾0的个数,求n的2^k
int lowbit (int n)
{
	return n & (-n);	
}

// modify: 修改树状数组c中索引为x的元素的值,增加d 
void modify (int x, int d) 
{
	if (x < 1)
	{
		return;
	}
	else
	{
		while (x <= XMAX+1)
		{
			c[x] += d;
			x += lowbit(x);
		} 
	}
}

// getsum: 求sum(n) = x坐标等于n的星星数量 
int getsum(int n)
{
	int ans = 0;
	while (n > 0)
	{
		ans += c[n];
		n -= lowbit(n);
	}
	return ans;
} 

struct node {
	int s, e, ind;
	
	node (void){}
	
	node (int ss, int ee, int ii): s(ss), e(ee), ind(ii) {}
	
	bool operator< (const node & bnode) const
	{
		if (e > bnode.e)
		{
			return true;
		}
		else if (e == bnode.e && s < bnode.s)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

struct ans {
	int res, ind;		// 最后结果需要按奶牛的序号排序输出 
	
	bool operator< (const ans & bans) const
	{
		return ind < bans.ind;
	}
};

node cow[NMAX] = {};
ans myans[NMAX] = {};

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("poj2481.txt", "r", stdin);
	#endif
	int n,i,s,e;
	while (scanf("%d", &n))
	{
		if (n==0)
		{
			break;
		}
		memset(c, 0, sizeof(c));
		for (i=0; i<n; i++)
		{
			scanf("%d%d", &s, &e);
			s++;
			cow[i] = node(s,e,i);
		}
		std::sort(cow, cow+n);
		for (i=0; i<n; i++)
		{
			if (i>0 && cow[i].s==cow[i-1].s && cow[i].e==cow[i-1].e)
			{
				myans[i].res = myans[i-1].res;
			}
			else
			{
				myans[i].res = getsum(cow[i].s);
			}
			modify(cow[i].s, 1);
			myans[i].ind = cow[i].ind;
		}
		std::sort(myans, myans+n);
		for (i=0; i<n; i++)
		{
			printf("%d ", myans[i]);
		}
		printf("\n");
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值