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. MonkeyRunner学习(1)测试连接

    前期的环境搭建详见:http://www.cnblogs.com/peng-lan/p/5388488.html 1.打开模拟器 打开模拟器有两种方法,正常的在eclipse 中启动就不介绍了 另一种 ...

  2. mysql 查询开销 sending data

    1.执行一个查询,发现时间开销都在sending data,为什么?2.sending data容易误导,让人以为只是发送数据给客户端,实际上sending data包含两个过程:读取数据并处理,发送 ...

  3. Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处:http://blog.csdn.net/xiaanming/article/detail ...

  4. Ubuntu 查看文件以及磁盘空间大小管理

    (1)查看文件大小  查看当前文件夹下所有文件大小(包括子文件夹)    du -sh   # du -h15M     ./package16K     ./.fontconfig4.0K    . ...

  5. jQuery内置函数 ready

    jQuery给我们提供了一些非常实用的函数 ready:加载函数 传统事件只能绑定一个函数 后面的函数会把前面的函数覆盖 js中用的是  window.onload = function(){}; j ...

  6. php错误以及常用笔记

    //语法错误(syntax error)在语法分析阶段,源代码并未被执行,故不会有任何输出. /* [命名规则] */ 常量名 类常量建议全大写,单词间用下划线分隔 // MIN_WIDTH 变量名建 ...

  7. Python核心编程-闭包

    百度搜了一下闭包的概念:简而言之,闭包的作用就是在外部函数执行完并返回后,闭包使得收机制不会收回函数所占用的资源,因为内部函数的执行需要依赖外函数中的变量.这是对闭包作用的非常直白的描述,不专业也不严 ...

  8. Rocketmq-尝试理解

    普通的信息发送和消费 首先要启动nameserver和broker,nameserver是一个几乎无状态节点.broker分为master和slave,master和slave的对应关系通过指定相同的 ...

  9. Android 为PopupWindow设置动画效果

    首先定义显示效果的动画文件: <?xml version="1.0" encoding="utf-8"?> <set xmlns:androi ...

  10. 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...