Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 64939   Accepted: 18770

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 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. 

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input. 

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

题意:

$N$个人$(1\leq N\leq10^4)$依次贴$N$张等高的海报,给出每张海报的左端点,右端点$l_i,r_i$。$(1\leq l_i\leq r_i\leq10^7)$

后面的海报可能会把前面的海报盖住。问最后能看见几张海报。

题解:

一个比较明显的线段树区间染色问题。但是端点范围太大,直接开数组明显不可行。

所以还要离散化一下。所谓的离散化,就是将一个很大的区间映射成一个很小的区间,而不改变原有的覆盖关系。

但是对于这道题而言,简单的离散化可能会出现错误。

比如说:

例子一:$[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$是否被覆盖掉了呢?

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

解决的办法,就是在距离$\geq 1$的两个相邻节点间插入一个点,保证准确性。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct node{int l,r;}a[]; //节点信息
int N,ans;
int c[]; //区间每个点的颜色
bool vis[]; //记录颜色访问信息
int x[]; //离散化后的区间
inline int read(){
int x=,f=;
char c=getchar();
for(;!isdigit(c);c=getchar())
if(c=='-')
f=-;
for(;isdigit(c);c=getchar())
x=x*+c-'';
return x*f;
}
void cover(int num){
if(c[num]!=-){
c[num<<]=c[num<<|]=c[num];
c[num]=-;
}
}
void update(int L,int R,int col,int l,int r,int num){
//如果这个点在需要染色的区间内,直接更新并且返回
if(l>=L && r<=R){
c[num]=col;
return;
}
cover(num); //更新
int mid=(l+r)>>;
if(L<=mid) update(L,R,col,l,mid,num<<); //更新左儿子
if(R>mid) update(L,R,col,mid+,r,num<<|); //更新右儿子
}
void query(int l,int r,int num){
//累加未出现的颜色
if(c[num]!=-){
if(!vis[c[num]]) ans++;
vis[c[num]]=;
return;
}
if(l==r) return; //如果到了叶子则返回
int mid=(l+r)>>;
query(l,mid,num<<); //统计左儿子
query(mid+,r,num<<|); //统计右儿子
}
int main(){
int T=read();
while(T--){
memset(c,-,sizeof(c));
memset(vis,,sizeof(vis));
N=read();ans=;
int p=; //输入点数
int q=; //离散化后点数
for(int i=;i<N;i++){
a[i].l=read();a[i].r=read();
x[p++]=a[i].l;x[p++]=a[i].r;
}
sort(x,x+p);
//去重操作
for(int i=;i<p;i++)
if(x[i]!=x[i-])
x[q++]=x[i];
//离散化操作
for(int i=q-;i>=;i--)
if(x[i]>x[i-]+)
x[q++]=x[i-]+;
sort(x,x+q);
//染色操作
for(int i=;i<N;i++){
int l=lower_bound(x,x+q,a[i].l)-x;
int r=lower_bound(x,x+q,a[i].r)-x;
update(l,r,i,,q,);
}
//统计操作
query(,q,);
printf("%d\n",ans);
}
return ;
}

【poj2528】Mayor's posters的更多相关文章

  1. 【poj2528】Mayor's posters

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59254   Accepted: 17167 Description The ...

  2. 【SDOJ 3741】 【poj2528】 Mayor's posters

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

  3. 【线段树】Mayor's posters

    [poj2528]Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 66154   Accept ...

  4. 【POJ 2528】Mayor’s posters(线段树+离散化)

    题目 给定每张海报的覆盖区间,按顺序覆盖后,最后有几张海报没有被其他海报完全覆盖.离散化处理完区间端点,排序后再给相差大于1的相邻端点之间再加一个点,再排序.线段树,tree[i]表示节点i对应区间是 ...

  5. 【hdu】Mayor&#39;s posters(线段树区间问题)

    须要离散化处理,线段树的区间改动问题. 须要注意的就是离散化的时候,由于给的数字是一段单位长度,所以须要特殊处理(由于线段的覆盖和点的覆盖是不一样的) 比方:(1,10)(1,4) (6,10) 离散 ...

  6. POJ2528 Uva10587 Mayor's posters

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

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

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

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

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

  9. POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】

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

随机推荐

  1. Arcgis Engine(ae)接口详解(8):临时元素(element)

    //主地图的地图(map)对象 IMap map = null; IActiveView activeView = null; //IGraphicsContainer用于操作临时元素,可以通过map ...

  2. Java 复杂excel报表导出

    MyExcel,是一个可直接使用Html文件,或者使用内置的Freemarker.Groovy.Beetl等模板引擎Excel构建器生成的Html文件,以Html文件中的Table作为Excel模板来 ...

  3. C++ JSON解析库RapidJSON

    https://github.com/Tencent/rapidjson jsontext.txt { "result" : [ { "face_id" : & ...

  4. Linux epoll 源码注释

    https://www.cnblogs.com/stonehat/p/8613505.html 这篇文章值得好好读,先留个记录,回头看. IO多路复用之epoll总结 - Anker's Blog - ...

  5. HDU 6109 数据分割 【并查集+set】 (2017"百度之星"程序设计大赛 - 初赛(A))

    数据分割 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. (linux)mmccard驱动的读写过程解析

      mmc io的读写从mmc_queue_thread()的获取queue里面的request开始. 先列出调用栈,看下大概的调用顺序, 下面的内容主要阐述这些函数如何工作. host->op ...

  7. 进程调度函数schedule()分析

    1.功能简述: 最主要作用就是 从就绪进程中选择一个优先级最高的进程来代替当前进程运行.   2.代码分析 schedule();      struct task_struct *tsk = cur ...

  8. JS基础篇--JS的event.srcElement与event.target(触发事件对象)

    IE下,event对象有srcElement属性,但是没有target属性; Firefox下,event对象有target属性,但是没有srcElement属性.但他们的作用是相当的,即: fire ...

  9. MYSQL进阶学习笔记六:MySQL视图的创建,理解及管理!(视频序号:进阶_14,15)

    知识点七:MySQL视图的创建(14) 视图的定义: 什么是视图: 视图数由查询结果形成的一张虚拟的表. 什么时候要用到视图? 如果某个查询结果出现的非常频繁,也就是,要经常拿这个查询结果来做子查询. ...

  10. html5--6-14 CSS3中的颜色表示方式

    html5--6-14 CSS3中的颜色表示方式 实例 每个参数 (red.green 以及 blue) 定义颜色的强度,可以是介于 0 与 255 之间的整数,或者是百分比值(从 0% 到 100% ...