POJ 2528-Mayor's posters- discrete segment tree +

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

main idea:

Segment tree.
Due to the length of the wall is too long, not to open such a large array of discrete first and then achievements.

code show as below:

 

  . 1 #include <cstdio>
   2 #include <the iostream>
   . 3 #include <algorithm>
   . 4  the using  namespace STD;
   . 5 typedef Long  Long LL;
   . 6  const  int N = 1E4 + 20 is ;
   . 7  int B [N << . 1 ], D [N << . 1 ], CD, Chu [N << . 1 ], CNT;
   . 8  // save data preparation discrete 
  . 9  struct Node {
 10      int L, R & lt;
 . 11  } A [N];
 12 is  struct TNODE {
 13 is      int l,r,sum,lazy;
 14 }tr[N<<3];
 15 void pushdown(int m)
 16 {
 17     int t=tr[m].lazy;
 18     if(t!=-1)
 19     {
 20         tr[m<<1].lazy=t;
 21         tr[m<<1|1].lazy=t;
 22     }
 23     tr[m].lazy=-1;
 24     return;
 25 }
 26 void build(int m,int l,int r)
 27 {
 28     tr[m].l=l;
 29     tr[m].r=r;
 30     tr[m].lazy=-1;
 31     if(l==r)
 32         return;
 33     int mid=(l+r)>>1;
 34     build(m<<1,l,mid);
 35     build(m<<1|1,mid+1,r);
 36     return;
 37 }
 38 void update(int m,int l,int r,int v)
 39 {
 40     if(tr[m].l==l&&tr[m].r==r)
 41     {
 42         tr[m].lazy=v;
 43         return;
 44     }
 45     pushdown(m);
 46     int mid=(tr[m].l+tr[m].r)>>1;
 47     if(r<=mid)
 48         update(m<<1,l,r,v);
 49     else if(l>mid)
 50         update(m<<1|1,l,r,v);
 51     else
 52     {
 53         update(m<<1,l,mid,v);
 54         update(m<<1|1,mid+1,r,v);
 55     }
 56     return;
 57 }
 58 void query(int m)
 59 {
 60     if(tr[m].l==tr[m].r)
 61     {
 62         chu[cnt++]=tr[m].lazy;
 63         return;
 64     }
 65     pushdown(m);
 66     query(m<<1);
 67     query(m<<1|1);
 68 }
 69 int getid(int x)
 70 {
 71     return lower_bound(d,d+cd,x)-d+1;
 72 }
 73 int main()
 74 {
 75     int T;
 76     cin>>T;
 77     while(T--)
 78     {
 79         int n;
 80         scanf("%d",&n);
 81         //输入并准备离散 
 82         int cb=1;
 83         b[0]=-1;
 84         for(int i=0;i<n;i++)
 85         {
 86             scanf("%d%d",&a[i].l,&a[i].r);
 87             b[cb++]=a[i].l;
 88             b[cb++]=a[i].r;
 89         }
90          // discrete 
91 is          Sort (B + . 1 , B + CB);
 92          CD = 0 ;
 93          for ( int I = . 1 ; I <CB; I ++ )
 94              IF (! B [I] = B [I- . 1 ])
 95                  D [cd ++] = B [I];
 96          // contribution depending on the length cd discretized 
97          Build ( . 1 , . 1 , cd);
 98          // update the segment tree 
99          for ( int I = 0 ; I <n-; I ++ )
 100             Update ( . 1 , getId (A [I] .L), getId (A [I] .r), I + . 1 );
 101          CNT = . 1 ;
 102          Chu [ 0 ] = - . 1 ;
 103          // will push in the end all the pushdown unit 
104          Query ( . 1 );
 105          // request ANS 
106          Sort (Chu + . 1 , Chu + CNT);
 107          int ANS = 0 ;
 108          for ( int I = . 1 ; I <CNT; I ++ )
 109              IF (Chu [I]! Chu = [I- . 1])
 110                  ANS ++ ;
 111          // output 
112          the printf ( " % D \ n- " , ANS);
 113      }
 114      return  0 ;
 115 }

 

Guess you like

Origin www.cnblogs.com/NothingbutFight/p/11307282.html