1. 程式人生 > >線段樹學習(3)離散化 POJ 2528

線段樹學習(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.
2528_1.jpg

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. usingnamespace std;
  4. constint 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. for(i=0;i<2*n;i++)
  90.         {
  91. if(line[i].li!=temp)  
  92.             {
  93.                 tp++;
  94.                 temp=line[i].li;
  95.             }
  96. if(line[i].num<0)
  97.                 post[-line[i].num-1][0]=tp;
  98. else
  99.                 post[line[i].num-1][1]=tp;
  100.         }
  101.         build(1,1,tp);
  102. for ( i=0 ; i<n ; i++ )
  103.             ins(1,post[i][0],post[i][1],i+1);
  104.         sum(1);
  105.         printf("%d/n",cnt);
  106.     }
  107. }
漸漸掌握線段樹了。。。O(∩_∩)O哈哈~,接下來學習擴充套件的線段樹了~~加油~