離散化+線段樹 poj2528

//離散化+線段樹

#include <cstdio>
#include <cstring>
#include <algorithm>
#define CLR(A) memset(A,0,sizeof(A))
using namespace std;

const int N = 20005;
int col[N * 4], vis[N];
int le[N], ri[N], c[N * 2], ans;

void pushdown(int p)
{
	if (col[p] == -1) return;
	col[p << 1] = col[p << 1 | 1] = col[p];
	col[p] = -1;
}

void update(int p, int s, int e, int l, int r, int v)
{
	if (s >= l && e <= r)
	{
		col[p] = v;
		return;
	}
	pushdown(p);
	if (l <= (s+e)>>1) update(p<<1,s,((s+e)>>1), l, r, v);
	if (r > (s+e)>>1) update(p<<1|1,((s+e)>>1)+1,e, l, r, v);
}

void query(int p, int s, int e)
{
	if (col[p] != -1)
	{
		if (!vis[col[p]])
			vis[col[p]] = 1, ++ans;
		return;
	}
	query(p << 1, s, (s+e)>>1);
	query(p << 1 | 1, ((s+e)>>1) + 1,e);
}

int compress(int n) //離散化
{
	int k = 0;
	for (int i = 0; i < n; ++i)
	{
		c[k++] = le[i];
		c[k++] = ri[i];
		c[k++] = ri[i] + 1;
	}
	sort(c, c + k);
	return unique(c, c + k) - c - 1;
}

int main()
{
	int cas, m, n, l, r;
	scanf("%d", &cas);
	while (cas--)
	{
		scanf("%d", &m);
		for (int i = 0; i < m; ++i)
			scanf("%d%d", &le[i], &ri[i]);
		n = compress(m);
		ans = 0;
		CLR(col), CLR(vis);
		for (int i = 0; i < m; ++i)
		{
			l = lower_bound(c, c + n, le[i]) - c + 1;
			r = lower_bound(c, c + n, ri[i]) - c + 1;
			update(1, 1, n, l, r, i + 1);
		}
		vis[0] = 1;
		query(1, 1, n);
		printf("%d\n", ans);
	}
	return 0;
}

發佈了7 篇原創文章 · 獲贊 3 · 訪問量 5565
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章