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. [转载] google mock CheatSheet

    原文: https://code.google.com/p/googlemock/wiki/CheatSheet Defining a Mock Class Mocking a Normal Clas ...

  2. mysql 执行计划的理解

    1.执行计划就是在sql语句之前加上explain,使用desc 也可以.2.desc有两个选项extended和partitions,desc extended 将原sql语句进行优化,通过show ...

  3. python 标准库

    https://www.zhihu.com/question/24590883 https://www.zhihu.com/question/20501628 http://blog.csdn.net ...

  4. Object Pascal 面向对象的特性

    2 面向对象的特性 在软件系统开发过程中,结构分析技术和结构设计技术具有很多优点,但同时也存在着许多难以克服的缺点.因为结构分析技术和结构设计技术是围绕着实现处理功能来构造系统的,而在系统维护和软件升 ...

  5. 工作了3年的JAVA程序员应该具备什么技能?(zhuan)

    http://www.500d.me/article/5441.html **************************************** 来源:五百丁 作者:LZ2016-03-18 ...

  6. Rocketmq-尝试理解

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

  7. 为什么删不掉date模块

    显示是field pending deletion一看report里面的field list并没有xxx_date_xxx,只好跑到数据库才看到一个field_date_test当时并没有把这个字段当 ...

  8. 基于TBDS的flume异常问题排查过程

    版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/214 来源:腾云阁 https://www.qclou ...

  9. android内存耗用:VSS/RSS/PSS/USS

    VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)  不是真实当前应用进程所占用的内存. 内存分配的原理 从操作系统角度来看,进程分配内存有两种方式,分别由两个系统调用完 ...

  10. [saiku] 集成单点登录

    思路: 自定义一个loginCallbackFilter用于单点登录成功后执行模拟用户认证授权登录的操作. 当授权成功后所有配置需要授权才能访问的url就再也不会被任何filter拦截,可随意访问了. ...