zoukankan      html  css  js  c++  java
  • POJ-2528 Mayor's posters (线段树+离散化)

    poj2825

    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
    ",ans);
    113     }
    114 return 0;
    115 }
  • 相关阅读:
    .net 下webservice 的WebMethod的属性
    做一个项目,平时都用到哪些工具提高效率(James Li)
    Android之解析Android Map地图返回的Json数据
    歌词文件LRC的解析,可用于音乐播放器实现歌词同步操作
    Android之创建程序快捷方式
    Android之Bitmap使用心得(持续更新)
    Socket编程之旅(服务器与客户端沟通)
    Android之应用自定义相机拍照并且对拍照文字(英文)进行识别
    android之App widget实际应用Demo
    Android之创建实时文件夹
  • 原文地址:https://www.cnblogs.com/1013star/p/9437990.html
Copyright © 2011-2022 走看看