任意门: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. while循环案例

    class While05{ public static void main(String[ ]args){ //练习1:使用while循环完成输出1------10中的每个数 /*int i =1; ...

  2. SpringMvc上传文件遇到重复读取InputStream的问题

    文件上传配置: <bean id="multipartResolver" class="org.springframework.web.multipart.comm ...

  3. innosetup卸载软件后,删除定时任务schedule task

    代码如下: //innosetup自带的方法,当卸载软件时,根据卸载的状态改变时而触发 procedure CurUninstallStepChanged(CurUninstallStep: TUni ...

  4. 设置Log文件每天生成一个(wamp)

    打开 Wamp的 httpd.conf文件 把下面两句话拷贝进去即可: 1.设置错误log的,    " 2.设置访问log的    " common    说明:bin/rota ...

  5. ubuntu安装卸载软件

    sudo apt-get remove nagios3 #卸载软件 sudo apt-get autoremove #卸载依附软件包 rpm格式 安装:rpm -ivh *** 查看:rpm -q * ...

  6. 使用request与正则表达式爬取bangumi动画排行榜

    import json import requests from requests.exceptions import RequestException import re import time d ...

  7. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  8. 【Elasticsearch】深入Elasticsearch集群

    7.1 节点发现启动Elasticsearch的时候,该节点会寻找有相同集群名字且课件的主节点,如果有加入,没有自己成为主节点,负责发现的模块两个目的 选出主节点以及发现集群的新节点7.1.1发现的类 ...

  9. mysql通过数据文件恢复数据方法

    情况描述:服务器硬盘损坏,服务器换了个新硬盘 ,然后老硬盘插在上面.挂载在这台机器.可以从老硬盘里面拿到数据.只拿到了里面的mysql数据文件夹,把数据文件夹覆盖新的服务器mysql数据文件夹 启动报 ...

  10. webpack+babel+react操作小结

    最近学习了一下Webpack,个人感觉还是非常实用的,现在总结一下自己的学习笔记. 什么是 Webpack Webpack 是一个模块打包器.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定 ...