Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 47905   Accepted: 13903

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

 
 
题目意思:
给一个木板,宽度分为单位为1的段,贴n张海报,海报从l到r,高度等于木板的高度,问最终能看到多少张海报。
 
 
思路:
若每张海报都有一个特定的颜色用数字表示,那么问题就转变为从minl---maxr区间内有多少不同的数字,那么就是线段树区间更新的模型了。
l r最大为10000000,建树的话还要乘上4,很明显爆空间,需要离散化,离散化的时候不能是普通的离散化,需要考虑边界问题,离散化后求染色数目即可。
 
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 40005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int x[N];
int m; int bin_s(int key){
int l=, r=m-;
while(l<=r){
int mm=(l+r)/;
if(x[mm]==key) return mm;
if(x[mm]>key) r=mm-;
else if(x[mm]<key) l=mm+;
}
} struct Line{
int l, r;
}line[N]; struct node{
int l, r, val;
bool f;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].val=-;
if(l==r) return;
build(l,mid,ll);
build(mid+,r,rr);
} void down(int root){
if(a[root].val>&&a[root].l!=a[root].r){
a[ll].val=a[rr].val=a[root].val;
a[root].val=-;
}
} void update(int l,int r,int val,int root){
if(a[root].val==val) return;
if(a[root].l==l&&a[root].r==r){
a[root].val=val;
return;
}
down(root);
if(r<=a[ll].r) update(l,r,val,ll);
else if(l>=a[rr].l) update(l,r,val,rr);
else{
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
if(a[ll].val==a[rr].val&&a[ll].val>) a[root].val=a[ll].val;
} bool visited[N];
int ans; void query(int root){
if(a[root].val!=-&&!visited[a[root].val]) {
ans++;
visited[a[root].val]=true;
return;
}
if(a[root].l==a[root].r)return ;
down(root);
query(ll);
query(rr);
} void out(int root){
if(a[root].l==a[root].r) {
printf("%d ",a[root].val);
return;
}
down(root);
out(ll);
out(rr);
} main()
{
int t, i, j, k; cin>>t;
while(t--){
scanf("%d",&n);
k=;
for(i=;i<n;i++) {
scanf("%d %d",&line[i].l,&line[i].r);
x[++k]=line[i].l;
x[++k]=line[i].r;
}
sort(x+,x+k);
k=unique(x+,x+k+)-x;
m=k;
for(i=;i<k;i++){
if(x[i]-x[i-]>) x[m++]=x[i]-;
}
sort(x,x+m);
build(,m,);
for(i=;i<n;i++){
int l=bin_s(line[i].l);
int r=bin_s(line[i].r);
update(l,r,i+,); }
memset(visited,false,sizeof(visited));
ans=;
query();
printf("%d\n",ans);
//out(1);
}
}

POJ 2528 区间染色,求染色数目,离散化的更多相关文章

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

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

  2. zoj 1610 Count the Colors 【区间覆盖 求染色段】

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  3. POJ - 2528 区间离散化,线段树区间修改,区间询问

    这个题非常有意思的地方是,我们发现区间[1,4]和[5,8]是紧挨着的,因为这个的数代表的是一段区间,原本我们对于普通的离散, a[1]=1,a[2]=5,a[3]=6,a[4]=8;数组下标就是重新 ...

  4. poj 2528(区间改动+离散化)

    题意:有一个黑板上贴海报.给出每一个海报在黑板上的覆盖区间为l r,问最后多少个海报是可见的. 题解:由于l r取值到1e7,肯定是要离散化的,但普通的离散化会出问题.比方[1,10],[1,4],[ ...

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

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

  6. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  7. POJ 2528 Mayor‘s poster 线段树+离散化

    给一块最大为10^8单位宽的墙面,贴poster,每个poster都会给出数据 a,b,表示该poster将从第a单位占据到b单位,新贴的poster会覆盖旧的,最多有10^4张poster,求最后贴 ...

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

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

  9. poj 2528 poster经典线段树+lazy+离散化

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; #def ...

随机推荐

  1. [转发] 老叶观点:MySQL开发规范之我见

    原文: http://imysql.com/2015/07/23/something-important-about-mysql-design-reference.shtml 老叶观点:MySQL开发 ...

  2. Generator 函数的含义与用法

    Generator 函数是协程在 ES6 的实现,最大特点就是可以交出函数的执行权(即暂停执行). function* gen(x){ var y = yield x + 2; return y; } ...

  3. poj2826An Easy Problem?!

    链接 繁琐细节题. 1.线段无交点时,ans=0; 2.如图 假设过p3.y的水平线与p1p2相交 因为雨是垂直下落的,左图的情况是无法收集到雨水的,而这种情况有一种简便的判定方式 cross(p1- ...

  4. DDL之操作表

    DDL之操作表 DDL是数据定义语言,用来定义数据库对象:数据库.表.列等.其中定义数据库我们已经在DDL之操作数据库中详细讲解了,今天我们来学习使用DDL操作表. 1.创建表 使用数据定义语言创建表 ...

  5. 1 HTML

    1 HTML 基础知识 软件的结构:    C/S(Client  Server)结构的软件: 比如: QQ. 极品飞车. 飞信 . 迅雷      cs结构的软件的缺点:更新的时候需要用户下载更新包 ...

  6. 转!! Java中如何遍历Map对象的4种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  7. javaweb2 URL(查找的过程)

    URL: 全名叫统一资源定位符,用于定位互联网的资源. 问题:接上(javaweb1 tomcat)http://localhost:8080/myweb/test.html 分析:http://-- ...

  8. 使用Beanstalkd实现队列

    Beanstalkd可以想象成缓存当中的memcahe或者redise,将我们的队列任务放到内存中进行管理. 运行环境是在linux中,反正我的windows中没运行成功.../(ㄒoㄒ)/~~ 首先 ...

  9. 图像处理JPEGCodec类错误问题 毕业设计遇到的问题

     图像处理JPEGCodec类已经从Jdk1.7移除 2014-06-16 20:01:26 分类: 架构设计与优化 著名测试工具jira在使用图像处理JPEGCodec类会报告以下信息: 我是这样用 ...

  10. 006-Selenium简介

    1.产生背景 Selenium工具诞生的时间已经超过了10年,目前已经在软件开发公司中得到大规模的应用.2004年,在ThoughtWorks公司,一个名为Jason Huggins的测试同行为了减少 ...