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

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

Alberta Collegiate Programming Contest 2003.10.18

题目意思:

给一个木板,宽度分为单位为1的段,贴n张海报,海报从l到r,高度等于木板的高度,问最终能看到多少张海报。

思路:

若每张海报都有一个特定的颜色用数字表示,那么问题就转变为从minl---maxr区间内有多少不同的数字,那么就是线段树区间更新的模型了。

l r最大为10000000,建树的话还要乘上4,很明显爆空间,需要离散化,离散化的时候不能是普通的离散化,需要考虑边界问题,离散化后求染色数目即可。

代码:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 #include <set>
  9 using namespace std;
 10
 11 #define N 40005
 12 #define ll root<<1
 13 #define rr root<<1|1
 14 #define mid (a[root].l+a[root].r)/2
 15
 16
 17 int max(int x,int y){return x>y?x:y;}
 18 int min(int x,int y){return x<y?x:y;}
 19 int abs(int x,int y){return x<0?-x:x;}
 20
 21 int n;
 22 int x[N];
 23 int m;
 24
 25 int bin_s(int key){
 26     int l=1, r=m-1;
 27     while(l<=r){
 28         int mm=(l+r)/2;
 29         if(x[mm]==key) return mm;
 30         if(x[mm]>key) r=mm-1;
 31         else if(x[mm]<key) l=mm+1;
 32     }
 33 }
 34
 35 struct Line{
 36     int l, r;
 37 }line[N];
 38
 39 struct node{
 40     int l, r, val;
 41     bool f;
 42 }a[N*4];
 43
 44
 45 void build(int l,int r,int root){
 46     a[root].l=l;
 47     a[root].r=r;
 48     a[root].val=-1;
 49     if(l==r) return;
 50     build(l,mid,ll);
 51     build(mid+1,r,rr);
 52 }
 53
 54 void down(int root){
 55     if(a[root].val>0&&a[root].l!=a[root].r){
 56         a[ll].val=a[rr].val=a[root].val;
 57         a[root].val=-1;
 58     }
 59 }
 60
 61 void update(int l,int r,int val,int root){
 62     if(a[root].val==val) return;
 63     if(a[root].l==l&&a[root].r==r){
 64         a[root].val=val;
 65         return;
 66     }
 67     down(root);
 68     if(r<=a[ll].r) update(l,r,val,ll);
 69     else if(l>=a[rr].l) update(l,r,val,rr);
 70     else{
 71         update(l,mid,val,ll);
 72         update(mid+1,r,val,rr);
 73     }
 74     if(a[ll].val==a[rr].val&&a[ll].val>0) a[root].val=a[ll].val;
 75 }
 76
 77 bool visited[N];
 78 int ans;
 79
 80 void query(int root){
 81     if(a[root].val!=-1&&!visited[a[root].val]) {
 82         ans++;
 83         visited[a[root].val]=true;
 84         return;
 85     }
 86     if(a[root].l==a[root].r)return ;
 87     down(root);
 88     query(ll);
 89     query(rr);
 90 }
 91
 92 void out(int root){
 93     if(a[root].l==a[root].r) {
 94         printf("%d ",a[root].val);
 95         return;
 96     }
 97     down(root);
 98     out(ll);
 99     out(rr);
100 }
101
102 main()
103 {
104     int t, i, j, k;
105
106     cin>>t;
107     while(t--){
108         scanf("%d",&n);
109         k=0;
110         for(i=0;i<n;i++) {
111             scanf("%d %d",&line[i].l,&line[i].r);
112             x[++k]=line[i].l;
113             x[++k]=line[i].r;
114         }
115         sort(x+1,x+k);
116         k=unique(x+1,x+k+1)-x;
117         m=k;
118         for(i=2;i<k;i++){
119             if(x[i]-x[i-1]>1) x[m++]=x[i]-1;
120         }
121         sort(x,x+m);
122         build(1,m,1);
123         for(i=0;i<n;i++){
124             int l=bin_s(line[i].l);
125             int r=bin_s(line[i].r);
126             update(l,r,i+1,1);
127
128         }
129         memset(visited,false,sizeof(visited));
130         ans=0;
131         query(1);
132         printf("%d\n",ans);
133         //out(1);
134     }
135 }
时间: 2024-11-07 13:44:05

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

Poj 2528 Mayor&#39;s posters (线段树+离散化)

题目连接: http://poj.org/problem?id=2528 题目大意: 有10000000块瓷砖,n张海报需要贴在墙上,每张海报所占的宽度和瓷砖宽度一样,长度是瓷砖长度的整数倍,问按照所给海报顺序向瓷砖上贴海报,最后有几张海报是可见的? 解题思路: 因为瓷砖块数和海报张数多,首选线段树,如果按照常规的建树方式,把瓷砖当做数的节点,肯定会MTL......... 所以我们可以用海报的起点和终点当做树的节点,这样树的节点才有20000个,但是这样建树的话,求海报覆盖了那些节点会很复杂,

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

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

poj 2528(区间修改+离散化)

题意:有一个黑板上贴海报,给出每个海报在黑板上的覆盖区间为l r,问最后多少个海报是可见的. 题解:因为l r取值到1e7,肯定是要离散化的,但普通的离散化会出问题,比如[1,10],[1,4],[4,6]普通得到答案是2,但其实是3,改进的离散化方法如果两个数字相差大于1,就在中间补一个数字. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> usin

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

题意:有一个黑板上贴海报.给出每一个海报在黑板上的覆盖区间为l r,问最后多少个海报是可见的. 题解:由于l r取值到1e7,肯定是要离散化的,但普通的离散化会出问题.比方[1,10],[1,4],[6,10]普通得到答案是2,但事实上是3.改进的离散化方法假设两个数字相差大于1,就在中间补一个数字. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> u

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

这个题非常有意思的地方是,我们发现区间[1,4]和[5,8]是紧挨着的,因为这个的数代表的是一段区间,原本我们对于普通的离散, a[1]=1,a[2]=5,a[3]=6,a[4]=8;数组下标就是重新离散的位置,但是a[2]和a[3]明显不重叠,为此我们需要重新考虑离散的内容,其实不妨这样,如果区间的间隔大于1,那么我们插入一个数a[i]+1,这样就强行把a[i]和a[i+1]分开,因为 如三张海报为:1~10 1~4 6~10 离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3

POJ 2528 Mayor&#39;s posters(线段树区间染色+离散化或倒序更新)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

poj 2528 Mayor&#39;s posters(线段树区间覆盖、离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49385   Accepted: 14304 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ 2528 Mayor&#39;s posters 区间离散化线段树

点击打开链接 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45894   Accepted: 13290 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their elector

POJ 2528 Mayor&#39;s posters (线段树区间更新+离散化)

题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值.由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案. 写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目. 1 #include <iostream> 2 #include <