PKU 2481 COWS 排序 + 树状数组

排好序后就跟stars那题一样了
一次遍历排序后的数组,由于比当前遍历元素strong的区间只可能存在于已经遍历过的元素中;所以我们可以放心大胆的直接对树状数组求和

然后向后更新树状数组中统治x的点上的值,因为那些位置都多了一个比  以他们为左端点的区间  强壮  的区间;

#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 100010;
int c[maxn],result[maxn];

typedef struct
{
	int e, s, ord;
} node;

node nd[maxn];

bool cmp(node a , node b)
{
	/*if(a.e > b.e)
		return true;
	if(a.e == b.e)
		if(a.s < b.s)
			return true;
	return false;*/
	if(a.e == b.e)
		return a.s < b.s;
	return a.e > b.e;
}
int Lowbit(int x)  
{  
	return x & (-x);  
}  

void Update(int x) 
{
	int i;
	for(i = x; i < maxn; i += Lowbit(i))
	{
		c[i]++;
	}
}
int Getsum(int x)  
{  
	int sum = 0;  
	int i;
	for( i = x; i > 0; i -= Lowbit(i) )  
	{  		
		sum += c[i];
	}  
	return sum;  
}  

int main()
{
	int n;
	while(scanf("%d", &n), n)
	{
		memset(c, 0, sizeof(c)); //少写了这句。wa了半天才检查出来。
		memset(result, 0, sizeof(result));
		int x, y;
		for(int i = 1; i <= n; i++)
		{
			scanf("%d %d", &x, &y);
			nd[i].s = x + 1;
			nd[i].e = y + 1; 
			nd[i].ord = i;
		}
		sort(nd+1, nd+n+1, cmp);

		result[nd[1].ord] = Getsum(nd[1].s);
		Update(nd[1].s);
		for(int i = 2; i <= n; i ++)
		{
			if(nd[i].s == nd[i-1].s && nd[i].e == nd[i-1].e)
				result[nd[i].ord] = result[nd[i-1].ord];
			else
				result[nd[i].ord] = Getsum(nd[i].s);
			Update(nd[i].s);
		}
		for(int i = 1; i < n; i++)
			printf("%d ", result[i]);
		printf("%d\n", result[n]);
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值