【线段树】Mayor's posters
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 66154 | Accepted: 19104 |
Description
- 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
Source
1
3
1 3
6 10
1 10 //正确输出:3
//错误输出:2
//问题原因:离散化成了[1,2] [3,4] [1,4],这样确实只剩下2了
如何解决?在两两之差>1时(区域不会被完全覆盖),就可以在这里插入一个节点以标记这里有一个区间要算。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=200001;
const int INF=999999;
int N,M;
int T;
int A[MAXN*2],a[MAXN*2],b[MAXN*2];
int tr[MAXN*8+100];
int cnt,tmp3;
bool flag[MAXN*8+100];
bool Hash[MAXN*8+100];
int tmp; void tage_lazy(int rt,int l,int r){
if(flag[rt]){
flag[rt*2]=flag[rt*2+1]=true;
tr[rt*2]=tr[rt*2+1]=tr[rt];
flag[rt]=false;
}
return ;
}
void add(int l,int r,int rt,int L,int R){
if(L<=l&&R>=r){
tr[rt]=cnt;
flag[rt]=true;
return ;
}
tage_lazy(rt,l,r);
int mid=(l+r)>>1;
if(mid<R) add(mid+1,r,rt*2+1,L,R);
if(mid>=L) add(l,mid,rt*2,L,R);
return ;
}
int Que(int l,int r,int rt,int L,int R){
if(flag[rt]){
if(!Hash[tr[rt]]){
Hash[tr[rt]]=true;
return 1;
}
else return 0;
}
if(l==r) return 0;
int mid=(l+r)>>1;
return Que(l,mid,rt*2,L,R)+Que(mid+1,r,rt*2+1,L,R);
} int main(){
T=read();
while(T--){
memset(tr,0,sizeof(tr));
memset(flag,false,sizeof(flag));
memset(Hash,false,sizeof(Hash));
N=read();tmp=tmp3=0;
for(int i=1;i<=N;i++){
++tmp;A[tmp]=a[tmp]=read();
++tmp;A[tmp]=a[tmp]=read();
}
sort(a+1,a+tmp+1);
int tmp2=0;int treef=tmp;
for(int i=1;i<=tmp;i++)
if(a[i]==a[i-1]) treef--;
else b[++tmp3]=a[i];
int k=tmp3;
for(int i=1;i<=k;i++)
if(b[i]>b[i-1]+1) b[++tmp3]=b[i-1]+1;
sort(b+1,b+tmp3+1);
treef=tmp3;
for(int i=1;i<=N;i++){
++cnt;
int l=lower_bound(b+1,b+tmp3+1,A[tmp2+1])-b;
int r=lower_bound(b+1,b+tmp3+1,A[tmp2+2])-b;
add(1,treef,1,l,r);
tmp2+=2;
}
printf("%d\n",Que(1,treef,1,1,treef));
}
}
【线段树】Mayor's posters的更多相关文章
- 线段树 Mayor's posters
甚至DFS也能过吧 Mayor's posters POJ - 2528 The citizens of Bytetown, AB, could not stand that the candidat ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- Mayor's posters(离散化线段树)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 54067 Accepted: 15713 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- Mayor's posters POJ - 2528(线段树 + 离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74745 Accepted: 21574 ...
随机推荐
- java springmvc4 图片或文件上传
1.文件配置 配置文件解析 上传文件处理的核心方法 // uploadOneFile.jsp, uploadMultiFile.jsp submit to. @RequestMapping(value ...
- 20、redis和memcached比较?
1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供lis ...
- for in、each; for 、forEach、map
1.jQuery.each(object, [callback]) 用于例遍任何对象.回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内容.如果需要退出 each 循环可使回调 ...
- 实战手工注入某站,mssql注入
昨天就搞下来的,但是是工具搞得,为了比赛还是抛弃一阵子的工具吧.内容相对简单,可掠过. 报错得到sql语句: DataSet ds2 = BusinessLibrary.classHelper.Get ...
- 树莓派开启smb
1.安装smb apt-get install samba samba-common-bin 2.修改/etc/samba/smb.conf配置 设置使用系统用户登入 增加smb访问文件夹 [shar ...
- Django【设计】settings方案
配置文件: 目标:配置文件,默认配置和手动配置分开,参考django的配置文件方案,默认配置文件放在内部,只让用户做常用配置 /bin/settings.py(手动配置) PLUGIN_ITE ...
- fork与vfork区别
1. 地址空间各段拷贝: fork: 内核为子进程生成新的地址空间结构,拷贝父进程的代码段,数据空间,堆,栈到自身的地址空间,但注意:子进程的代码段并不会分配物理空间,而是指向父进程的代码段物理空间, ...
- WA时查错点
这篇文章旨在总结可能出错的原因,想到时随时会补充. 查看调试输出语句是否删除 查看数组是否清零 查看是否使用long long 查看是否有的常量应开LL(如1LL << (32) ) 查看 ...
- 部署HBase系统(分布式部署)
1.简介 HBase系统主要依赖于zookeeper和hdfs系统,所以部署HBase需要先去部署zookeeper和hadoop 2.部署开始 IP或者HOSTNAME需要根据自身主机信息设定. 部 ...
- django渲染模板时跟vue使用的{{ }}冲突解决方法
var vm = new Vue({ el: '#app', // 分割符: 修改vue中显示数据的语法, 防止与django冲突 delimiters: ['[[', ']]'], data: { ...