http://poj.org/problem?id=2528

https://www.luogu.org/problem/UVA10587

Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • 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

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input. 

Sample Input


Sample Output

 

题目大意:

给你一个无限长的板子,n(n<=10000)个人依次贴n张等高的海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000),求出最后还能看见多少张海报。

题解:

一看线段树没跑了对吧? 就像染色一样,贴一张海报就把那一段染上新颜色, 这就是个很明显的区间修改嘛, 最后统计线段上有多少种不同颜色输出答案就OK了。

但是要注意线段树区间更新问题,给的长度的可能非常大,有1e9,不加处理直接维护一个线段树肯定会MLE,TLE,

但是我们注意到一共最多只有2e4个点,因此我们可以用离散化的思想先对区间进行预处理,所谓的离散化,

在我理解看来就是将一个很大的区间映射为一个很小的区间,而不改变原有的大小覆盖关系,但是注意简单的离散化可能

会出现错误,给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖

解决的办法则是对于距离大于1的两相邻点,中间再插入一个点。

下面的离散化方法摘自别处(分不清哪位大佬是原创。。。反正不是本蒟蒻):

解法:离散化,如下面的例子(题目的样例),因为单位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。

解这题特意去看了下STL中的unique函数(from:https://www.cnblogs.com/wangkundentisy/p/9033782.html

unique函数属于STL中比较常用函数,它的功能是元素去重。即”删除”序列中所有相邻的重复元素(只保留一个)。

此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了。由于它”删除”的是相邻的重复元素,所以在使用unique函数之前,一般都会将目标序列进行排序。

unique函数的函数原型如下:

只有两个参数,且参数类型都是迭代器:

iterator unique(iterator it_1,iterator it_2);

这种类型的unique函数是我们最常用的形式。其中这两个参数表示对容器中[it_1,it_2)范围的元素进行去重(注:区间是前闭后开,即不包含it_2所指的元素),返回值是一个迭代器,它指向的是去重后容器中不重复序列的最后一个元素的下一个元素

unique函数通常和erase函数一起使用,来达到删除重复元素的目的。(注:此处的删除是真正的删除,即从容器中去除重复的元素,容器的长度也发生了变换;而单纯的使用unique函数的话,容器的长度并没有发生变化,只是元素的位置发生了变化)

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); struct node
{
int l;
int r;
int color;
}SegTree[<<]; struct Segment
{
int l;
int r;
}A[];
int B[];
int vis[<<];
int T,n,tot,cnt,ans; int init()//初始化,离散化
{
scanf("%d",&n);
tot=;//tot为离散化的计数器
for(int i=;i<=n;i++)
{
scanf("%d %d",&A[i].l,&A[i].r);
B[++tot]=A[i].l;
B[++tot]=A[i].r;
B[++tot]=A[i].r+;//完善后的离散化多加的一点
}
sort(B+,B++tot);
int len=unique(B+,B++tot)-B-;//去除重点,求出规格
for(int i=;i<=n;i++)//重新算出每个贴纸的区间
{
A[i].l=lower_bound(B+,B++len,A[i].l)-B;
A[i].r=lower_bound(B+,B++len,A[i].r)-B;
}
return len;
} void PushDown(int rt)
{
if(SegTree[rt].color!=-)
{
SegTree[rt<<].color=SegTree[rt].color;
SegTree[rt<<|].color=SegTree[rt].color;
SegTree[rt].color=-;
}
} void Build(int l,int r,int rt)
{
SegTree[rt].l=l;
SegTree[rt].r=r;
SegTree[rt].color=;
if(l==r)
{
return;
}
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
} void Update(int L,int R,int color,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)
{
SegTree[rt].color=color;
return ;
}
PushDown(rt);
int mid=(l+r)>>;
if(L<=mid)
Update(L,R,color,rt<<);
if(R>mid)
Update(L,R,color,rt<<|);
} void Query(int L,int R,int rt)
{
if(SegTree[rt].color!=-)
{
if(!vis[SegTree[rt].color])
{
ans++;
vis[SegTree[rt].color]=;
}
return;
}
Query(L,R,rt<<);
Query(L,R,rt<<|);
} int main()
{
scanf("%d",&T);
while(T--)
{
int len=init();
cnt=;//区间染色时的计数器
Build(,len,);//建树
for(int i=;i<=n;i++)//染色
{
Update(A[i].l,A[i].r,++cnt,);
}
memset(vis,,sizeof(vis));
ans=;
vis[]=;
Query(,len,);
printf("%d\n",ans);
}
return ;
}

据说可以用Chtholly Tree珂朵莉树做,反正我不会。。。

珂朵莉树详解:https://www.luogu.org/blog/ACdreamer/chtholly-tree

该题的ODT做法可在这里找https://www.luogu.org/problemnew/solution/UVA10587

POJ-2528 Mayor's posters(线段树区间更新+离散化)的更多相关文章

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

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

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

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

  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 (线段树+区间覆盖+离散化)

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

  5. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  6. POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)

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

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

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

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

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

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

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

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

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

随机推荐

  1. python numpy 矩阵左右翻转/上下翻转

    numpy API: flattened flip() (in module numpy) fliplr() (in module numpy) flipud() (in module numpy) ...

  2. [极客大挑战 2019]Upload

    0x00 知识点 一个常规上传题目,知识点全都来自前几天写的文章: https://www.cnblogs.com/wangtanzhi/p/12243206.html 1:某些情况下绕过后缀名检测: ...

  3. Python csv文件操作

    一.open文件打开和with open as 文件打开的区别 file= open("test.txt","r") try: for line in file ...

  4. luogu P4219 [BJOI2014]大融合

    题解:原来LCT也能维护子树信息,我太Naive了 用LCT维护当前子树节点个数 具体做法维护siz[x]=当前Splay子树和指向当前Splay子树的虚边所代表的节点个数 auxsiz[x]=指向x ...

  5. Nacos快速开始

    Nacos是一个服务发现.配置管理和服务管理的组件. 说到服务注册与发现,我想到Eureka.Zookeeper 说到服务治理,我想到Dubbo 说到配置管理,我想到Apollo 作为后起之秀的Nac ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习:弹窗

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> ...

  7. javaweb01

    Java web应用由一组servlet.HTML页,类,以及它可以被绑定的资源构成,它可以在各种供应商提供的实现servlet规范容器中运行javaweb包括这些:Servlet jsp 实用类 静 ...

  8. OpenMP笔记(四)

    个人博客地址:http://www.bearoom.xyz/2019/02/22/openmp4/ 一.private private子句用于将一个或多个变量声明成线程私有的变量,这样每个线程都有该变 ...

  9. 17.3.12--time模块

    import time  #导入time模块 应用的时候有两种方式来表示时间: 1)时间戳 2)格式化的时间str(字符串)   3)元祖(struct_time)以及calendar 2---tim ...

  10. SpringMVC在使用JSON时报错信息为:Content type 'application/json;charset=UTF-8' not supported

    直接原因是:我的(maven)项目parent父工程pom.xml缺少必要的三个jar包依赖坐标. 解决方法是:在web子模块的pom.xml里面添加springMVC使用JSON实现AJAX请求. ...