【线段树】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 ...
随机推荐
- 炒鸡简单的canvas粒子(山东数漫江湖)
位图的canvas一直不会被svg比下去的原因了. 俗话说,须弥芥子,是大小之说,也有以小见大之说,颗颗粒子,足以构建宏大效果. 这是一篇炒鸡简单的canvas粒子教程,主要是讲如何粒子特效的原理,一 ...
- Everything Has Changed(HDU6354+圆交+求周长)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6354 题目: 题意:用一堆圆来切割一个圆心为原点,半径为R的圆A,问切割完毕后圆A外围剩余部分的周长( ...
- LCD实验学习笔记(六):存储控制器
s3c2440可使用地址空间为1GB(0x00000000到0x40000000). 1G空间分为8个BANK,每个BANK为128MB. 设27条地址线,和8个片选引脚(nGCS0-nGCS7). ...
- Centos7的iso everything与DVD以及Live的区别
DVD.ISO 可以用安装程序安装的所有安装包,推荐镜像 Netinstall.iso 从网络安装或者救援系统 Everything.iso 包含centos7的一套完整的软件包,可以用来安装系统或者 ...
- 64_a1
AGReader-1.2-16.fc26.x86_64.rpm 13-Feb-2017 23:31 50654 ATpy-0.9.7-11.fc26.noarch.rpm 13-Feb-2017 22 ...
- JVM对象分配和GC分布【JVM】
最近在学习java基础结构,刚好学到了jvm,总结了以下并可以结合思维导图认识以下Jvm的对象: 栈:什么是栈? 先说一下栈的数据结构吧,栈它是一种先进后出的数据结构(FILO),跟队列刚好相反(先进 ...
- docker安装(2016-08-25版本)
. 通过命令对系统的版本进行查看 [root@localhost ~]# uname -a [root@localhost ~]# cat /etc/issue --> 如果是6.5之前的版本 ...
- awk常见操作整理(更新)
awk的基本结构 awk 'BEGIN{} pattern {} END {}' #pattern {} 部分是针对每行进行循环处理的,有pattern表示对匹配到的行处理,没有pattern表示对所 ...
- tornado 响应头 中断 状态码 工作流程
set_header 设置响应头 clear_header 清除响应头 add_header 增加响应头 self.flush self.finish 中断 set_status ...
- IntelJ IDEA 进行Java Web开发+热部署+一些开发上的问题
基本上像放弃MyEclipse或者Eclipse了,因为IDEA现在也有对应的版本旗舰版和社区版了,而且使用更贴心,更给力,为什么还要选一个难用的要死的东西呢? 最近要开发一个Java Web项目,所 ...