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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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,很明显爆空间,需要离散化,离散化的时候不能是普通的离散化,需要考虑边界问题,离散化后求染色数目即可。
 
 
代码:
  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 }

 

转载于:https://www.cnblogs.com/qq1012662902/p/4535002.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值