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. 空指针错误 java.lang.NullPointerException

    使用基本的JAVA数据类型,变量的值要么已经是默认值,如果没有对其正常赋值,程序便 不能通过编译,因此使用基本的JAVA数据类型(double,float,boolean,char,int,long) ...

  2. hdu_3549_Flow Problem(最大流)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意:求1到n的最大流 题解:模版题,直接上Claris的ISAP,效率是一般dfs的十倍,OR ...

  3. Linux下find命令用法小结

    find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. find命令的格式:find [-path……] -options [-print -exec -ok] path ...

  4. MyEclipse8.5 无法安装ADT解决办法

    打开MYECLIPSE.点击菜单栏的help ->my eclipse configure center .然后add site  指向 https://dl-ssl.google.com/an ...

  5. 洛谷 U4704 函数

    设gcd(a,b)为a和b的最大公约数,xor(a,b)为a异或b的结果. 题目描述 kkk总是把gcd写成xor.今天数学考试恰好出到了gcd(a,b)=?这样的题目,但是kkk全部理解成了xor( ...

  6. Dice Possibility

    Dice Possibility 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice and th ...

  7. zepto学习之路--源代码提取

    最近在看zepto的源代码,把一些有用的函数摘出来,看看zepto是怎么实现的,自己做的时候也可以用.说实话,zepto的实现有一些看起来还是很晦涩的,可能是自己的水平不够,看不透作者的真正的意图. ...

  8. armstrong's programming erlang 2nd

    Re: json handling map functions in erlang 17 I have not read Joes final book on the matter (several ...

  9. 使用bootstrap响应式布局——手机屏幕中横向滚动显示标签页选项

    导航栏到小屏幕的时候,我们的处理办法是隐藏为一个按钮.可是选项卡的标签页部分,我们的处理办法是加一个水平滚动条.但是加水平滚动条需要解决一个问题,就是宽度的问题,如果不设置宽度,他就会根据屏幕大小自适 ...

  10. JS——基础知识(二)

    1.变量提升问题 <script> var num=10; fun(); function fun(){ console.log(num); var num=20; } </scri ...