题解 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, 如果利用线段树求解的话,很明显 ...
随机推荐
- win10系统安装postgresql后无法连接
win10系统安装postgresql后在系统服务列表中找不到,连接不上数据库. 可以尝试关闭系统防火墙后重启电脑或者重装程序.
- B+树知识点
B+树介绍 目录 B+树 B+树的插入操作 B+树的删除操作 回到顶部 B+树 B+树和二叉树.平衡二叉树一样,都是经典的数据结构.B+树由B树和索引顺序访问方法(ISAM,是不是很熟悉?对,这也 ...
- Unity中确定时间是否在一定范围内
NowTime = DateTime.Now.ToLocalTime(); Timeyear = DateTime.Now.ToLocalTime().ToString("yyyy-MM-d ...
- 12. tie_breaker的使用原因和使用方法
主要知识点: tie_breaker的使用原因和使用方法 一.tie_breaker的使用原因 dis_max,只是取分数最高的那个query的分数而已,完全不考虑其他query的分数 ...
- 【转载】Apache shutdown unexpectedly启动错误解决方法
http://blog.csdn.net/dong123dddd/article/details/21372179 xampp启动时显示的错误为: 9:52:41 [Apache] Attempti ...
- echarts demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ubuntu16.04安装jdk/mysql/tomcat (使用apt-get命令)
安装jdk 更新系统安装包缓存,并且安装OpenJDK8 sudo apt-get update sudo apt-get install openjdk-8-jdk 检查jdk版本 java -ve ...
- ZOJ 3199 Longest Repeated Substring
Longest Repeated Substring Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on Z ...
- qwb与小数
qwb与小数 Time Limit: 1 Sec Memory Limit: 128 MB Description qwb遇到了一个问题:将分数a/b化为小数后,小数点后第n位的数字是多少? 做了那 ...
- H - Seek the Name, Seek the Fame
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...