题目链接:http://poj.org/problem?id=2528

Time Limit: 1000MS Memory Limit: 65536K

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

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

题意:

现有一面墙,墙上有一些格子,从1~n标号,给出m张海报的左右边界值,表示从l到r的格子上贴了海报。

按顺序海报一层层贴,会有覆盖,求所有海报贴完后,整面墙上露出多少张海报(不一定要露出整张海报,露出一部分也算)。

题解:

首先想到的是,区间修改线段树,但是看到l与r的范围,显然不能直接建立那么大范围的线段树;

看到海报数量不超过10000,便考虑进行离散化;

离散化后,我们给树初始化建树为0,表示没有海报,后续按照海报的编号(1~m)进行区间修改;

最后查询每个格子,只要大于0,就代表这个格子上有海报,进行计数,最后即可得答案;

otherwise,就像discuss里说的那样,例如case:

1
3
1 10
1 3
6 10

如果进行普通的离散化,会出现问题:

原本3到6之间露出会露出海报1,但是普通离散化后,3被离散化为2,6被离散化为3,之间的间隔消失,就无法露出海报1,就会产生错误答案;

故进行离散化时,idx[]数组除了加入l与r值外,再加入l-1与r+1(当然,其实只加入l-1也是可以的),使得即使在离散化后,每个海报间有一定的间隙;

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 40000+5
using namespace std;
typedef long long ll;
int m,a[MAXN];
int idx[MAXN];//离散化索引
struct Paint{int l,r;}paint[MAXN];
struct Node{
int l,r;
int val,lazy;
void update(ll x)
{
val=(r-l+)*x;
lazy=x;
}
}node[*MAXN];
void pushdown(int root)
{
if(node[root].lazy)
{
node[root*].update(node[root].lazy);
node[root*+].update(node[root].lazy);
node[root].lazy=;
}
}
void pushup(int root)
{
node[root].val=max(node[root*].val,node[root*+].val);
}
void build(int root,int l,int r)
{
node[root].l=l; node[root].r=r;
node[root].val=; node[root].lazy=;
if(l==r) node[root].val=a[l];
else
{
int mid=l+(r-l)/;
build(root*,l,mid);
build(root*+,mid+,r);
pushup(root);
}
}
void update(int root,int st,int ed,int val)
{
if(st>node[root].r || ed<node[root].l) return;
if(st<=node[root].l && node[root].r<=ed) node[root].update(val);
else
{
pushdown(root);
update(root*,st,ed,val);
update(root*+,st,ed,val);
pushup(root);
}
}
int query(int root,int st,int ed)
{
if(ed<node[root].l || node[root].r<st) return ;
if(st<=node[root].l && node[root].r<=ed) return node[root].val;
else
{
pushdown(root);
ll a=query(root*,st,ed);
ll b=query(root*+,st,ed);
pushup(root);
return max(a,b);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
memset(a,,sizeof(a));
int _size=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&paint[i].l,&paint[i].r);
idx[_size++]=paint[i].l;
idx[_size++]=paint[i].l-;
idx[_size++]=paint[i].r;
idx[_size++]=paint[i].r+;
}
sort(idx,idx+_size);
_size=unique(idx,idx+_size)-idx;
build(,,_size);//线段树建树
for(int i=;i<=m;i++)
{
int l=lower_bound(idx,idx+_size,paint[i].l)-idx+;
int r=lower_bound(idx,idx+_size,paint[i].r)-idx+;
//得到paint[i].l和r对应的离散化后的值
update(,l,r,i);
}
int cnt=;
for(int i=;i<=_size;i++)//遍历墙上每个格子
{
int tmp=query(,i,i);
if(tmp>) idx[cnt++]=tmp;//如果墙上贴了海报,就进行记录
}
sort(idx,idx+cnt);//排序后进行去重,去重后的数组的size即答案
printf("%d\n",unique(idx,idx+cnt)-idx);
}
}

PS.代码里,idx[]数组在完成了离散化任务后,被我重复利用了一下,存了每个墙格子最后贴了海报几;

POJ 2528 - Mayor's posters - [离散化+区间修改线段树]的更多相关文章

  1. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

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

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

  3. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

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

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

  5. POJ - 2528 Mayor's posters(dfs+分治)

    POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...

  6. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

  7. POJ:2528(Mayor's posters)离散化成段更新+简单哈希

    http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the ca ...

  8. POJ - 2528 Mayor's posters (离散化+线段树区间修改)

    https://cn.vjudge.net/problem/POJ-2528 题意 给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张? 分析 ...

  9. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

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

随机推荐

  1. SpringBoot------JPA连接数据库

    步骤: 1.在pom.xml文件下添加相应依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...

  2. SpringMVC由浅入深day01_7入门程序小结

    7 入门程序小结 通过入门程序理解springmvc前端控制器.处理器映射器.处理器适配器.视图解析器用法. 前端控制器配置: 处理器映射器: 非注解处理器映射器(了解) 注解的处理器映射器(掌握) ...

  3. UDP通信-UdpClient

    static void Main(string[] args) { Console.WriteLine("发送端"); byte[] buffer = System.Text.En ...

  4. [AX]AX2012 R2 HR Jobs, Positions, Department和Workers

    部门.作业(Job的官方翻译)和位置(Position的官方翻译)是AX人力资源管理的基本组织元素,Job和Position在AX有的地方又称作工作和职位,其实这个翻译更为恰当. Job定义的是一个工 ...

  5. PHP mysql经典问题,防止库存把控不足问题

    在目前这家公司做的第一个项目抽奖项目,要求每人每天可以有20次抽奖机会,抽奖机会可以通过多种方式获取,那么就要求每次入库增加抽奖机会的时候检测当前拥有的抽奖机会是否达到了20次,如果达到了,就不再增加 ...

  6. IIS中采用ISAPI-Rewrite防盗链

    本规则支持白名单排除式防盗链,搜索引擎友好(不屏蔽),被盗链后的错误提示转向,支持各种文件类型,经作者亲验真的能用,第一时间在itmop.com原创发表,请继续往下阅读. 近来小站遇到了盗链问题,至使 ...

  7. zabbix的启动和关闭脚本

    1. zabbix客户端的系统服务脚本 1.1 拷贝启动脚本 zabbix的源码提供了系统服务脚本,在/usr/local/src/zabbix-3.2.6/misc/init.d目录下,我的系统是C ...

  8. jQuery ajax中serialize()方法增加其他参数

    表单提交 使用jQuery.ajax()进行表单提交时,需要传递参数,最直接的方法便是使用Form的serializa()将表单序列化,前提只是将Form表单中的name属性与数据库的字段名保持一致便 ...

  9. Charles抓包(iOS的http/https请求)

    Charles抓包(iOS的http/https请求) Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Charles:https://www.charlesp ...

  10. .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?

    .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法,区别? RadioButton实例及说明: <asp:RadioButton ID=& ...