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 ...
随机推荐
- 小白学 Python(24):Excel 基础操作(下)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- C# V: 读取XML文件
在C#中读取XML有LINQ版本和非LINQ版本. LINQ版本: // Loading from a file, you can also load from a stream var xml = ...
- Mybatis动态语句部分收集
where: <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BL ...
- XjhDemo 插入数据
create database XjhDemo gouse XjhDemogo --商品类别Create table Sort( SortId int primary key identity(1,1 ...
- mysql定时任务(event事件)
1.event事件 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器” 事件和触发器类似,都 ...
- 使用sklearn和caffe进行逻辑回归 | Brewing Logistic Regression then Going Deeper
原文首发于个人博客https://kezunlin.me/post/c50b0018/,欢迎阅读! Brewing Logistic Regression then Going Deeper. Bre ...
- 小白学 Python 爬虫(5):前置准备(四)数据库基础
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- Stream系列(二)Map方法使用
Stream 将List 里面的对象转换为新的对象 EmployeeTestCase.java package com.example.demo; import lombok.Data; import ...
- nginx-(三)基本模块1
nginx常用模块介绍 ngx_http_access_module模块的配置(基于IP的访问控制) allow address | CIDR | unix: | all; deny address ...
- linux 进程简介
进程相关知识简介 进程定义: 一个运行中的程序即一个process task struct: 内核存储进程信息的固定格式称为task struct,task struct记录了例如该进程内存下一跳位置 ...