poj 2481 Cows

题目大意:

        给定你一些区间,去判断每个区间是其他区间真子集的个数。

分析:

        本题与Stars有点类似。首先对E从大到小排序,如果Ei  = Ej 则让S从小到大排序,然后就是stars类型。就是求cow[i].S前比他小的个数。

 

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

using namespace std;

const int MAXN = 100010;

int c[MAXN];

struct Node
{
	int S, E;
	int id;
}cow[MAXN];

bool cmp(Node a, Node b)
{
	if(a.E == b.E)
		return a.S < b.S;
	return a.E > b.E;
}

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

void UFset(int pos, int data)
{
	while(pos < MAXN)
	{
		c[pos] += data;
		pos += lowbit(pos);
	}
}

int Querry( int pos )
{
	int sum = 0;
	while(pos > 0)
	{
		sum += c[pos];
		pos -= lowbit(pos);
	}
	return sum;
}

int main()
{
	int n, i;
	int cnt[MAXN];
	while(scanf("%d", &n) && n)
	{
		for(i = 1; i <= n; ++i)
		{
			scanf("%d %d", &cow[i].S, &cow[i].E);
			cow[i].id = i;
		}
		memset(c, 0, sizeof(c));
		memset(cnt, 0, sizeof(cnt));
		sort(cow+1, cow+1+n, cmp);
		for(i = 1; i <= n; ++i)
		{
			if( (i-1) && (cow[i].E == cow[i-1].E) && (cow[i].S == cow[i-1].S))
				cnt[cow[i].id] = cnt[cow[i-1].id];
			else
				cnt[cow[i].id] = Querry(cow[i].S+1);//由于S是从零开始的,而pos是大于0的,因此此处加1
			UFset(cow[i].S+1, 1);
		}
		for(i = 1; i < n; ++i)
			printf("%d ", cnt[i]);
		printf("%d\n", cnt[n]);
	}
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值