任意门: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. mysql 命令笔记

    添加密码 mysqladmin -uroot -p password 123456 创建用户只能在10.0.0.0网段下访问数据库grant select,create,insert,update o ...

  2. python从字符串内取两个符号之间的内容

    #取字符串中两个符号之间的东东 def txt_wrap_by(self,start_str, end, html): start = html.find(start_str) if start &g ...

  3. BNU29140——Taiko taiko——————【概率题、规律题】

    Taiko taiko Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class ...

  4. 记自己的hexo个人博客

    https://othercoding.github.io/

  5. sqlite3在别的目录写文件的问题

    今天碰到一个文件,就是sqlite数据不能把db创建在别的目录下.找了好久不得其解.后来换了一个sqlite jar包就好了. 原来我用的是sqlite-nested 内嵌的jar包. 换成这里的包h ...

  6. js字符操作

    js字符串方法预览: fromCharCode(num1, num2,,,), charAt(), charCodeAt(), length, split(''), slice(start, end? ...

  7. IIS发布常见错误-HTTP 错误 404.0 - Not-Found

    错误信息:HTTP 错误 404.0 - Not-Found 错误代码:0x80070002 原 因:IIS配置错误. 解决方法:我配置IIS时漏掉了下面几项,一定要记得勾选.

  8. Java基础入门 - 简介

    官网:https://www.oracle.com Java分为三个体系: JavaEE: Java Platform, Enterprise Edition, Java平台企业版 JavaSE: J ...

  9. Protocol Buffers序列化原理

    1. 使用Varint编码数据,越小的数据,用少的字节编码.如小于128的用一个字节编码,大于128的用多个字节编码.同时,每个字节最高为1或者0表示是否为数字的一部分. 2. 由于负数的补码表示很大 ...

  10. initBinder转换日期格式

    @Controller public class FirstController { @RequestMapping("/first") //类型转化工作一定是在真正的handle ...