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;
}

你可能感兴趣的:(POJ 2528 Mayor's posters)