POJ2528---Mayor's posters
- 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
Output
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
题解:这题注意数据范围,需要离散化(就是将大区间映射为小区间而其表示的内容不变),用线段树区间修改
AC代码为:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=20000+100;
int tree[maxn<<4];
int li[maxn],ri[maxn];
int lisan[3*maxn];
bool visit[3*maxn];
void pushdown(int p)
{
tree[p<<1]=tree[(p<<1)|1]=tree[p];
tree[p]=-1;
}
void update(int p,int l,int r,int x,int y,int v)
{
if(x<=l&&y>=r)
{
tree[p]=v;
return;
}
if(tree[p]!=-1) pushdown(p);
int mid=(l+r)>>1;
if(y<=mid) update(p<<1,l,mid,x,y,v);
else if(x>mid) update((p<<1)|1,mid+1,r,x,y,v);
else update(p<<1,l,mid,x,mid,v),update((p<<1)|1,mid+1,r,mid+1,y,v);
}
int ans;
void query(int p,int l,int r)
{
if(tree[p]!=-1)
{
if(!visit[tree[p]])
{
ans++;
visit[tree[p]]=true;
}
return;
}
if(l==r) return;
int mid=(l+r)>>1;
query(p<<1,l,mid);
query((p<<1)|1,mid+1,r);
}
int main()
{
int T;
scanf("%d",&T);
int n;
while(T--)
{
scanf("%d",&n);
memset(tree,-1,sizeof(tree));
memset(visit,false,sizeof(visit));
int tot=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&li[i],&ri[i]);
lisan[tot++]=li[i];
lisan[tot++]=ri[i];
}
sort(lisan,lisan+tot);
int m=unique(lisan,lisan+tot)-lisan;
int t=m;
for(int i=1;i<t;i++)
{
if(lisan[i]-lisan[i-1]>1)
lisan[m++]=lisan[i-1]+1;
}
sort(lisan,lisan+m);
for(int i=0;i<n;i++)
{
int x=lower_bound(lisan,lisan+m,li[i])-lisan;
int y=lower_bound(lisan,lisan+m,ri[i])-lisan;
update(1,0,m-1,x,y,i);
}
ans=0;
query(1,0,m-1);
printf("%d\n",ans);
}
return 0;
}
/* 1
5
1 4
2 6
8 10
3 4
7 10
*/
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 campa ...
- 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 ...
随机推荐
- PowerMock学习(一)之PoweMock的入门--模拟新增学生操作
关于powermock 在TDD领域Mock框架有很多,比如EasyMock,JMock,Mockito.可能有些同学会好奇了,为什么要重点把powermock拿出来呢,因为powermock可以解决 ...
- PHP数组与xml互相转换
1.数组转xml function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $va ...
- 怎样在PaaS平台上搭建一个会自动关闭的会议室
首相得解释一下,什么叫做会自动关闭的会议室.我们的会议室是存在一个会议预定系统的,一般情况下,我们需要开会的时候,需要先抢占会议室.等待要开会的时候,去会议室里边开会,如果里边有别人,我们可以告诉他们 ...
- [剑指offer]删除链表中重复的结点(把重复的都删掉,1个不留)
①题目 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后为 ...
- JenKins结合cppcheck及cpplint进行代码风格及静态代码检测
JenKins结合cppcheck及cpplint 最近公司需要在Jenkins上安装cppcheck及cpplint进行代码风格及静态代码检测,这里记录下过程. 前提条件 安装了Jenkins 步骤 ...
- 关键路径法(Critical Path Method, CPM)
1.活动节点描述及计算公式 通过分析项目过程中哪个活动序列进度安排的总时差最少来预测项目工期的网络分析. 产生目的:为了解决,在庞大而复杂的项目中,如何合理而有效地组织人力.物力和财力,使之在有限资源 ...
- gcc在x64体系中如何传递参数,linux,mac,iOS适用
上一篇介绍了vc(windows)平台在x64体系当中,c函数的传参方式.本篇将要介绍gcc(类linux,mac)平台在x64中,c函数是如何传参的.为节约时间和篇幅,首先来定义一个有十个参数的函数 ...
- ubuntu server 1604 关机和重启
命令有很多,记住以下两三个就够了 重启: sudo reboot (这个短,易记) sudo shutdown -r now 统一的shutdown形式 关机:sudo shutdown -P no ...
- HotSpot虚拟机对象的创建过程
1.文中讨论的对象限于普通Java对象,不包括数组和class对象. 2.内存的分配方式由Java堆是否规整来决定,而Java堆是否规整取决于垃圾收集器是否有压缩整理的功能. 3.还需要考虑:对象的创 ...
- Linux虚拟机扩容根分区CentOs6.9 VMware14
1.首先关闭虚拟机点击编辑虚拟机设置 2.点击想要扩容的硬盘点击扩容 3.增加容量 输入想增加的容量,因为我本身是30G写到35G是加了5G不是增加30G.(此处为了演示只增加5G) 4.开启虚拟机 ...