首页 > 代码库 > POJ 2528 Mayor's posters(线段树+离散化)

POJ 2528 Mayor's posters(线段树+离散化)

题目链接:Mayor‘s posters

题意:按顺序往墙上贴海报,可以重叠,问最后可以看到多少海报。(被覆盖的海报是看不到的)

注意:

因为数据比较大,所以不离散化,肯定爆内存。

还有就是,不能只是单纯的离散化,还要处理好点的边界

举个例子

4

2  10.

2  8

3  6

6  8

8  10

离散化后

2 3 6 8 10

1 2 3 4 5

覆盖掉了 

1 5   和  1 4俩段

只剩下 2  3  、3  4、 4  5

答案是 3

但是正确答案是4


所以,离散化处理时要处理边界,邻数字间距大于1的话,在其中加上1即可

AC代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define max(a,b) (a>b)?a:b
#define min(a,b) (a>b)?b:a
const int maxn = 110000;
using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

#define MAX INT_MAX
#define MIN INT_MIN

bool vis[maxn];
int zuo[maxn] , you[maxn];
int dian[maxn<<2];
int lazy[maxn<<4];
int sum[maxn<<2];
int lisan[maxn<<2];
int ans = 0;
int cmp(const  void *a,const void *b)
{
    return *(int *)a - *(int *)b;
}
void pushup(int rt)
{
    sum[rt] = sum[rt<<1|1] = sum[rt<<1] ;
}
void pushdown(int rt)
{
	if (lazy[rt] != -1)
    {
		lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
		lazy[rt] = -1;
	}
}
void update(int L,int R,int c,int l,int r,int rt)
{
	if (L <= l && r <= R)
    {
		lazy[rt] = c;
		return ;
	}
	pushdown(rt);
	int m = (l + r) >> 1;
	if (L <= m) update(L , R , c , lson);
	if (m < R) update(L , R , c , rson);
	pushup(rt);
}
void query(int l,int r,int rt)
{
	if (lazy[rt] != -1)
    {
		if (!vis[lazy[rt]])
             ans++;
		vis[lazy[rt]] = 1;
		return ;
	}
	if (l == r) return ;
	int m = (l + r) >> 1;
	query(lson);
	query(rson);
}
int B_search(int key,int n)
{
	int low = 0 , high = n-1;
	while (low <=high)
    {
		int mid = (low + high)/2;
		if (lisan[mid] == key)
            return mid;
		if (lisan[mid] < key)
		 low = mid + 1;
		else
		    high = mid - 1;
	}
	return -1;
}
int main()
{
	int T , n;
	scanf("%d",&T);
	while (T--)
    {
		scanf("%d",&n);
		int num = 0;
		for (int i = 0 ; i < n ; i ++)
		{
			scanf("%d%d",&zuo[i] , &you[i]);
			dian[num++] = zuo[i];
			dian[num++] = you[i];
		}
		qsort(dian , num , sizeof(dian[0]),cmp);
		int cnt = 1;
		lisan[0] = dian[0];
		for (int i = 1 ; i < num; i ++)
		{
			if (dian[i] == dian[i-1])
                continue ;

              //  printf("%d ",dian[i]);
            lisan[cnt++] = dian[i];

		}
		for (int i = cnt - 1 ; i > 0 ; i --)
		{
			if (dian[i] > dian[i-1] + 1)
            {
                lisan[cnt++] = ++dian[i-1];
                //printf("%d = %d ",i-1,dian[i-1]+1);
            }
		}
		//printf("\n");
		qsort(lisan , cnt , sizeof(lisan[0]),cmp);
//		for(int j = 0;j<m;j++)
//            printf("%d ",dian[j]);
//        printf("\n");
		memset(lazy , -1 , sizeof(lazy));
		//int *p,*q;
		for (int i = 0 ; i < n ; i ++)
        {
			int l = B_search(zuo[i], cnt);

			int r = B_search(you[i], cnt);

			update(l, r, i, 0, cnt - 1, 1);
		}
		ans = 0;
		memset(vis, false, sizeof(vis));
		query(0, cnt - 1, 1);
		printf("%d\n",ans);
	}
	return 0;
}