题解 UVA10587 【Mayor's posters】
先讲一下:dalao @lisuier 发布的前一篇题解严格来讲是有错误的
比如下一组数据:
1
3
1 10
1 4
7 10
显然答案是3,然而用lisuier dalao的程序做出来的答案是2(后面会讲错误原因)
简单看出这道题用线段树可解
so
我们用离散化+权值线段树(戳这里详解)
实际上是安利自己博客
思路:建一棵空数,然后把某一区间的颜色更新为读入的颜色;
WA,SO EASY
OK
那我们先建一棵(10^7*4)的空树
然后
空间就炸了
正经的处理方法
对区间端点进行离散化
接下来
引用一下的 @lisuier 的话
离散化,如下面的例子,因为单位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
3
1 10
1 4
7 10
用该方法离散化后
第二张海报与第三张海报中间的间隔就消...消失了
也就是说第一张海报就看不到了(手动模拟一下发现是能看到的)
处理方法:离散化时,加到临时数组b中的右端点+1也加到临时数组中
看起来是这样的
int init(){//读入并进行离散处理
n = read(); tot=0;
for(int i = 1;i <= n;i++)
a[i].l = read(),a[i].r = read(),
b[++tot] = a[i].l,b[++tot] = a[i].r,b[++tot] = a[i].r + 1;//加入右边的端点+1
sort(b + 1,b + tot + 1);
int len=unique(b + 1,b + tot + 1) - b - 1;
for(int i = 1; i <= n;i++)
a[i].l = lower_bound(b + 1,b + len + 1,a[i].l) - b,
a[i].r = lower_bound(b + 1,b + len + 1,a[i].r) - b; //下面是正常的离散化
return len; //离散化后总共处理多长的墙;
}
更新之类的与普通线段树差不多
但是要注意push_down操作和query操作
比如说询问时已经访问过得颜色要标记一下
接下来是
简单易懂
的代码.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define M 20005
using namespace std;
inline int read(){
char chr=getchar(); int f=1,ans=0;
while(!isdigit(chr)) {if(chr=='-') f=-1;chr=getchar();}
while(isdigit(chr)) {ans=ans*10;ans+=chr-'0';chr=getchar();}
return ans*f;
}
int ans = 0;
struct segment
{
int l,r;
}a[10005 << 4];
bool vis[20005 << 4];
struct node
{
int l,r,val,lazy,sum;
int mid()
{
return l + r >> 1;
}
}t[M << 4];
int b[20005 << 4],n,tot=0,x,y;
int init()
{//读入并进行离散处理
n = read();
tot = 0;
for(int i = 1;i <= n;i++)
a[i].l = read(),
a[i].r = read(),
b[++tot] = a[i].l,
b[++tot] = a[i].r,
b[++tot] = a[i].r + 1;
sort(b + 1,b + tot + 1);
int len=unique(b + 1,b + tot + 1) - b - 1;
for(int i = 1; i <= n;i++)
a[i].l = lower_bound(b + 1,b + len + 1,a[i].l) - b,
a[i].r = lower_bound(b + 1,b + len + 1,a[i].r) - b;
return len; //离散化后总共处理多长的墙;
}
void push_down(int i){
if(t[i].val == -1) return;
t[i << 1].val = t[i << 1 | 1].val = t[i].val;
t[i].val = -1;
}
void build(int i,int l,int r)
{
t[i].l = l;
t[i].r = r;
t[i].val = 0;
if(l == r)
{
return;
}
int m=t[i].mid();
build(i << 1,l,m);
build(i << 1 | 1,m + 1,r);
}
void updata(int i,int l,int r,int x)
{
if(l <= t[i].l && t[i].r <= r)
{
t[i].val = x;
return;
}
push_down(i);
int m = t[i].mid();
if(l <= m)
updata(i << 1,l,r,x);
if(r > m)
updata(i << 1 | 1,l,r,x);
}
void query(int i,int l,int r)
{
if(t[i].val != -1)
{
if(!vis[t[i].val])
{
vis[t[i].val] = 1;//做标记
++ans;
}
return;
}
query(i << 1,l,r);
query(i << 1 | 1,l,r);
}
int ask(int l,int r)
{
memset(vis,0,sizeof(vis));
ans = 0;
vis[0] = 1;
query(1,l,r);
return ans;
}
int main()
{
int T = read();
while(T--)
{
int m=init(); tot=0;//海报染成的颜色
build(1,1,m);
for(int i = 1;i <= n;i++)
updata(1,a[i].l,a[i].r,++tot);
printf("%d\n",ask(1,m));
}
return 0;
}
题解 UVA10587 【Mayor's posters】的更多相关文章
- POJ2528 Uva10587 Mayor's posters
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj2528 Mayor's posters(线段树之成段更新)
Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...
- 【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 ...
- 线段树---poj2528 Mayor’s posters【成段替换|离散化】
poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- 【poj2528】Mayor's posters
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 64939 Accepted: 18770 ...
- POJ 2528 Mayor's posters
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
随机推荐
- git 缓存密码导致的不能和远程仓库交互unable to access... 403错误
尝试了各种方式,包括卸载等最终解决方案: 查看本机的credential 是否已经被清空. 如果输入了 git config credential.helper 命令之后没有输出,说明 git 的配置 ...
- kswapd和pdflush
首 先,它们存在的目的不同,kswap的作用是管理内存,pdflush的作用是同步内存和磁盘,当然因为数据写入磁盘前可能会换存在内存,这些缓存真正写 入磁盘由三个原因趋势:1.用户要求缓存马上写入磁盘 ...
- java 同时安装多版本问题
java 同时安装多版本问题(转) http://www.cnblogs.com/SamuelSun/p/6022296.html http://blog.csdn.net/u013256622/ar ...
- eclipse 快捷键及使用技巧
一.程序的编译和运行的环境配置(一般不改) window -- Preferences -- Java 编译环境:Compiler 默认选中的就是最高版本. 运行环境:Installed JREs 默 ...
- scp相关命令总结
scp 跨机远程拷贝scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.当你服务器硬盘 ...
- [TyvjP1515] 子串统计 [luoguP2408] 不同子串个数(后缀数组)
Tyvj传送门 luogu传送门 经典题 统计一个字符串中不同子串的个数 一个字符串中的所有子串就是所有后缀的前缀 先求出后缀数组,求出后缀数组中相邻两后缀的 lcp 那么按照后缀数组中的顺序遍历求解 ...
- bootstrap-table与Spring项目集成实例收集
bootstrap-table项目官网:https://github.com/wenzhixin/bootstrap-table bootstrap-table各版本下载:https://github ...
- 查看当前Java进程工具jps(转)
jps是JDK提供的一个查看当前Java进程的小工具, 可以看做是JavaVirtual Machine Process Status Tool的缩写.非常简单实用. 命令格式: jps [optio ...
- 2015 年度新增开源软件排名TOP100
本榜单包括 2015 年开源中国新收录的 5977 款开源软件中,依据软件本身的关注度.活跃程度进行排名前 100 名的软件.从这份榜单中也许能够了解到最新业界的趋势. 1.SwitchyOmega ...
- 多个结果显示成一个group_concat函数
需求:获取班级.课程中文名.老师 扩展:一个班级一门课程,老师可能多个,想把多个教师显示成在一个结果里 解决方案:加个group by 参考资料:https://www.cnblogs.com/zhu ...