POJ 2528 Mayor's posters

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 111111;

#define lc o*2
#define rc o*2+1

int color[maxn<<2], hash[maxn<<2];
int li[maxn], ri[maxn];
int X[maxn*3];

int n, m;

void init()
{
	memset(color, -1, sizeof(color));
	memset(hash, 0, sizeof(hash));
}

void pushdown(int o)
{
	if(color[o] != -1)
	{
		color[lc] = color[rc] = color[o];
		color[o] = -1;
	}
}

void update(int o, int y1, int y2, int L, int R, int v)
{
	if(y1 <= L && y2 >= R)
	{
		color[o] = v;
		return ;
	}
	pushdown(o);
	int M = L + (R-L)/2;
	if(y1 <= M) update(lc, y1, y2, L, M, v);
	if(y2 > M) update(rc, y1, y2, M+1, R, v);
}

void query(int o, int L, int R, int &ans)
{
	if(color[o] != -1)
	{
		if(!hash[color[o]]) ans++;
		hash[color[o]] = 1;
		return ;
	}
	if(L == R) return ;
	int M = L + (R-L)/2;
	query(lc, L, M, ans);
	query(rc, M+1, R, ans);
}

void read_case()
{
	init();
	int nn = 0;
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d%d", &li[i], &ri[i]);
		X[nn++] = li[i];
		X[nn++] = ri[i];
	}
	sort(X, X+nn);
	m = 1;
	for(int i = 1; i < nn; i++) if(X[i-1] != X[i]) X[m++] = X[i];
	for(int i = m-1; i > 0; i--) if(X[i] != X[i-1]+1) X[m++] = X[i-1]+1;
	sort(X, X+m);
}

int BSearch(int *A, int n, int v)
{
	int x = 0, y = n-1;
	while(x <= y)
	{
		int m = x+(y-x)/2;
		if(A[m] == v) return m;
		if(A[m] > v) y = m-1;
		else x = m+1; 
	}
	return -1;
}

void solve()
{
	read_case();
	int y1, y2;
	for(int i = 0; i < n; i++)
	{
		y1 = BSearch(X, m, li[i]);
		y2 = BSearch(X, m, ri[i]);
		update(1, y1, y2, 0, m, i);
	}
	int ans = 0;
	query(1, 0, m, ans);
	printf("%d\n", ans);
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值