POJ 2528

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

const int MAXN = 25000;

struct node
{
	int l;
	int r;
	bool full;
}nodes[MAXN * 5];

struct postPnt
{
	int pos;
	int num;
}post[MAXN];


// cmp functions
bool cmp1(postPnt &a, postPnt &b)
{
	if(a.pos < b.pos)
		return true;
	return false;
};

bool cmp2(postPnt &a, postPnt &b)
{
	if(a.num > b.num)
		return true;
	if(a.num == b.num && a.pos < b.pos)
		return true;
	return false;
};

// segment tree functions
void build(int l, int r, int pos)
{
	nodes[pos].l = l;
	nodes[pos].r = r;
	nodes[pos].full = false;
	int mid;
	if(r != l)
	{
		mid = (l + r) / 2;
		build(l, mid, pos * 2);
		build(mid + 1, r, pos *2 + 1);
	}
};

bool flag = false;
void insert(int l, int r, int pos)
{
	if(nodes[pos].full)
		return;
	int l0 = nodes[pos].l;
	int r0 = nodes[pos].r;
	if(l0 == l && r0 == r)
	{
		nodes[pos].full = true;
		flag = true;
		return;
	}

	if(r <= nodes[pos * 2].r)
		insert(l, r, pos * 2);
	else if(l >= nodes[pos * 2 +  1].l)
		insert(l, r, pos * 2 + 1);
	else
	{
		insert(l, nodes[pos * 2].r, pos * 2);
		insert(nodes[pos * 2 +  1].l, r, pos * 2 + 1);
	}

	nodes[pos].full = nodes[pos * 2].full & nodes[pos * 2 + 1].full;
};

int main()
{
	int caseCnt;
	scanf("%d", &caseCnt);
	
	int posterCnt;
	while(caseCnt--)
	{
		cin >> posterCnt;

		for(int i = 0; i < posterCnt; i++)
		{
			scanf("%d%d", &post[i * 2].pos, &post[i * 2 + 1].pos);
			post[i * 2].num = post[i * 2 + 1].num = i;
		}
		sort(post, post + posterCnt * 2, cmp1);
		int pre = 0;
		int k = 0;
		for(int i = 0; i < posterCnt * 2; i++)
		{
			if(post[i].pos != pre)
			{
				pre = post[i].pos;
				post[i].pos = ++k;
			}
			else
				post[i].pos = k;
		}

		sort(post, post + posterCnt * 2, cmp2);

		build(1, k, 1);
		
		int totalCount = 0;
		for(int i = 0; i < posterCnt; i++)
		{
			flag = false;
			insert(post[i * 2].pos, post[i * 2 + 1].pos, 1);
			if(flag)
				totalCount++;
		}
		printf("%d\n", totalCount);

	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值