POJ-2528 Mayor's posters (线段树+离散化)

本文详细介绍了如何使用线段树解决在给定长度的墙面上放置不同宽度海报的问题,确保最终可见海报数量最大化。通过离散化优化区间存储,实现高效的区间查询和更新操作。案例分析和代码实现清晰展示了算法应用过程。

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. 
InputThe 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.OutputFor 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

题意:有一块足够长的墙了给竞选人贴海报,后贴的可能会把前面贴的给覆盖掉,问最后有多少不同的海报是能看到的。
题解:我觉得线段树的离散化就是因为区间在一个线段长度上过于分散,比如数据范围是1-100000 可是只有1-2 和500-510 两段,这样在数据的存储方面就会过于浪费空间,所以通过将线段区间端点从小到大排序,去重,使用lower_bound函数就可以将总区间长度尽可能缩小,而不影响原本区间之间的关系。之后本题就变成了单纯的区间着色和区间查询问题,套用线段树区间操作模板。
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 using namespace std;
  6 typedef long long ll;
  7 const int maxn=10000+10;
  8 int flag;
  9 struct node{
 10     int left;
 11     int right;
 12     int num;
 13     int vis;
 14 }e[maxn<<3];
 15 int vis[maxn<<2];
 16 struct Tree{
 17     int left;
 18     int right;
 19 }tree[maxn<<3]; 
 20 long long ans;
 21 int a[maxn<<2];
 22 int pos[maxn][2];
 23 void pushdown(int cur)
 24 {
 25     if(e[cur].num==0)    return;
 26     e[cur*2].num=e[cur*2+1].num=e[cur].num;//将标记传递给e[cur]的左右子树。 
 27     e[cur].num=0;
 28 }
 29 void build(int l,int r,int cur)
 30 {
 31     e[cur].left=l;
 32     e[cur].right=r;
 33     e[cur].vis=0;
 34     e[cur].num=0;
 35     if(l==r)
 36     {
 37         return;
 38     } 
 39     int mid=(l+r)/2;
 40     build(l,mid,cur*2);
 41     build(mid+1,r,cur*2+1);
 42 }
 43 void update(int pl,int pr,int cur,int i)
 44 { 
 45     if(e[cur].left>=pl&&e[cur].right<=pr)
 46     {
 47         e[cur].num=i;    
 48         return;
 49     }
 50     pushdown(cur);
 51     int mid=(e[cur].left+e[cur].right)/2;
 52     if(pl<=mid)
 53         update(pl,pr,cur*2,i);
 54     if(pr>=mid+1)
 55         update(pl,pr,cur*2+1,i);
 56 }
 57 void query(int pl,int pr,int cur)
 58 {
 59     if(e[cur].num!=0)
 60     {
 61         if(vis[e[cur].num]==0)//vis数组表示将已经出现的海报标记。 
 62         {
 63             ans++;
 64             vis[e[cur].num]=1;
 65         }
 66      
 67      return ;
 68     }
 69     
 70     int mid=(pl+pr)/2;//此处注意理解 不能写成mid=(e[cur].left+e[cur].right)/2; 
 71     query(pl,mid,cur*2);
 72     query(mid+1,pr,cur*2+1);
 73 }
 74 int main()
 75 {
 76     int casen;
 77     int n;
 78     cin>>casen;
 79     while(casen--)
 80     {
 81         memset(vis,0,sizeof(vis));
 82         memset(a,0,sizeof(a));
 83         memset(pos,0,sizeof(pos));
 84         int id=0;
 85         scanf("%d",&n);
 86         for(int i=1;i<=n;i++)
 87         {
 88             scanf("%d%d",&pos[i][0],&pos[i][1]);
 89             a[++id]=pos[i][0];
 90             a[++id]=pos[i][1];
 91         }
 92         int cnt=1;
 93         sort(a+1,a+id+1);
 94         for(int i=2;i<=id;i++)
 95         {
 96             if(a[i]!=a[i-1])
 97                 a[++cnt]=a[i];
 98         } //去重 
 99         ans=0;
100         int R=0;
101         for(int i=1;i<=n;i++)
102         {
103             int ul=lower_bound(a+1,a+1+cnt,pos[i][0])-a;
104             int ur=lower_bound(a+1,a+1+cnt,pos[i][1])-a;
105             tree[i].left=ul;
106             tree[i].right=ur;
107             R=max(R,tree[i].right);
108         }//离散化缩短区间长度 
109         build(1,R,1);
110         for(int i=1;i<=n;i++)
111         {
112             update(tree[i].left,tree[i].right,1,i);    
113         }
114         query(1,R,1);
115         cout<<ans<<endl;
116     }
117 return 0;
118 }

 稍微改进一下下~

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 using namespace std;
  6 const int maxn=100010;
  7 int n;
  8 struct edge{
  9     int l;
 10     int r;
 11 }e[maxn<<2];
 12 int ans;
 13 struct node{
 14     int l;
 15     int r;
 16     int laz;
 17     int val;
 18 }tree[maxn<<2];
 19 int vis[maxn<<2];
 20 int pos[maxn][2],a[maxn<<2];
 21 int casen;
 22 void build(int l,int r,int cur)
 23 {
 24     tree[cur].l=l;
 25     tree[cur].r=r;
 26     tree[cur].val=0;
 27     if(l==r)
 28         return;
 29     int mid=(l+r)/2;
 30     build(l,mid,cur<<1);
 31     build(mid+1,r,cur<<1|1);
 32 }
 33 void pushdown(int cur)
 34 {
 35     if(tree[cur].val!=0)
 36     {
 37         tree[cur<<1].val=tree[cur<<1|1].val=tree[cur].val;
 38         tree[cur].val=0;
 39     }
 40     return;
 41 }
 42 void update(int pl,int pr,int cur,int color)
 43 {
 44     if(pl<=tree[cur].l&&tree[cur].r<=pr)
 45     {
 46         tree[cur].val=color;
 47         return;
 48     }
 49     pushdown(cur);
 50     int mid=(tree[cur].l+tree[cur].r)/2;
 51     if(pl<=mid)
 52         update(pl,pr,cur<<1,color);
 53     if(pr>mid)
 54         update(pl,pr,cur<<1|1,color);
 55 }
 56 void query(int cur)
 57 {
 58     if(tree[cur].val!=0)
 59     {
 60         if(!vis[tree[cur].val])
 61         {
 62             vis[tree[cur].val]=1;
 63             ans++;
 64         }
 65         return;
 66     }
 67     query(cur<<1);
 68     query(cur<<1|1);
 69 }
 70 int main()
 71 {
 72     cin>>casen;
 73     while(casen--)
 74     {
 75         memset(vis,0,sizeof(vis));
 76         memset(a,0,sizeof(a));
 77         memset(pos,0,sizeof(pos));
 78         scanf("%d",&n);
 79         int id=0;
 80         for(int i=1;i<=n;i++)
 81         {
 82               scanf("%d%d",&pos[i][0],&pos[i][1]);
 83               a[++id]=pos[i][0];
 84               a[++id]=pos[i][1];
 85         }
 86         sort(a+1,a+id+1);
 87         int cnt=1;
 88         for(int i=2;i<=id;i++)
 89         {
 90             if(a[i]!=a[i-1])
 91             {
 92                 a[++cnt]=a[i];
 93             }
 94         }
 95         ans=0;
 96         int R=0;
 97         for(int i=1;i<=n;i++)
 98         {
 99             int ul=lower_bound(a+1,a+1+cnt,pos[i][0])-a;
100             int ur=lower_bound(a+1,a+1+cnt,pos[i][1])-a;
101             e[i].l=ul;
102             e[i].r=ur;
103             R=max(R,e[i].r);
104         }
105         //cout<<R<<endl;
106         build(1,R,1);
107         for(int i=1;i<=n;i++)
108         {
109             update(e[i].l,e[i].r,1,i);
110         }
111         query(1);
112         printf("%d\n",ans);
113     }
114 return 0;
115 }

 

转载于:https://www.cnblogs.com/1013star/p/9437990.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值