POJ 2528 区间染色,求染色数目,离散化

时间:2022-12-19 08:22:46
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.

 
POJ 2528 区间染色,求染色数目,离散化

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&&num;39&semi;s posters 线段树&plus;离散化技巧

    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&lpar;区间改动&plus;离散化&rpar;

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

  5. POJ 2528 Mayor&&num;39&semi;s posters&lpar;线段树&plus;离散化&rpar;

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

  6. poj 2528 Mayor&&num;39&semi;s posters 线段树&plus;离散化 &vert;&vert; hihocode &num;1079 离散化

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

  7. POJ 2528 Mayor&OpenCurlyQuote;s poster 线段树&plus;离散化

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

  8. POJ 2528 Mayor&&num;39&semi;s posters &lpar;线段树&plus;离散化)

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

  9. poj 2528 poster经典线段树&plus;lazy&plus;离散化

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

随机推荐

  1. 取消TableViewCell选中状态的外观变化

    tabelViewcell 使用Xib创建自定义外观的时候,在tableview选中的时候,cell的外观会发生变化,在定义Xib中如下图将选中的外观状态取消掉 也有其他选项,可以选择控制选中的时候的 ...

  2. atitit&period;web ui 结构建模工具总结

    atitit.web ui 结构建模工具总结 1. 王者.dreamweaver 1 2. Frontpage/SharePoint Designer(FrontPage) 2010... 1 3.  ...

  3. Web基础开发最核心要解决的问题

    Web基础开发要解决的问题,往往也就是那些框架出现的目的 - 要解决问题. 1. 便捷的Db操作: 2. 高效的表单处理: 3. 灵活的Url路由: 4. 合理的代码组织结构: 5. 架构延伸 缓存. ...

  4. Zend Framework XML外部实体和安全绕过漏洞

    漏洞版本: Zend Framework 1.x 漏洞描述: Bugtraq ID:66358 Zend Framework是一款开放源代码的PHP5开发框架实现. Zend Framework存在多 ...

  5. HTML5外包团队 更新一下2019最新案例

    本项目控件均为动态加载,3D部分使用Unity3D,其它基于ReactJS,NodeJS,部分使用cocos2D,由于项目涉密,只能发部分截图,欢迎联系索取更多案例,企鹅号 372900288 祝大家 ...

  6. 综述 - 染色质可及性与调控表观基因组 &vert; Chromatin accessibility and the regulatory epigenome

    RNA-seq这个工具该什么时候用?ATAC-seq该什么时候用?有相当一部分项目设计不行,导致花大钱测了一些没有意义的数据. 还是在中心法则这个框架下来解释,这是生物信息的核心.打开华大科技服务官网 ...

  7. &period;Net学习资料

    1.博客系列文章 (1)设计模式 吕震宇 设计模式 张逸:晴窗笔记 Design & Pattern 梦幻Dot Net  .Net设计模式 李会军          .NET设计模式系列文章 ...

  8. 最后一次谈 VirtualBox的安装方法

    用 VirtualBox....run 或 .rpm安装都可以, 最重要的是要 用 /usr/sbin/vboxconfig -> vboxdrv.sh --> 去创建 VirutalBo ...

  9. HDOJ-1156 Brownie Points II 线段树&sol;树状数组(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1156 在一张二位坐标系中,给定n个点的坐标,玩一个划线游戏(线必须穿过点),Stan先手画一条垂直的线,然后Ol ...

  10. POJ2411&lpar;SummerTrainingDay02-I 状态压缩dp&rpar;

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17187   Accepted: 991 ...