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. POJ - 3061 Subsequence(连续子序列和>=s的最短子序列长度)

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  2. Oracle Sql优化之范围处理

    1.表中字段自关联与分析函数的性能比较,自关联需要扫描表两次,分析函数扫描一次即可 ----自关联 select v1.proj_id,v1.proj_start,v1.proj_end from v ...

  3. 百度地图移动版API 1.2.2版本(Android)地图偏移的最佳解决办法

    Import import com.baidu.mapapi.CoordinateConvert;import com.baidu.mapapi.GeoPoint; Code GeoPoint p = ...

  4. 剑指offer 重建二叉树

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  5. JavaBean--简介及基本使用

    JavaBean本身就是一个类,属于java的面向对象编程 JavaBean是使用java语言开发的一个可重用的组建,在JSP开发中如果要应用JSP提供的JavaBean标签来操作简单的类的话,需要满 ...

  6. 无法加载shockwave flash

    热心网友 360浏览器的话,浏览器——工具——选项(非Internet选项)——高级设置——FLASH, 默认使用PPAPI Flash(需要重启浏览器) 默认使用NPAPI Flash(需要重启浏览 ...

  7. RocketMQ源码 — 三、 Producer消息发送过程

    Producer 消息发送 producer start producer启动过程如下图 public void start(final boolean startFactory) throws MQ ...

  8. 使用compass更高效的编辑css --- 图片精灵

    compass是sass的一个库,关系相当于js中的jq.比较可惜的是compass的文档比较混乱 看的不太舒服,下面是compass的文档地址,有兴趣的可以去看看 http://compass-st ...

  9. 关于数据结构的10个面试题(c语言实现)

    关于数据结构的10个面试题(c语言实现) 2010-04-21 22:17 5702人阅读 评论(0) 收藏 举报 数据结构面试c语言bttree 1.         输入一个链表的头结点,从尾到头 ...

  10. Python3基础 list(enumerate()) 将一个列表的每一个元素转换成 带索引值的元组

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...