POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)
POJ2528.Mayor's posters
这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知道离散化是什么东西之前,就已经用过这种东西了,只是不知道叫什么。关于离散化,就是根据数的相对大小对他们进行映射,用小的数来表示他们。所以要先排序,然后去重,然后操作就可以。有排序版的离散化和STL版的离散化(自己起的名字 。。。),代码里写的是排序版的,就是for循环遍历的时候,和身边的数比较一下是否是相同的数,然后操作。具体的不介绍,自行百度吧。
这道题就是贴海报,只与两个边界值有关系,但是如果两个数中间的间隔大于1的话,中间应该加数,否则新的点覆盖到上面的时候,就会出错。举个例子,假设我点为1,2,7,10,我离散化映射为0,1,2,3,区间操作为(1,10),(1,2),(7,10),因为线段树是对点进行维护,所以是对点染色,对点染色的话,就将两个点都改变了,所以1,2,7,10都变了颜色,只能看到2种,但是正确的应该是3种,(2,7)还能看到,所以需要在间隔大于1的时候中间加点,这样就不会出错了。映射的时候二分查找就会快很多,其他的就没什么了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
const double eps=1e-;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int col[maxn<<],hash[maxn<<],a[maxn],li[maxn],ri[maxn],cnt;
void pushdown(int rt)
{
if(col[rt]!=-){
col[rt<<]=col[rt<<|]=col[rt];
col[rt]=-;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R){
col[rt]=c;
return ;
} pushdown(rt);
int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
}
void query(int l,int r,int rt)
{
if(col[rt]!=-){
if(hash[col[rt]]==) cnt++;
hash[col[rt]]=;
return ;
} if(l==r) return ;
int m=(l+r)>>;
query(lson);
query(rson);
}
int reflect(int key,int n,int a[])
{
int l=,r=n-;
while(l<=r){
int m=(l+r)>>;
if(a[m]==key) return m;
if(a[m]< key) l=m+;
else r=m-;
}
return -;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int h=,k=;
memset(col,-,sizeof(col));
memset(hash,,sizeof(hash));
for(int i=;i<n;i++){
scanf("%d%d",&li[i],&ri[i]);
a[h++]=li[i];a[h++]=ri[i];
}
sort(a,a+h);
for(int i=;i<h;i++){
if(a[i]!=a[i-]) a[k++]=a[i];
}
for(int i=k-;i>;i--){
if(a[i]!=a[i-]+) a[k++]=a[i]+;
}
sort(a,a+k);
for(int i=;i<n;i++){
int l=reflect(li[i],k,a);
int r=reflect(ri[i],k,a);
update(l,r,i,,k,);
}
cnt=;
query(,k,);
printf("%d\n",cnt);
}
return ;
}
POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)的更多相关文章
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- 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: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- 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 (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
- poj 2528 Mayor's posters(线段树)
题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- POJ 2528 Mayor’s posters (线段树段替换 && 离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
随机推荐
- Java从后台重定向(redirect)到另一个项目的方法
(1)通过ModelAndView跳转 @RequestMapping("alipayforward") public ModelAndView alipayforward(Htt ...
- JavaSE总结--多线程
进程: 进程之间内存隔离,内存不共享. 线程: 可以共享内存. 每个线程都是一个栈. 多线程的好处: 1)防止程序阻塞. wait与notify的区别: 针对等待队列而言. wait:进入等待队列.必 ...
- .net 下word 中的图片与文字分离
最近在做一个项目要求word 中的图片与文字分离 ,找了好久终于找到一个完美的方法 c#实现word中的图文分离 part 1: class define Code highlighting pr ...
- 《Cracking the Coding Interview》——第18章:难题——题目5
2014-04-29 01:51 题目:你有一个文本文件,每行一个单词.给定两个单词,请找出这两个单词在文件中出现的其中一对位置,使得这两个位置的距离最短. 解法:我的思路是建立倒排索引,计算出所有单 ...
- .Net导出Word和Excel
using System; using System.Collections.Generic; using System.Linq; using System.Web; public class Ex ...
- 一些优秀的SLAM博主
http://blog.csdn.net/u010566411 http://blog.csdn.net/qq_18661939/article/details/51782376 http://www ...
- zabbixproxy安装
目录 1 zabbix3.2安装.... 1 2 安装proxy. 1 2.1 安装zabbix-proxy. 1 2.1.1 配置zabbix yum源.... 1 2.1 ...
- typescript语言
百度百科:2013年6月19日,在经历了一个预览版之后微软正式发布了正式版TypeScript 0.9
- 【Python】- 第一行跟第二行的写法
第一行:目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它,就这么简单 #!/usr/bin/python:告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: ...
- Mac平台重新设置MySQL的root密码
Mac OS X - 重置 MySQL Root 密码 您是否忘记了Mac OS 的MySQL的root密码? 通过以下4步就可重新设置新密码: 1. 停止 mysql server. 通常是在 ...