[poj2528]Mayor's posters
题目描述
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的更多相关文章
- 线段树---poj2528 Mayor’s posters【成段替换|离散化】
poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...
- poj2528 Mayor's posters(线段树之成段更新)
Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- poj2528 Mayor's posters(线段树区间覆盖)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50888 Accepted: 14737 ...
- [POJ2528]Mayor's posters(离散化+线段树)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 70365 Accepted: 20306 ...
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- poj2528 Mayor's posters【线段树】
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
随机推荐
- BZOJ 4804: 欧拉心算
数论题不多BB,直接开始推导吧: \(\sum_{i=1}^n \sum_{j=1}^n \phi(gcd(i,j))\) \(=\sum_{i=1}^n \sum_{j=1}^n \sum_{d=1 ...
- 【LGR-048 五周年庆贺】洛谷6月月赛
Luogu的五周年庆典比赛,还是比较满意的. 题目清新不毒瘤,数据优质不卡常,解法自然,为出题人点赞. 前三题的难度都很低,T5个人感觉还好.但是最后那个splay+hash是什么神仙东西. 最后好像 ...
- [Codeforces1137D]Cooperative Game
[Codeforces1137D]Cooperative Game Tags:题解 题意 这是一道交互题. 给你一张下面这样的地图,由一条长为\(t\)的有向链和一个长为\(c\)的环构成. 现在你有 ...
- 汇编 OD 标志位 置位相关指令
知识点: l 标志位 置位相关指令 l 标志寄存器PSW 标志寄存器PSW(程序状态字寄存器PSW) 标志寄存器PSW是一个16为的寄存器.它反映了CPU运算的状态特征并且存放某些控制标志. ...
- Quartz.Net分布式任务管理平台(第二版)
前言:在Quartz.Net项目发布第一版后,有挺多园友去下载使用,我们通过QQ去探讨,其中项目中还是存在一定的不完善.所以有了现在这个版本.这个版本的编写完成其实有段时间了一直没有放上去.现在已经同 ...
- ExtJs 编译
前台使用Extjs加载源码的话是非常庞大的,编译之后就只加载一个app.js文件.这种技能如果不知道的话怕别人骂我不是个女程序员.哈哈哈哈哈. 打开cmd,进入程序Extjs的文件夹,如我的程序Ext ...
- 领跑衫获奖感言 & 课程总结
很荣幸在最后一次课获得了黄色领跑衫.在此,我要感谢教师杨贵福,感谢<构建之法>的作者邹欣老师和出版人周筠老师,感谢“耐撕”团队的队员们. 作为旁听生,最后一堂课,有些不舍.不多说,先上图, ...
- Onezero团队第三次站立会议随感
>首先这是一个关于Android的小应用APP(记账本) >在Java基础薄弱的基础上尝试Android开发,让我感觉力不从心. >说实话本迭代周在程序设计,确实让我头疼,不知道怎么 ...
- 《Linux内核分析》第七周: 可执行程序的装载
LINUX内核分析第七周学习总结--可执行程序的装载 杨舒雯(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163.com/course/ ...
- 07-java学习-方法重载-idea集成开发工具学习-项目-模块-包
方法重载的概念? 方法重载的好处? 集成开发工具idea的学习 下载 安装 设置 建项目 导入项目 建模块 导入模块 建包 复制粘贴包 建类 复制粘贴类 运行 调试