Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50888   Accepted: 14737

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, 10), (1, 6), (8, 10) 这样6和8之间,只有加点才会使7被考虑在内
 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int xx[], yy[];
int sor[], cnt, deal[], col[];
int ans, vis[];
int hi;
void hash() {
int last = sor[];
hi = ;
memset(deal, , sizeof(deal));
deal[++hi] = last;
for (int i = ; i <= cnt; i++ ) {
if (sor[i] == last) continue;
if (sor[i] - last > ) deal[++hi] = last + , deal[++hi] = sor[i], last = sor[i];
else deal[++hi] = sor[i], last = sor[i];
}
sort(deal + , deal + + hi);
return;
}
int Bsearch(int x) {
int low = , high = hi;
while (low <= high) {
int mid = low + high >> ;
if (deal[mid] == x) return mid;
if (deal[mid] < x) low = mid + ;
else high = mid - ;
}
return -;
}
void Push_down(int o) {
if (col[o] != -) {
col[o << ] = col[o << | ] = col[o];
col[o] = -;
}
}
void query(int o, int l, int r) {
if (col[o] != -) {
if (!vis[col[o]]) ans++;
vis[col[o]] = true;
return;
}
if (l == r) return;
int mid = (l + r) >> ;
query(o << , l, mid);
query(o << | , mid + , r);
}
void update(int o, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
col[o] = v;
return;
}
Push_down(o);
int mid = (l + r)>> ;
if (ql <= mid) update(o << , l, mid, ql, qr, v);
if (qr > mid) update(o << | , mid + , r, ql, qr, v);
}
int main() {
int t, n;
scanf("%d", &t);
while (t--) {
ans = ;
scanf("%d", &n);
cnt = ;
memset(sor, , sizeof(sor));
for (int i = ; i <= n; i++) {
scanf("%d%d", &xx[i], &yy[i]);
sor[++cnt] = xx[i], sor[++cnt] = yy[i];
}
sort(sor + , sor + cnt + );
hash();
memset(col, -, sizeof(col));
for (int i = ; i <= n; i++) {
int ql = Bsearch(xx[i]), qr = Bsearch(yy[i]);
update(, , hi, ql, qr, i);
}
memset(vis, , sizeof(vis));
query(, , hi);
printf("%d\n", ans);
}
return ;
}
												

poj2528 Mayor's posters(线段树区间覆盖)的更多相关文章

  1. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  2. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  3. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

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

  4. Mayor's posters 线段树区间覆盖

    题目链接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that t ...

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

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

  6. POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)

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

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

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

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

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

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

随机推荐

  1. DHCP详细工作过程(转)

    DHCP客户端通过和DHCP服务器的交互通讯以获得IP地址租约.为了从DHCP服务器获得一个IP地址,在标准情况下DHCP客户端和DHCP服务器之间会进行四次通讯.DHCP协议通讯使用端口UDP 67 ...

  2. hadoop伪分布式安装

    hadoop的伪分布安装:一台实体机或虚拟机的安装. 环境:Windows7.VMWare.CentOS 1.1 设置ip地址 说明:在CentOS中的网络的类型: 仅主机模式:虚拟机在Windows ...

  3. Servlet程序开发-- 取得其他内置对象

    servlet本身提供的只有request和response对象,如果要取得session对象,只能依靠request对象,因为session属于HTTP协议范畴,而且在每次发送请求的时候,服务器都会 ...

  4. jquery控制audio的播放与暂停

    <audio id="audio" src='music.mp3'></audio> <script type="type/javascri ...

  5. 设置HTML表格细边框

    简介:WEB前端|这是关于怎么设置HTML表格细边框的问题,把表格边框设置为细小的线条边框一般我们用表格的时候总会给它个border属性,比如:<tableborder="1" ...

  6. error: QApplication: No such file or directory

    尝试用Qt5编译Qt4的工程.你会遇到下面的问题: 错误:C1083: 无法打开包括文件:“QApplication”: No such file or directory 出现原因:Qt5里不再用Q ...

  7. [转]iptables

    iptables ptables简介 iptables是基于内核的防火墙,功能非常强大,iptables内置了filter,nat和mangle三张表. filter负责过滤数据包,包括的规则链有,i ...

  8. iOS LaunchScreen和LaunchImage的转换启动图

    今天开始设置一个新项目的启动图,需要我自己设置,我在UI那拿到以前格式的启动图不知道为何需要那么多图,我记得用LaunchScreen只需一张即可,利用自动布局,今天看到这么多图,发现他用的是Laun ...

  9. Java时间操作(一):关于UTC格式时间处理

    Java中获取形如:20160811T122132+08 格式,可以通过如下方法 package com.mc.others; public class UTCTimeTest { @Test pub ...

  10. strace 分析 跟踪 进程错误

    strace是什么? 按照strace官网的描述, strace是一个可用于诊断.调试和教学的Linux用户空间跟踪器.我们用它来监控用户空间进程和内核的交互,比如系统调用.信号传递.进程状态变更等. ...