原创  线段树学习(3)离散化 POJ 2528 收藏

看了好多关于线段树离散化的资料,对离散化还是很陌生,于是尝试着做一道需要离散化的线段树题,来找一找感觉。资料上的离散化的知识,用不到题中(我好笨。。。),于是在网上找到了这题的代码,经过一番研究后,顿悟。
POJ 2528 对于初学者来说,是道不错的题。
-----------------------------------------题目----------------------------------------------------------

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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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
假如不离散化,那线段树的上界是10e7,假如建一个那么大的线段树的话。。。必然MLE。于是要考虑离散化。
离散化的目的就是要将线段的长度适当的缩小,但不破坏题意。
比如:
------   (1,6)
------------ (1,12 )
像这样这样的两条线段,可以把它们看作:
-- (1,2)
--- ( 1,3 )
这样,缩短了线段的长度,但是他们的覆盖关系并没有改变。
关键我们要重新给压缩后的线段标记起点和终点。
按照通用的离散化方法。。。。
首先依次读入线段端点坐标,存于post[MAXN][2]中,post[i][0]存第一条线段的起点,post[i][1]存第一条线段的终点,然后用一个结构题数组line[MAXN]记录信息,line[i].li记录端点坐标,line[i].num记录这个点属于哪条线段(可以用正负数表示,负数表示起点,正数表示终点)。假如有N条线段,就有2*N个端点。然后将line数组排序,按照端点的坐标,从小到大排。接着要把线段赋予新的端点坐标了。从左到右按照递增的次序,依次更新端点,假如2*N个点中,共有M个不同坐标的点,那么线段树的范围就是[1,M]。

----------------------------------------------code------------------------------------------------------------
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. const int MAXN = 30000 ;
  5. struct NODE
  6. {
  7.     int l,r,c;
  8. }tree[MAXN*5];
  9. struct L
  10. {
  11.     int li,num;
  12.     bool operator < ( const L& r ) const 
  13.     {
  14.         return li<r.li;
  15.     };
  16. };
  17. L line[MAXN];
  18. int post[MAXN][2];
  19. bool vis[MAXN];
  20. int cnt;
  21. void build ( int v , int l, int r )
  22. {
  23.     int m;
  24.     tree[v].c=0,tree[v].l=l,tree[v].r=r;
  25.     if ( l>=r )
  26.         return;
  27.     m=(l+r)/2;
  28.     build( v*2, l,m );
  29.     build(v*2+1,m+1,r);
  30. }
  31. void ins (int v , int l , int r , int c )
  32. {
  33.     int m;
  34.     if ( tree[v].l==l && tree[v].r==r )
  35.     {
  36.         tree[v].c=c;
  37.         return;
  38.     }
  39.     if ( tree[v].c>0 && tree[v].c!=c )
  40.     {
  41.         tree[v*2].c=tree[v].c;
  42.         tree[v*2+1].c=tree[v].c;
  43.         tree[v].c=0;
  44.     }
  45.     m=(tree[v].l+tree[v].r)>>1;
  46.     if ( r<=m )
  47.         ins(v*2,l,r,c);
  48.     else
  49.         if ( l>m )
  50.             ins( v*2+1,l,r,c);
  51.         else
  52.         {
  53.             ins(v*2,l,m,c);
  54.             ins(v*2+1,m+1,r,c);
  55.         }
  56. }
  57. void sum ( int v )
  58. {
  59.     if ( tree[v].c )
  60.     {
  61.         if ( !vis[tree[v].c] )
  62.         {
  63.             vis[tree[v].c]=true;
  64.             cnt++;
  65.         }
  66.         return;
  67.     }
  68.     sum(v*2);
  69.     sum(v*2+1);
  70. }
  71. int main ( )
  72. {
  73.     int n,N,i;
  74.     scanf("%d",&N);
  75.     while ( N-- )
  76.     {
  77.         cnt=0;
  78.         scanf("%d",&n);
  79.         memset(vis,false,sizeof(vis));
  80.         //记录端点
  81.         for ( i=0 ; i<n ; i++ )
  82.         {
  83.             scanf("%d%d",&post[i][0],&post[i][1]);
  84.             line[i*2].li=post[i][0],line[i*2].num=-(i+1),line[i*2+1].li=post[i][1],line[i*2+1].num=i+1;
  85.         }
  86.         //离散化坐标
  87.         sort(line,line+n*2);
  88.         int tp=1,temp=line[0].li;
  89.         
  90.         for(i=0;i<2*n;i++)
  91.         {
  92.             if(line[i].li!=temp)  
  93.             {
  94.                 tp++;
  95.                 temp=line[i].li;
  96.             }
  97.             if(line[i].num<0)
  98.                 post[-line[i].num-1][0]=tp;
  99.             else
  100.                 post[line[i].num-1][1]=tp;
  101.         }
  102.         build(1,1,tp);
  103.         for ( i=0 ; i<n ; i++ )
  104.             ins(1,post[i][0],post[i][1],i+1);
  105.         sum(1);
  106.         printf("%d\n",cnt);
  107.     }
  108. }
渐渐掌握线段树了。。。O(∩_∩)O哈哈~,接下来学习扩展的线段树了~~加油~

发表于 @ 2008年10月23日 23:15:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:线段树学习(2)POI Promotion | 新一篇:线段树小节

  • 发表评论
  • 评论内容:
  •  
Copyright © chinaeli
Powered by CSDN Blog