POJ 2528 Mayor's posters (hash+线段树成段更新)

题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度。现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW。后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报。现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到。)

思路:简单的成段更新,但是数据量是1千万,会MT,所以要区间压缩(离散化),保证覆盖的关系不变,离散化的时候有个易错的细节,poj数据水了,这个易错点引用hh牛的话:

而这题的难点在于每个数字其实表示的是一个单位长度(并非一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)

给出下面两个简单的例子应该能体现普通离散化的缺陷:

例子一:1-10 1-4 5-10

例子二:1-10 1-4 6-10

普通离散化后都变成了[1,4][1,2][3,4]

线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?

例子一是完全被覆盖掉了,而例子二没有被覆盖

//1412 KB 79 ms
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define M 10005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int col[M*16];
int poi[M*4]; //因为经过了特殊化处理,所以在2*M的基础上又放大了一倍
int li[M],ri[M];
bool book[M];
void build(int l,int r,int rt)
{
    col[rt]=-1;
    if(l==r) return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
int Bin(int g,int m)  //二分出离散后的序号
{
    int l=0,r=m-1;
    while(r>=l){
        int mid=(l+r)>>1;
        if(poi[mid]==g) return mid;
        else if(poi[mid]>g) r=mid-1;
        else l=mid+1;
    }
}
void pushdown(int rt)
{
    if(col[rt]==-1) return ;
    col[rt<<1]=col[rt<<1|1]=col[rt];
    col[rt]=-1;
}
void update(int L,int R,int c,int l,int r,int rt)
{

    if(L<=l&&r<=R){
        col[rt]=c;
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
}
int query(int l,int r,int rt)
{
    int ans=0;
    if(col[rt]!=-1){
       if(!book[col[rt]]){
            book[col[rt] ]=true;
            ans++;
       }
       return ans;
    }
    if (l == r) return 0;
    int m=(l+r)>>1;
    ans+=query(lson);
    ans+=query(rson);
    return ans;
}
int main()
{
    int cas,n;
    scanf("%d",&cas);
    while(cas--){
        scanf("%d",&n);
        int cnt=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&li[i],&ri[i]);
            poi[cnt++]=li[i];
            poi[cnt++]=ri[i];
        }
        sort(poi,poi+cnt);
        int top=1;
        for(int i=1;i<cnt;i++){
            if(poi[i]!=poi[i-1]){ //去重
                poi[top++]=poi[i];
            }

        }
        int tmp=top;

        for(int i=1;i<tmp;i++){ //特殊化处理
            if(poi[i]-poi[i-1]>1){
                poi[top++]=poi[i-1]+1;
            }
        }
        sort(poi,poi+top);

        build(0,top-1,1);
        int c=0; //待标记的颜色

        for(int i=1;i<=n;i++){

            int l=Bin(li[i],top);
            int r=Bin(ri[i],top);
            update(l,r,++c,0,top-1,1);

        }
        memset(book,0,sizeof(book));

        printf("%d\n",query(0,top-1,1));
    }
    return 0;
}

POJ 2528 Mayor's posters (hash+线段树成段更新)

时间: 2024-08-02 02:48:53

POJ 2528 Mayor's posters (hash+线段树成段更新)的相关文章

poj 2528 Mayor&#39;s posters(线段树 离散化 区间更新 贴海报)

     这个题目本来对大神来说可能是水题, 对我就不行了,昨晚非折腾到下半夜一点 搞定, 并且可以总结出 ,只有把问题想清楚,或着看人家解题报告自己把问题和代码思路 搞清楚,才能谈的上调bug,否则根本就不知道错在哪儿.说说这个题目的理解,他是如何转化为线段树问题的呢?我们知道线段树有一个区间更新的东西,每张海报的宽度不就是一个区间么?那么我们可以用一棵树中的部分结点 来表示整张海报的可视部分,也就是说每个结点只允许表示一张完整的或着不完整的海报(好几个结点才可以表示成完整的一张海报),那么如

POJ - 2528 - Mayor&#39;s posters 【线段树+离散化+补点】

http://poj.org/problem?id=2528 #include <cstdio> #include <iostream> #include <set> #include <cstring> #include <string> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAXN = 32768 + 5; in

POJ 2528——Mayor&#39;s posters——————【线段树区间替换、找存在的不同区间】

Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been plac

poj(2528)——Mayor&#39;s posters(线段树+离散化)

这道题目让我又重新认识了一下离散化: 首先总结一下离散化的特点: 1)有时区间的端点并不是整数,或者区间太大导致建树内存开销过大而MLE,那么就需要进行离散化后再建树. 2)意思是将区间范围很大的数据集映射到较小的数据集,这样建树更加有效,或者说我们只取需要的值来用. 这个意思说到底就是进行映射,把原来很大的映射到一个较小的空间中去. 题意: 给定一些海报,它们可能相互重叠,告诉你每个海报的宽度(它们的高度都是一样的)和先后的叠放次序,问没有被完全盖住的海报有多少张? 这里我们注意到了数据的范围

POJ - 2528 Mayor&#39;s posters (线段树+离散化)

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

POJ 2528 Mayor&#39;s posters(离散化线段树)

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

(中等) POJ 2528 Mayor&#39;s posters , 离散+线段树。

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

POJ 2528 Mayor&#39;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 所以,离散化处理时要处理边界,

POJ 2528 Mayor&#39;s posters(离散化 线段树)

题意  在墙上贴n张海报  输入每张海报的的左右端点坐标  问最后可以看到多少张海报  能看到一点也是能看到 先把线段树初始化为0 输入一张海报  就把那个区间变成这张海报的序号  最后判断墙上有多少个不同的序号就行了 但是海报坐标的端点值高达10000000  直接用线段树会超时   但是注意到海报最多只有10000张  也就是最多有20000个不同的坐标  于是可以利用离散化的知识   把所有坐标排序 注意所有右端点坐标+1也要加入排序(注意1,10 ; 1,3;  7,10这种情况  如果