POJ 2528 Mayor's posters(离散化的线段树)
题意:
有一个墙,很长,有n个人在上面贴海报。问最后能看到的海报个数(没被完全覆盖的也算)
思路:
1. 首先是用线段树来求解,但是如果按照题目的意思来,会超时。所以要想办法把问题离散化。
2. 因为人的数量范围是很小的,所以可以利用人来作为区间,这样把区间控制在了n的数量级。
3. 在离散化的时候,为了保证正确性,还要考虑2点是否相邻,如果不相邻,则要在中间加上一个点。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define lhs l, m, rt << 1
#define rhs m + 1, r, rt << 1 | 1
const int maxn = 10010;
int seg[maxn << 4];
int coord[maxn << 2];
int cl[maxn], cr[maxn];
bool hash[maxn];
void PushDown(int rt)
{
if (seg[rt])
{
seg[rt << 1] = seg[rt << 1 | 1] = seg[rt];
seg[rt] = 0;
}
}
void Update(int beg, int end, int value, int l, int r, int rt)
{
if (beg <= l && r <= end)
{
seg[rt] = value;
return ;
}
PushDown(rt);
int m = (l + r) >> 1;
if (beg <= m)
Update(beg, end, value, lhs);
if (end > m)
Update(beg, end, value, rhs);
}
int Query(int l, int r, int rt)
{
if (seg[rt] && !hash[seg[rt]])
{
hash[seg[rt]] = true;
return 1;
}
else if (seg[rt] || l == r)
return 0;
int ret = 0;
int m = (l + r) >> 1;
ret += Query(lhs);
ret += Query(rhs);
return ret;
}
int Search(int l, int r, int value)
{
while (l <= r)
{
int m = (l + r) >> 1;
if (coord[m] == value)
return m;
else if (coord[m] > value)
r = m - 1;
else
l = m + 1;
}
return -1;
}
int main()
{
int cases;
scanf("%d", &cases);
while (cases--)
{
memset(seg, 0, sizeof(seg));
memset(coord, 0, sizeof(coord));
memset(hash, false, sizeof(hash));
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d %d", &cl[i], &cr[i]);
int c = 0;
for (int i = 1; i <= n; ++i)
coord[++c] = cl[i], coord[++c] = cr[i];
sort(coord + 1, coord + c + 1);
int i, j;
for (i = 1, j = 2; j <= c; ++j)
if (coord[i] != coord[j])
coord[++i] = coord[j];
int m = c = i;
for (i = 1; i + 1 <= m; ++i)
if (coord[i] + 1 != coord[i+1])
coord[++c] = coord[i] + 1;
sort(coord + 1, coord + c + 1);
for (int i = 1; i <= n; ++i)
{
int a = Search(1, c, cl[i]);
int b = Search(1, c, cr[i]);
Update(a, b, i, 1, c, 1);
}
printf("%d\n", Query(1, c, 1));
}
return 0;
}
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------