Mayor's posters (线段树加离散化)
个人心得:线段树也有了一定的掌握,线段树对于区间问题的高效性还是挺好的,不过当区间过大时就需要离散化了,一直不了解离散化是什么鬼,后面去看了下
解法:离散化,如下面的例子(题目的样例),因为单位1是一个单位长度,将下面的
1 2 3 4 6 7 8 10
— — — — — — — —
1 2 3 4 5 6 7 8
离散化 X[1] = 1; X[2] = 2; X[3] = 3; X[4] = 4; X[5] = 6; X[7] = 8; X[8] = 10
于是将一个很大的区间映射到一个较小的区间之中了,然后再对每一张海报依次更新在宽度为1~8的墙上(用线段树),最后统计不同颜色的段数。
但是只是这样简单的离散化是错误的,
如三张海报为:1~10 1~4 6~10
离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10
第一张海报时:墙的1~4被染为1;
第二张海报时:墙的1~2被染为2,3~4仍为1;
第三张海报时:墙的3~4被染为3,1~2仍为2。
最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。
新的离散方法为:在相差大于1的数间加一个数,例如在上面1 4 6 10中间加5(算法中实际上1,4之间,6,10之间都新增了数的)
X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10
这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3
最终,1~2为2,3为1,4~5为3,于是输出正确结果3。
看题目吧。
- 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
#include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int inf=0xffffff0;
const int length=;
const int hl=;
struct tree
{
int l,r;
bool tcover;
int mid()
{
return (l+r)/;
}
tree *left,*right;
};
tree Tree[length];
struct poster
{
int l,r;
};
poster pos[];
int hashb[hl];
int x[length];
int ntree=;
void builttree(tree *t,int l,int r)
{
t->l=l;
t->r=r;
t->tcover=false;
if(l==r) return ;
ntree++;
t->left=Tree+ntree;
ntree++;
t->right=Tree+ntree;
builttree(t->left,l,(l+r)/);
builttree(t->right,(l+r)/+,r);
}
bool post(tree *t,int l,int r)
{
if(t->tcover==true) return false;
if(t->l==l&&t->r==r){
t->tcover=true;
return true;
}
bool result;
if(r<=t->mid())
result=post(t->left,l,r);
else if(l>=t->mid()+)
result=post(t->right,l,r);
else
{
bool b1=post(t->left,l,t->mid());
bool b2=post(t->right,t->mid()+,r);
result=b1||b2;
}
if(t->left->tcover==true&&t->right->tcover==true)
t->tcover=true;
return result; }
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n;
ntree=;
scanf("%d",&n);
int accout=;
for(int i=;i<n;i++)
{
scanf("%d%d",&pos[i].l,&pos[i].r);
x[accout++]=pos[i].l;
x[accout++]=pos[i].r;
}
sort(x,x+accout);
int m=unique(x,x+accout)-x;
int treel=;
for(int i=;i<m;i++)
{
hashb[x[i]]=treel;
if(i<m-){
if(x[i+]-x[i]==)
treel++;
else treel+=;
}
}
builttree(Tree,,treel);
int sum=;
for(int i=n-;i>=;i--)
if(post(Tree,hashb[pos[i].l],hashb[pos[i].r]))
sum++;
printf("%d\n",sum); }
return ;
}
Mayor's posters (线段树加离散化)的更多相关文章
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- 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 ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- 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 ...
随机推荐
- OpenGL学习进程(8)第六课:点、边和图形(三)绘制图形
本节是OpenGL学习的第六个课时,下面介绍OpenGL图形的相关知识: (1)多边形的概念: 多边形是由多条线段首尾相连而形成的闭合区域.OpenGL规定,一个多边形必须是一个“凸多边形”. ...
- $《第一行代码:Android》读书笔记——第5章 Broadcast
(一)广播机制简介 1.Android广播的分类: 如图所示: 2.发送广播:使用Intent:接收广播:Broadcast Receiver. (二)接收系统广播 1.动态注册监听网络变化 示例程序 ...
- Word内容修改,以及转PDF
Word模板内容修改 1.java代码 package com.sicdt.sicsign.web.utils; import java.io.ByteArrayInputStream; import ...
- 【TopCoder】SRM160 DIV1总结
做了两道题之后才发现做的是DIV1,不是DIV2,DIV1的第二道题是DIV1的第三道题,果断决定第3题就不看了=.= 250分题:给定一个时间起点8:00 AM DAY 1,再给出一组时间终点,格式 ...
- 【Flask】sqlalchemy 排序
### 排序:1. order_by:可以指定根据这个表中的某个字段进行排序,如果在前面加了一个-,代表的是降序排序.2. 在模型定义的时候指定默认排序:有些时候,不想每次在查询的时候都指定排序的方式 ...
- Java中的HashMap
今天到中关村软件园面试被问到Java中HashMap的存值原理,瞬间无言已对,回答用了一个数组,然后沉默,面试官说,一次的面试失败不算什么,之后...... 1.关于hashCode hashCode ...
- docker link
什么是docker的link机制 同一个宿主机上的多个docker容器之间如果想进行通信,可以通过使用容器的ip地址来通信,也可以通过宿主机的ip加上容器暴露出的端口号来通信,前者会导致ip地址的硬编 ...
- Docker 数据管理-tmpfs mounts
Use tmpfs mounts Volumes and bind mounts are mounted into the container’s filesystem by default, and ...
- PHP开发环境MAMP for Windows
Windows上的整合PHP开发环境有很多,如:Windows上使用的Wampserver(http://www.wampserver.com/),跨平台的Xampp(https://www.apac ...
- Phoenix on HBase
(一)概要 Apache Phoenix是基于BSD许可开源的一个Java中间层,可以让开发者在Apache HBase上执行SQL查询.Apache Phoenix主要特性: 嵌入式的JDBC驱动, ...