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


题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度。现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW。后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报。现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到。)

思路:线段树区间更新+离散化

----------------------------------->>>>>>离散化<<<<<<----------------------------------------

原来的:1  2  3  4  6  7  8  10   (去重后排序)

映射后:1  2  3  4  5  6  7  8

原来的:[1, 4]   [2, 6]   [8, 10]   [3, 4]   [7, 10]

映射后:[1, 4]   [2, 5]   [7, 8]     [3, 4]   [6, 8]

很多代码能够AC但是不正确,比如下面的这组数据:

离散化后前:[1, 10]   [1, 3]   [6, 10]

离散化后是:[1, 4]   [1, 2]   [3, 4]

离散化前,3与6之间是有间隙的,但是离散化后,2与3相连的,于是原来3与6之间的部分就看不到,少算了,得到的结果是2,而正解是3。

为了解决这个问题,我们可以对排序后的数组处理一下。比如[1, 3, 6, 10]如果相邻数字间距大于1,就在其中加上任意一个数字,比如加成[1, 3, 4, 6, 7, 10, 11],这样处理就好了。

#include   
#include   
#include   
#include   
using namespace std;  
#define N 11111  
#define lson l, m, rt<<1  
#define rson m+1, r, rt<<1|1  
#define abbreviations int l,int r,int rt  
#define mid (l+r)>>1  
int le[N],ri[N],root[N<<4],lr[N<<4],m,cnt;  
bool flag[N<<4];  
  
void update(abbreviations, int L,int R,int c)  
{  
      if(L <= l && R >= r)  
      {  
            root[rt] = c;  
            return ;  
      }  
      if(root[rt] != -1)  
      {  
          root[rt<<1] = root[rt<<1|1] = root[rt];  
          root[rt] = -1;  
      }  
      int m = mid;  
      if(m >= L) update(lson,L,R,c);  
      if(m < R) update(rson,L,R,c);  
  
}  
void query(abbreviations)  
{  
  
     if(root[rt] != -1)  
     {  
           if(!flag[root[rt]]) cnt++;  
                flag[root[rt]] = true;  
           return ;  
     }  
     if(l == r)  
      return ;  
     int m = mid;  
     query(lson);  
     query(rson);  
}  
int main()  
{  
    int t,n,i,nn;  
    scanf("%d",&t);  
    while(t--)  
    {  
          scanf("%d",&n);  
          for(i = 0,nn = 0; i < n; i++)  
          {  
                scanf("%d%d",&le[i],&ri[i]);  
                lr[nn++] = le[i];  //记录左端点  
                lr[nn++] = ri[i];  //记录右端点  
          }  
          sort(lr,lr+nn);  //排序不能少  
          m = unique(lr,lr+nn)-lr;  //去重  
  
          for(i = m-1; i >= 0; i--)  
          {  
                if(lr[i] != (lr[i-1]+1)) lr[m++] = lr[i] + 1;  //相邻数字大于1就添加一个数  
          }  
          sort(lr,lr+m);  //添加数字后不要忘记排序  
          memset(root,-1,sizeof(root));  //建树叶子节点全部赋值为-1  
  
          for(i = 0; i < n; i++)  
          {  
                int L = lower_bound(lr, lr+m, le[i]) - lr; //二分查找左端点  
                int R = lower_bound(lr, lr+m, ri[i]) - lr; //二分查找右端点  
                update(0, m, 1, L, R, i);  //更新  
          }  
          memset(flag,false,sizeof(flag));  
          cnt = 0;  
          query(0, m, 1);  //求出答案  
          printf("%d\n",cnt);  
    }  
    return 0;  
}

你可能感兴趣的:(【线段树】)