任意门:http://poj.org/problem?id=2528

Mayor's posters

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 77814   Accepted: 22404

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

Source

题目大意:

贴海报,给出每张海报的起点和终点,问最后能看见多少张海报(后来的海报会把前面的覆盖);

解题思路:

海报的数据范围太大了,如果按照海报的数据范围开线段树会放不下而且浪费内存。所以考虑离散化,但是这不同于单点离散化,这是区间离散化,意思就是说离散化原来数据的同时还要保留他们的区间的性质。(大神的栗子:原数据“1-10, 1-4,6-10”,常规单点离散化之后“1-4,1-2,3-4”,原本不相邻的点变成邻居了,如果继续按照这种错误的数据建树,那么后果是直接失去了区间的性质) 如果我们想缩小这两点的数据范围,但又想保留不相邻两点表示一个区间的性质,区间离散化要这样搞:缩小数据范围就是按原本数据大小进行排序,他们的先后性保持不变,新下标暂时替代他们,而保留不相邻两点的性质嘛,小脑洞:在他们之间加一个值把他们隔离,这样他们排序之后就不会相邻啦。

离散化之后就还是常规的线段树区间更新,但最后求解的区间查询操作如果暴力单点查询找出有几种不同的海报这样太委屈线段树了,这里的查询只要这张海报类型出现了,那么我们的答案肯定就要加加,然后这张海报我们就可以book一下,下次如果见到有book的海报类型也不需要再更新答案了,这些判重的操作可以直接在Query()里面做,这样才能更好的发挥线段树的本领。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long int
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
using namespace std;
const int MAXN = 1e4+;
struct date
{
int L, R;
}node[MAXN];
int num[MAXN<<];
int lazy_tag[MAXN<<];
bool book[MAXN<<];
int res, cnt;
int read()
{
int f=, res=;char s=getchar();
while(s<''||s>''){if(s=='-')f=-;s=getchar();}
while(s>=''&&s<=''){res=res*+s-'';s=getchar();}
res*=f;
return res;
}
void PushD(int root)
{
if(lazy_tag[root]){
lazy_tag[root<<] = lazy_tag[root];
lazy_tag[root<<|] = lazy_tag[root];
lazy_tag[root] = ;
}
} void Update(int st, int ed, int l, int r, int root, int val)
{
if(st <= l && ed >= r){lazy_tag[root] = val; return;}
PushD(root);
int mid = (l+r)>>;
if(st <= mid) Update(st, ed, lson, val);
if(ed > mid) Update(st, ed, rson, val);
} void Query(int l, int r, int root)
{
if(lazy_tag[root]){
if(!book[lazy_tag[root]]) res++;
book[lazy_tag[root]] = true;
return;
}
if(l == r) return;
int mid = (l+r)>>;
Query(lson); Query(rson);
}
void init()
{
cnt = ; res = ;
memset(lazy_tag, , sizeof(lazy_tag));
memset(book, false, sizeof(book));
}
int main()
{
int T_case, N;
T_case = read();
while(T_case--)
{
init();
N = read();
for(int i = ; i < N; i++){
node[i].L = read();node[i].R = read();
num[cnt++] = node[i].L;
num[cnt++] = node[i].R;
}
sort(num, num+cnt);
cnt = unique(num, num+cnt)-num;
int top = cnt;
for(int i = ; i < top-; i++){
if(num[i+]!=num[i]+) num[cnt++] = num[i]+;
} sort(num, num+cnt);
/*
for(int i = 0; i < cnt; i++)
printf("%d ", num[i]);
puts("");
*/
for(int i = ; i < N; i++){
int st = lower_bound(num, num+cnt, node[i].L)-num;
int ed = lower_bound(num, num+cnt, node[i].R)-num;
Update(st+, ed+, , cnt, , i+);
}
Query(, cnt, );
printf("%d\n", res);
}
return ;
}

POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】的更多相关文章

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

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

  2. POJ 2528 Mayor's posters 贴海报 线段树 区间更新

    注意离散化!!!线段树的叶子结点代表的是一段!!! 给出下面两个简单的例子应该能体现普通离散化的缺陷: 1-10 1-4 5-10 1-10 1-4 6-10 普通离散化算出来的结果都会是2,但是第二 ...

  3. Mayor's posters (离散化线段树+对lazy的理解)

    题目 题意: n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围 li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 思路: 由于 ...

  4. POJ - 2528Mayor's posters (离散化+线段树区间覆盖)

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  5. [POJ2528]Mayor's posters(离散化+线段树)

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

  6. poj 2528 Mayor’s posters 【离散化】+【线段树】

    <题目链接> 题目大意: 往一堵墙上贴海报,依次输出这些海报张贴的范围,这些海报能够相互覆盖,问最后能够看见几张海报? 解题分析: 由于是给出每张海报的区间,所以在这些区间内的很多点可能用 ...

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

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

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

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

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

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

随机推荐

  1. OSG DB的插件地址设置

    今天搞了一整天OSG,结果每次都说could not find plugin,就是说找不到OSG的插件去加载文件,我大概看了下OSG的插件机制,发现他是用插件的形式下去读取文件的 http://blo ...

  2. Murano Weekly Meeting 2016.08.16

    Meeting time: 2016.August.16 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: ...

  3. nginx location 配置阐述优先级别使用说明

    使用nginx 有大半年了,它的高性能,稳定性表现很好. 这里也得到很多人的认可. 其中它的配置,有点像写程序一样,每行命令结尾一个";"号,语句块用"{}"括 ...

  4. 九度oj题目1012:畅通工程

    题目1012:畅通工程 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6643 解决:2863 题目描述: 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇. ...

  5. Windows 下推荐软件

    神器 Dism++ Quicker(效率工具) Bandizip 火绒安全软件 Everyting(搜索神器并支持http远程连接) Xmanager VMware Workstation IDMan ...

  6. HDU 5289——Assignment——————【RMQ+优化求解】

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. 转帖:kindeditor编辑区空格被隐藏,导致所见所得不一致的解决办法

    1.修改kindereditor-all.js中的 var re = /(\s)<(/)?([\w-:]+)((?:\s+|(?:\s+[\w-:]+)|(?:\s+[\w-:]+=[^\s&q ...

  8. Spring3.2下使用JavaMailSenderImpl类发送邮件

    1.JavaMailSenderImpl类 Spring的邮件发送的核心是MailSender接口,在Spring3.0中提供了一个实现类JavaMailSenderImpl,这个类是发送邮件的核心类 ...

  9. 2、Spring之AOP

    AOP术语 通知:定义了切面是什么以及何时使用.除了要描述页面要完成的工作,通知还解决了何时执行这个工作的问题. 连接点:连接点是一个物理的存在.这个点可以是调用方法时.抛出异常时.甚至是修改一个字段 ...

  10. 提取url中参数的方法(转换成json格式)

    还是直接上代码吧. //将url中的参数获取到并抓换成json格式 function serilizeUrl(url){ var urlObject={}; //1.正则匹配是不是以?结尾 if(/\ ...