任意门: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. Android NDK 安装与配置

    本文主内容: 1.  Android NDK 安装 2.  安装Cygwin与使用NDK编译 3.  在Eclipse中集成C/C++开发环境CDT 4.  安装Sequoyah插件 5.  JNI编 ...

  2. mavne 工程jsp页面首行报错

    在pom.xml文件中加入servlet依赖 <dependency> <groupId>javax.servlet</groupId> <artifactI ...

  3. ebiao 报表工具使用入门

    一.ebiao简价 e表是一个功能强大的Web报表工具,可使复杂报表的设计简单化,避免了大量的复杂SQL编写以及编程来准备数据,报表设计的效率大大提高.e表分为e表 for .NET和e表 for J ...

  4. [转] 微信小程序 页面跳转 传递参数

    本文转自:http://blog.csdn.net/qq_31383345/article/details/52795212 微信小程序的页面跳转,页面之间传递参数笔记. CSDN微信小程序开发专栏, ...

  5. MVC页面简单post提交

    页面代码 <script src="~/Scripts/jquery-1.10.2.js"></script> <script> $(funct ...

  6. 自己用jquery+css+div写的一个弹窗

    弹窗支持两种模式,一种是普通信息提示框,调用方法:popup.msgPopup(msg); 另一种是可以加载页面的弹窗,调用方法:popup.pagePopup(url); 效果图: css代码 ;; ...

  7. HttpClient请求工具类

    package com.yangche.utils; import org.apache.http.NameValuePair; import org.apache.http.client.Clien ...

  8. ATL模板库中的OLEDB与ADO

    上次将OLEDB的所有内容基本上都说完了,从之前的示例上来看OLEDB中有许多变量的定义,什么结果集对象.session对象.命令对象,还有各种缓冲等等,总体上来说直接使用OLEDB写程序很麻烦,用很 ...

  9. css之margin,padding的百分比

    注意:上下内边距与左右内边距一致:即上下内边距的百分数会相对于父元素宽度设置,而不是相对于高度. PS:而且是基于父元素内容的宽度(width属性的大小),不是基于父元素整个框架的宽度

  10. Django的模型层

    一.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...