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)的更多相关文章

  1. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  2. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  3. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  4. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  5. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  6. POJ 2528 Mayor's posters (线段树)

    题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...

  7. poj 2528 Mayor's posters(线段树)

    题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  9. POJ 2528 Mayor's posters (线段树+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:75394   Accepted: 21747 ...

  10. POJ 2528 Mayor’s posters (线段树段替换 && 离散化)

    题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...

随机推荐

  1. 剑指Offer - 九度1503 - 二叉搜索树与双向链表

    剑指Offer - 九度1503 - 二叉搜索树与双向链表2014-02-05 23:39 题目描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树 ...

  2. CodeIgniter学习笔记四:CI中的URL相关函数,路由,伪静态,去掉index.php

    一.URL相关函数 1.加载url模块 加载url有两种方式: a.自动加载:在 application/config/autoload.php 中开启 $autoload['helper'] = a ...

  3. 【Kernel Logistic Regression】林轩田机器学习技术

    最近求职真慌,一方面要看机器学习,一方面还刷代码.还是静下心继续看看课程,因为觉得实在讲的太好了.能求啥样搬砖工作就随缘吧. 这节课的核心就在如何把kernel trick到logistic regr ...

  4. Intellij 设置只更新静态文件(js、view、css)的方法

    1.打开 Tomcat Run/Debug configuration 2.打开Deployment标签 3.在“Deploy at Server Startup” 中,移出现有的.war 包 4.点 ...

  5. python之列表/元组/字典/字符串

    一.列表 格式:list = ['xxx','xxx','xxx'] 性质:可以修改列表内容 copy用法: import copy names = ['] names01 = names #直接引用 ...

  6. linux->centos7设置tomcat开机自启

    找到/etc/rc.d/文件下的rc.local,添加如下内容 export JAVA_HOME=/usr/local/jdk1.8.0_144export JRE_HOME=$JAVA_HOME/j ...

  7. 查看Linux系统用户登录日志

    日志: /var/log/wtmp说明: 此文件是二进制文件,查看方法如下:该日志文件永久记录每个用户登录.注销及系统的启动.停机的事件.因此随着系统正常运行时间的增加,该文件的大小也会越来越大,增加 ...

  8. 孤荷凌寒自学python第四十三天python 的线程同步之Queue对象

     孤荷凌寒自学python第四十三天python的线程同步之Queue对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Queue对象是直接操作队列池的对象,队列中可以存放多种对象,当然也 ...

  9. [Leetcode/Javascript] 461.Hamming Distance

    [Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...

  10. Linux开启MySQL远程连接

    Linux开启MySQL远程连接的设置步骤 . MySQL默认root用户只能本地访问,不能远程连接管理MySQL数据库,那么Linux下如何开启MySQL远程连接?设置步骤如下: 1.GRANT命令 ...