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

    个人心得:线段树也有了一定的掌握,线段树对于区间问题的高效性还是挺好的,不过当区间过大时就需要离散化了,一直不了解离散化是什么鬼,后面去看了下

    离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
    通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小。例如:
    原数据:1,999,100000,15;处理后:1,3,4,2;
    原数据:{100,200},{20,50000},{1,400};
    处理后:{3,4},{2,6},{1,5};
    线段树还有一个重要点就是信息的更新和lazy标志的建立,因为每一个节点除了根节点外都有父节点,而如果俩个子节点都满足同样的情况是
    就可以直接对父节点进行lazy标志,只要对最大的区间进行更新就好了,最后在下放下去,比如这一题,当俩个子节点都完全被覆盖时,
    此时的父节点也就被覆盖了,所以要在程序最后进行更新。
    本来这一题还有一个点不清楚,就是为什么离散化点时,不相邻的要建立个中间点,后面明白了。就好比如果1-10,1-4,6-10,你只分成
    这三个区间1,4,6,10
    则你最终只会得到2个可见,因为4-6这一段你并没有保存并且显示,这就是一些题目中卡在中间bug问题了吧,需要谨慎处理。
    还有这题是后面的后贴,如果你先将前面的标志的话,后面的覆盖起来就会非常麻烦,所以选择从后面开始更新,这些都需要思维的转弯。

    解法:离散化,如下面的例子(题目的样例),因为单位1是一个单位长度,将下面的

          1   2   3   4  6   7   8   10

         —  —  —  —  —  —  —  —

          1   2   3   4  5   6   7   8

    离散化  X[1] = 1; X[2] = 2; X[3] = 3; X[4] = 4; X[5] = 6; X[7] = 8; X[8] = 10

    于是将一个很大的区间映射到一个较小的区间之中了,然后再对每一张海报依次更新在宽度为1~8的墙上(用线段树),最后统计不同颜色的段数。

    但是只是这样简单的离散化是错误的,

    如三张海报为:1~10 1~4 6~10

    离散化时 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10
    第一张海报时:墙的1~4被染为1;
    第二张海报时:墙的1~2被染为2,3~4仍为1;
    第三张海报时:墙的3~4被染为3,1~2仍为2。
    最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。

    新的离散方法为:在相差大于1的数间加一个数,例如在上面1 4 6 10中间加5(算法中实际上1,4之间,6,10之间都新增了数的)

    X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6, X[ 5 ] = 10

    这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3

    最终,1~2为2,3为1,4~5为3,于是输出正确结果3。

    看题目吧。

    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
      1 #include <cstdio>
      2 #include <cstring>
      3 #include<iostream>
      4 #include <algorithm>
      5 #include <queue>
      6 using namespace std;
      7 const int inf=0xffffff0;
      8 const int length=1000005;
      9 const int hl=20000005;
     10 struct tree
     11 {
     12     int l,r;
     13     bool tcover;
     14     int mid()
     15     {
     16         return (l+r)/2;
     17     }
     18     tree *left,*right;
     19 };
     20 tree Tree[length];
     21 struct poster
     22 {
     23     int l,r;
     24 };
     25 poster pos[20005];
     26 int hashb[hl];
     27 int x[length];
     28 int ntree=0;
     29 void builttree(tree *t,int l,int r)
     30 {
     31     t->l=l;
     32     t->r=r;
     33     t->tcover=false;
     34     if(l==r) return ;
     35     ntree++;
     36     t->left=Tree+ntree;
     37     ntree++;
     38     t->right=Tree+ntree;
     39     builttree(t->left,l,(l+r)/2);
     40     builttree(t->right,(l+r)/2+1,r);
     41 }
     42 bool post(tree *t,int l,int r)
     43 {
     44     if(t->tcover==true) return false;
     45     if(t->l==l&&t->r==r){
     46         t->tcover=true;
     47         return true;
     48     }
     49     bool result;
     50     if(r<=t->mid())
     51         result=post(t->left,l,r);
     52     else if(l>=t->mid()+1)
     53         result=post(t->right,l,r);
     54     else
     55     {
     56         bool b1=post(t->left,l,t->mid());
     57         bool b2=post(t->right,t->mid()+1,r);
     58         result=b1||b2;
     59     }
     60     if(t->left->tcover==true&&t->right->tcover==true)
     61          t->tcover=true;
     62     return result;
     63 
     64 }
     65 int main()
     66 {
     67     int t;
     68     scanf("%d",&t);
     69     while(t--){
     70         int n;
     71         ntree=0;
     72         scanf("%d",&n);
     73         int accout=0;
     74         for(int i=0;i<n;i++)
     75         {
     76             scanf("%d%d",&pos[i].l,&pos[i].r);
     77             x[accout++]=pos[i].l;
     78             x[accout++]=pos[i].r;
     79         }
     80         sort(x,x+accout);
     81         int m=unique(x,x+accout)-x;
     82         int treel=0;
     83         for(int i=0;i<m;i++)
     84         {
     85             hashb[x[i]]=treel;
     86             if(i<m-1){
     87                 if(x[i+1]-x[i]==1)
     88                     treel++;
     89                 else treel+=2;
     90             }
     91         }
     92         builttree(Tree,0,treel);
     93         int sum=0;
     94         for(int i=n-1;i>=0;i--)
     95             if(post(Tree,hashb[pos[i].l],hashb[pos[i].r]))
     96                sum++;
     97         printf("%d
    ",sum);
     98 
     99 
    100     }
    101     return 0;
    102 }


  • 相关阅读:
    什么是API
    Maxiee的Vim入门日记(4)——安装windows下的Cscope
    将字符串变成大写----C++实现
    POJ 3254 炮兵阵地(状态压缩DP)
    UIKit和Core Graphics绘图(三)——绘制虚线,椭圆以及饼图
    CRC 模式及实现
    [HDU 1317]XYZZY[SPFA变形][最长路]
    poj 2155 Matrix
    [置顶] Application,Session,Cookie之Application对象
    [Todo] Java及C++ Exception整理
  • 原文地址:https://www.cnblogs.com/blvt/p/7337886.html
Copyright © 2011-2022 走看看