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 ...
随机推荐
- ios沙盒机制---基本数据类型的存取和文件的基本操作
沙盒快速存储及读取 存储: [[NSUserDefaults standardUserDefaults] setObject:@"abc" forKey:@"1&quo ...
- 2018年航空概论课后作业(PS:部分答案不正确, 综合得分:83.6)
1 [单选题]航空是指载人或不载人的飞行器在地球____的航行活动. • A.高空• B.大气层内• C.宇宙• D.大气层外我的答案:B 得分: 33.3分 2 [多选题]军用飞机可分为____两大 ...
- 【Java】面向对象初探
前段时间经历了一段心态浮躁期,这让我想起了自己最初的计划,要提升自己知识体系的广度.前几年一直做的是web前端这一块的工作,但我希望通过自己在学习Java这样的过程来提升自己的知识广度. 面向对象概述 ...
- PHP-会话控制Cookie和Session
会话控制:就是为了我们在访问页面和页面之间的跳转是,能够识别到你的登录状态,已经你的登录时长等 在php的会话控制当中,涉及到两个概念Cookie和Session Cookie 会话控制 原理:在登录 ...
- windows 10 上使用pybind11进行C++和Python代码相互调用 | Interfacing C++ and Python with pybind11 on windows 10
本文首发于个人博客https://kezunlin.me/post/8b9c051d/,欢迎阅读! Interfacing C++ and Python with pybind11 on window ...
- Java实现AES加密解密
之前常用两种加密算法:Base64和Md5,前者容易破解,后者不可逆. AES采用对称加密方式,破解难度非常大,在可逆的基础上,能很好的保证数据的安全性. 这里介绍Java中实现AES加密算法的加密与 ...
- 用.net core mvc 开发一个虽小但五脏俱全的网站
.net core mvc 发布有很长时间了,但是一直没有用过,最近突然想开发一个导航网站,于是就抽时间开发了一个专门为开发者使用的导航站点,想看的话请移步我的上一篇博客https://www.cnb ...
- Python练习100题
Python练习100题 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #Filename:001.py cnt = 0#count the sum of res ...
- 关于.ssh目录下的known_hosts文件的补充
一.关于.ssh目录下的known_hosts文件的补充 其实一开始是没有注意到的,按照网上的教程一步一步操作,并没有注意到这个文件的生成.直到有一次我试着去查询.ssh目录是否存在时,出现了下面的情 ...
- tensorflow:模型的保存和训练过程可视化
在使用tf来训练模型的时候,难免会出现中断的情况.这时候自然就希望能够将辛辛苦苦得到的中间参数保留下来,不然下次又要重新开始. 保存模型的方法: #之前是各种构建模型graph的操作(矩阵相乘,sig ...