题目描述

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 placing the posters and introduce the following rules:
Every candidate can place exactly one poster on the wall.
All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
The wall is divided into segments and the width of each segment is one byte.
Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

题目大意

在一面无限长的墙上有很多的画,让你求出最后一共能看到多少画。

解题思路

首先我们看到这个比较恐怖的数据范围(无限oo),对于我们直接维护裸的线段树。线段树先生:。。。我做不到。。。(粗鄙之语)****
不开玩笑了。我们需要思考如何将这个线段树维护的区间尽量的变小。那么因为一面画会占据一段的区间,那么这一段的区间所有的点全都是一样的,那么我们就可以把这个区间当作一个点,这就是我们常常说的离散化。
所谓离散化就是把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。来自百度娘的温馨提示
举个例子:
原数据:1,999,100000,15;处理后:1,3,4,2;
原数据:{100,200},{20,50000},{1,400};
处理后:{3,4},{2,6},{1,5};
做个比方:有一排的香蕉,有一些香蕉是一样的,这些香蕉可以合体成一个王者香蕉中二病又犯了,这个王者香蕉代表着组成他的香蕉臣子。
那么我们就将这个线段树需要维护的区间尽可能的缩小了,减小了时空复杂度。
重点来了,注意离散化的边界,因为离散化时,我们相邻的两个区间常常会出现问题,就如这道题,因为是覆盖的问题,我们在离散的时候,会驶两个区间相交,但是无法确定是否是覆盖的还是刚好接触。为了防止这种问题,我们需要通过+1来区别区间和区间之间的边界问题,也就是要新增节点是前一个节点+1,保证了离散化的正确性。
那么就是线段树的基本操作,稍微讲一下过程:每次将原来的画放到离散化的线段树中,将这个区间标记成\(i\),表示这个是第i号区间覆盖过的地方,在最后统计答案,遍历整个线段树,如果有不一样的为遍历的标记,那么就答案++。

ac代码

#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#define lson nod<<1
#define rson nod<<1|1
#define N 100005
using namespace std;
struct node{int l,r;}a[N];
int disc[N],ans;
bool vis[N];
struct SegmantTree{
    int tree[N<<2];
    void init(){memset(tree,-1,sizeof(tree));}
    void pushdown(int nod){tree[lson]=tree[rson]=tree[nod],tree[nod]=-1;}
    void update(int l,int r,int ql,int qr,int v,int nod){
        if(ql<=l&&r<=qr){tree[nod]=v;return;}
        int mid=l+r>>1;
        if(tree[nod]!=-1)pushdown(nod);
        if(ql<=mid) update(l,mid,ql,qr,v,lson);
        if(qr>mid) update(mid+1,r,ql,qr,v,rson);
    }
    void query(int l,int r,int nod){
        if(tree[nod]!=-1){
            if(!vis[tree[nod]]) ++ans,vis[tree[nod]]=true;
            return;
        }
        if(l==r)return;
        int mid=l+r>>1;
        query(l,mid,lson); query(mid+1,r,rson);
    }
}St;
int r(){
    int w=0,x=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x;
}
int main(){
    int cas=r();
    while(cas--){
        memset(vis,0,sizeof(vis));
        St.init();
        int n=r(),cnt=0;
        for(int i=0;i<n;i++) a[i].l=r(),a[i].r=r(),disc[cnt++]=a[i].l,disc[cnt++]=a[i].r;
        sort(disc,disc+cnt);
        cnt=unique(disc,disc+cnt)-disc;
        int t=cnt;
        for(int i=1;i<t;i++) if(disc[i]-disc[i-1]>1) disc[cnt++]=disc[i-1]+1;
        sort(disc,disc+cnt);
        for(int i=0;i<n;i++){
            int x=lower_bound(disc,disc+cnt,a[i].l)-disc;
            int y=lower_bound(disc,disc+cnt,a[i].r)-disc;
            St.update(0,cnt-1,x,y,i,1);
        }
        ans=0;
        St.query(0,cnt-1,1);
        printf("%d\n",ans);
    }
    return 0;
}

[poj2528]Mayor's posters的更多相关文章

  1. 线段树---poj2528 Mayor’s posters【成段替换|离散化】

    poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...

  2. poj2528 Mayor's posters(线段树之成段更新)

    Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...

  3. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 ...

  4. poj2528 Mayor's posters(线段树区间覆盖)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 50888   Accepted: 14737 ...

  5. [POJ2528]Mayor's posters(离散化+线段树)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 70365   Accepted: 20306 ...

  6. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  7. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  8. poj2528 Mayor's posters【线段树】

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  9. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

随机推荐

  1. Luogu P3990 [SHOI2013]超级跳马

    这道题还是一道比较不可做的矩阵题 首先我们先YY一个递推的算法:令f[i][j]表示走到第i行第j列时的方案数,那么有以下转移: f[i][j]=f[i-1][j-2*k+1]+f[i+1][j-2* ...

  2. pyenv+virtual 笔记

    Pyenv + virtualEnv 设置 安装这两个组件是为了适应不同版本的python在同一个系统下的运行:例如现在最明显就是python2.7和python3.6的两个版本,很多库依旧是使用了P ...

  3. C# 全屏坐标及区域坐标获取。自定义光标及系统光标描边捕捉显示。

    最近手头工作比较轻松了一点就继续研究和完善之前的录屏软件,使用AForge最大的问题在于:最原始的只能够录全屏,而自定义的录屏需要更改非常多的细节:like follows: 1.需要支持区域化录屏: ...

  4. javascript调用ActiveX接口失败的解决方案及使用心得

    前段时间公司做了个比较大的项目,需要用到ocx控件,我厂大部分项目都采用C#.net,而winform程序条用ocx控件接口是相对简单的,但是javascript调用ocx接口,却和winform的用 ...

  5. rsync同步时,删除目标目录比源目录多余文件的方法(--delete)

    在日常运维工作中,我们经常用到rsync这个同步神器.有时在同步两个目录时,会要求删除目标目录中比源目录多出的文件,这种情况下,就可用到rsync的--delete参数来实现这个需求了. 实例说明:在 ...

  6. vue-cli中的check-versions.js配置文件包括semver,chalk,shell插件的解释

    本文介绍vue-cli脚手架build目录中check-versions.js的配置 本文件是用来检测node和npm版本的 直接上代码加注释 // 下面的插件是chalk插件,他的作用是在控制台中输 ...

  7. Scrum Meeting 8

                第八次会议 No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 No_11:照片记录 待更新 No_100:代码/文档签入记录 No_101:出席表 ...

  8. linux及安全第四周总结

    学习内容:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 一.用户态.内核态 权限分级——为了系统本身更稳定,使系统不宜崩溃.(并不是所有程序员缩写的代码都很健壮!!) x86 CP ...

  9. 《Linux内核设计与实现》读书笔记 3

    第三章 进程管理 3.1进程 概念: 进程:处于执行期的程序.但不仅局限于程序,还包含其他资源(打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内催音社的内存地址空间及一个或多个执行线 ...

  10. 虚拟机Linux(centos)系统能ping通主机,主机无法ping通Linux解决方案

    本文引用:https://blog.csdn.net/clean_water/article/details/53023308 三个步骤: 第一步:虚拟机网络连接方式选择Nat 第二步.关闭liunx ...