POJ 2528 Mayor's posters (线段树+离散化)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions:75394 | Accepted: 21747 | 
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 思路:
好久没有写博客了,做这个题,我写了一整天,就因为一句话没有写好,我实在是想骂人呀!
首先离散化,避免超时,避免超内存,这个我相信你在别的博客上可以看见,我就不多说,线段树的节点里面,存的是下面全部相同的数字,也就是说,如果这个节点,代表的是l到r的范围内的数,
如果l到r里面,数字完全相同,那么,这个节点就存下这个数,否则,那我们就存下-1.如果查询的时候遇到-1,那我们就继续向下查询就行了,直到查到不为-1或到达叶子节点为止。
我的错误是update的最后一行,忽略了下面两个节点都是-1的情况
AC代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int s[400086];
bool book[400086];
struct node
{
int l,r,num;
bool lazy;
}tree[1600099];
int x[100086],y[100086]; void build(int t,int l,int r)
{
tree[t].l=l;tree[t].r=r;
tree[t].num=-1;tree[t].lazy=false;
if(l==r){return;}
int mid=(l+r)/2;
build(t<<1,l,mid);
build((t<<1)|1,mid+1,r);
} void push_down(int t)
{
tree[t].lazy=false;
tree[t<<1].num=tree[t].num;
if(tree[t<<1].l!=tree[t<<1].r){tree[t<<1].lazy=true;} tree[(t<<1)|1].num=tree[t].num;
if(tree[(t<<1)|1].l!=tree[(t<<1)|1].r){tree[(t<<1)|1].lazy=true;}
} void update(int t,int l,int r,int e)
{
if(tree[t].lazy){push_down(t);}
if(tree[t].l==l&&r==tree[t].r){
tree[t].num=e;
if(l!=r){tree[t].lazy=true;}
return;
} int mid=(tree[t].l+tree[t].r)>>1;
if(r<=mid){update(t<<1,l,r,e);}
else if(l>mid){update((t<<1)|1,l,r,e);}
else{
update((t<<1),l,mid,e);
update((t<<1)|1,mid+1,r,e);
}
if(tree[t<<1].num!=tree[(t<<1)|1].num||tree[t<<1].num==-1||tree[(t<<1)|1].num==-1){tree[t].num=-1;}
} int query(int t)
{
int ans=0;
if(tree[t].num==-1){
if(tree[t].lazy){push_down(t);}
if(tree[t].l==tree[t].r){return 0;}
ans+=query(t<<1);
ans+=query((t<<1)|1);
}
else{
if(!book[tree[t].num]){book[tree[t].num]=true;return 1;}
}
return ans;
} int main()
{
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(book,0,sizeof(book));
memset(s,0,sizeof(s));
int nn=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
s[++nn]=x[i];s[++nn]=y[i];
}
sort(s+1,s+nn+1);
int siz=unique(s+1,s+nn+1)-s-1;
int k=siz;
for(int i=1;i<=k-1;i++){
if(s[i+1]-s[i]>1){s[++siz]=s[i]+1;}
}
sort(s+1,s+siz+1);
build(1,1,siz);
int l,r;
for(int i=1;i<=n;i++){
l=lower_bound(s+1,s+siz+1,x[i])-s;
r=lower_bound(s+1,s+siz+1,y[i])-s;
update(1,l,r,i);
}
printf("%d\n",query(1)); }
}
POJ 2528 Mayor's posters (线段树+离散化)的更多相关文章
- poj 2528 Mayor's posters 线段树+离散化技巧
		
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
 - POJ 2528 Mayor's posters(线段树+离散化)
		
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
 - poj 2528 Mayor's posters  线段树+离散化  ||  hihocode #1079 离散化
		
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
 - POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
		
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
 - POJ 2528 Mayor's posters (线段树区间更新+离散化)
		
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
 - POJ 2528 Mayor’s posters (线段树段替换 &&  离散化)
		
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
 - poj 2528 Mayor's posters 线段树区间更新
		
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
 - poj 2528 Mayor's posters(线段树)
		
题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...
 - POJ 2528 Mayor's posters (线段树)
		
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
 
随机推荐
- yii2的下载安装
			
1.直接使用归档文件安装yii2的高级模板: 从 yiiframework.com 下载归档文件. 下载yii2的高级模板的压缩文件, 将yii-advanced-app-2.0.12文件夹复制到项目 ...
 - NaN与Null与undefiined的关系
			
在js中,定义一个变量需要通过关键字var来定义,定义的变量可以是字符串.数字等等都行.但是如果你只是定义了一个变量,没有给他赋值,那么它就默认为'undefined'.例如 var name; co ...
 - yolo算法解析
 - 自定义django-admin命令
			
我们可以通过manage.py编写和注册自定义的命令. 自定义的管理命令对于独立脚本非常有用,特别是那些使用Linux的crontab服务,或者Windows的调度任务执行的脚本.比如,你有个需求,需 ...
 - jsp大学作业:jsp编写单选,复选判断题及得分情况
			
project_1_1.jsp <%@ page contentType="text/html;charset=utf-8" language="java" ...
 - 「POJ 1135」Domino Effect(dfs)
			
BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...
 - android 通过修改图片像素实现CircleImageView
			
CircleImageView实现方法有很多种,各有优缺点,因此需要按照不同的场景使用.我们今天使用修改图片像素的方法实现CircleImageView,主要知识点无非是勾股定理和点到圆形的距离. 素 ...
 - 【BZOJ5289】[HNOI2018]排列(贪心)
			
[BZOJ5289][HNOI2018]排列(贪心) 题面 BZOJ 洛谷 题解 这个限制看起来不知道在干什么,其实就是找到所有排列\(p\)中,\(p_k=x\),那么\(k<j\),其中\( ...
 - 「SDOI2014」Lis 解题报告
			
「SDOI2014」Lis 题目描述 给定序列 \(A\),序列中的每一项 \(A_i\) 有删除代价 \(B_i\) 和附加属性 \(C_i\). 请删除若干项,使得 \(A\) 的最长上升子序列长 ...
 - Squid代理服务部署
			
构建Squid代理服务器1.配置IP地址 2.编译安装Squid软件[root@localhost ~]# tar -zxvf squid-3.4.6.tar.gz -C /usr/src/[root ...