Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 62771   Accepted: 18120

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

题意:
10000000 长的墙,有n张海报依次贴在墙上,每张海报贴在[a,b]范围,问最后没有完全被覆盖的海报有多少。
代码:
//离散化一:比如对于如下区间集合,[1,1000],[500,2000],[1500,2500].那么
//把所有区间端点1,500,1000,1500,2000,2500离散化后就是1,2,3,4,5,6.离散化
//后所得区间为:[1,3],[2,5],[4,6].可以知道离散化前可见区间有3个,但是离散
//化后只有区间[1,3]和区间[4,6]可见.所以离散化一的方式是有问题的
//离散化二:对于区间端点的离散化,如果离散化之前相邻的两个数不是类似于a与a+1的差距1关系,
//那么就自动在后面的这个数的离散化结果上加1.比如:[1,10],[1,5],[7,10] 离散化后
//的区间为[1,7][1,3],[5,7]
//离散化方式二主要就是让本来不相邻的数继续保持不相邻即可. //离散化之后用二分查找对应的离散化后的点,本题val数组要开大。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
bool vis[maxn*+];
int mp[maxn*+],val[maxn*+],t,n,ans;
struct node
{
int l,r;
}nodes[maxn*+];
int Bsearch(int a,int b,int *c)
{
int l=,r=b-,mid;
while(l<=r){
mid=(l+r)>>;
if(c[mid]==a) return mid;
else if(c[mid]<a) l=mid+;
else r=mid-;
}
return -;
}
void Pushdown(int rt)
{
if(val[rt]>=){
val[rt<<]=val[rt<<|]=val[rt];
}
val[rt]=-;
}
void Update(int ql,int qr,int c,int l,int r,int rt)
{
if(ql<=l&&qr>=r){
val[rt]=c;
return ;
}
Pushdown(rt);
int m=(l+r)>>;
if(ql<=m) Update(ql,qr,c,l,m,rt<<);
if(qr>m) Update(ql,qr,c,m+,r,rt<<|);
}
void Query(int l,int r,int rt)
{
if(val[rt]>=){
if(!vis[val[rt]]) ans++;
vis[val[rt]]=;
return;
}
if(l==r) return;
Pushdown(rt);
int m=(l+r)>>;
Query(l,m,rt<<);
Query(m+,r,rt<<|);
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int m=;
for(int i=;i<n;i++){
scanf("%d%d",&nodes[i].l,&nodes[i].r);
mp[m++]=nodes[i].l;mp[m++]=nodes[i].r;
}
sort(mp,mp+m);
m=unique(mp,mp+m)-mp;//去重,加入相关的点
for(int i=m-;i>=;i--){
if(mp[i]-mp[i-]>) mp[m++]=mp[i]-;
}
sort(mp,mp+m);
memset(vis,,sizeof(vis));
memset(val,-,sizeof(val));
for(int i=;i<n;i++){
nodes[i].l=Bsearch(nodes[i].l,m,mp);
nodes[i].r=Bsearch(nodes[i].r,m,mp);
Update(nodes[i].l,nodes[i].r,i,,m-,);
}
ans=;
Query(,m-,);
printf("%d\n",ans);
}
return ;
}

POJ2528 线段树离散化的更多相关文章

  1. poj2528(线段树+离散化)Mayor's posters

    2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 ...

  2. poj2528 线段树+离散化 (倒序)

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  3. poj2528 线段树+离散化

    由于坐标可能很大,此时需要离散化,将值转化为对应的坐标. #include<stdio.h> #include<algorithm> using namespace std; ...

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

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

  5. poj-2528线段树练习

    title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...

  6. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  7. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  8. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  9. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

随机推荐

  1. jQuery 对象 与 原生 DOM 对象 相互转换

    区别 jQuery 选择器得到的 jQuery对象 和 原生JS 中的document.getElementById() document.querySelector取得的 DOM对象 是两种不同类型 ...

  2. Kali信息收集工具-dimtry

    帮助文档 -s和-e参数需要用到google搜索 1.获取whois主机ip信息 2.扫描端口,根据banner信息判断服务  

  3. Python3 数值类型与运算符

    1.数值类型与进制 (1)基本类型 整型:int 浮点型:float 布尔类型:bool 复数:complex print(type(1)) print(type(1.1)) print(type(F ...

  4. Simple Pipelined Function

    SELECT * FROM TABLE(PKG_TEST.FN_DIC_DB_TAB) CREATE OR REPLACE PACKAGE PKG_TEST IS   TYPE OBJ_DICDB_R ...

  5. java — 设计模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 一.设计模式的分类 ...

  6. setsockopt 设置socket 详细用法

    1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket:BOOL bReuseaddr=TRUE;setsockopt(s,SOL_SOCKET ,SO ...

  7. 提升MyEclipse运行速度

    修改MyEclipse.ini文件中的,将-vmargs后面的参数修改为 -Xms256m -Xmx768m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:Max ...

  8. [剑指Offer] 45.扑克牌顺子

    题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决 ...

  9. bootstrap 有些控件需要调用锚点,会与angular 路由 冲突

    最简单的方法 就是 在 #号前加/, 但有人说 在服务器上回失效,也不知道是什么原理.慎用 最靠谱的方法 就 是 使用bootstrap中的js控制控件, 比如轮播图的上一页 下一页,就可以在 ang ...

  10. java 写入int型时会自动转换成字符

    java  写入int型时会自动转换成字符