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. 《Effective C++》读书笔记 条款03 尽可能使用const 使代码更加健壮

    如果你对const足够了解,只需记住以下结论即可: 将某些东西声明为const可帮助编译器侦测出错误用法,const可被施加于任何作用于内的对象.函数参数.函数返回类型.成员函数本体. 编译器强制实施 ...

  2. 小程序解析html和富文本编辑内容【亲测有效】

    首先去 https://github.com/icindy/wxParse 下载wxParse,只拷贝wxParse文件夹即可. 1.引入wxss @import "../../util/w ...

  3. Python基础 之 list类-列表

    list类-列表 一.list类的基本属性 1. 列表格式 li = [1, 12, 9, ", 10], "even"], "root", True ...

  4. 词嵌入向量WordEmbedding

    词嵌入向量WordEmbedding的原理和生成方法   WordEmbedding 词嵌入向量(WordEmbedding)是NLP里面一个重要的概念,我们可以利用WordEmbedding将一个单 ...

  5. Linux内核设计笔记10——内核同步

    Linux内核同步笔记 几个基本概念 - 临界区(critical region):访问和操作共享数据的代码段: - 原子操作:操作在执行中不被打断,要么不执行,要么执行完: - 竞争条件: 两个线程 ...

  6. .Net并行编程 - Reactive Extensions(Rx)并发浅析

    关于Reactive Extensions(Rx) 关于Reactive Extensions(Rx),先来看一下来自微软的官方描述: The Reactive Extensions (Rx) is ...

  7. nodejs笔记--与MongoDB的交互篇(七)

    原文地址:http://www.cnblogs.com/zhongweiv/p/node_mongodb.html 目录 简介 MongoDB安装(windows) MongoDB基本语法和操作入门( ...

  8. Python的string模块化方法

    Python 2.X中曾经存在过一个string模块,这个模块里面有很多操作字符串的方法,但是在Python 3.X中,这些模块化方法已经被移除了(但是string模块本身没有被移除,因为它还有其他可 ...

  9. 定点数(fixed-point number)

    定义 定点数(fixed-point number)就是小数点位置固定的数,也就是说,小数点后面的位数是固定的,比如要记录一笔账目,这些账目的数字都不会超过100,就可以使用2位小数位定点数来记录,比 ...

  10. 软件工程 作业part1

    自我介绍 老师您好,我叫宋雨,本科在长春理工大学,专业是计算机科学与技术. 1.回想一下你曾经对计算机专业的畅想:当初你是如何做出选择计算机专业的决定?你认为过去接触的课程是否符合你对计算机专业的期待 ...