Poj2528  Mayor & #39; s posters (line tree + discretization)
Mayor's posters
Time Limit:1000 MS 

Memory Limit:65536 K 
Total Submissions:41785 

Accepted:12164 
Description
The citizens of Bytetown, AB, cocould not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. the city councel 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 curous 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 Ith 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 Ith poster, respectively. we know that for each 1 <= I <= n, 1 <= li <= ri <= 10000000. after the Ith 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 has strates the case of the sample input.
Sample Input
151 42 68 103 47 10
Sample Output
4
A wall is provided, n posters are attached to the wall, and each poster is covered by a range. How many posters can be seen at the end?
A large range of posters can be covered, but only a maximum of 10000 posters can be saved using arrays. That is to say, up to 20000 vertices can appear. Therefore, Discretization can be used to separate each vertex, return the control interval, so that the maximum range is 1 to 20000. you can directly use the line segment tree to update segments. Each time you update a color, you can traverse all segments and save the colors that can be traversed.
Define "1" as "no color", "0" indicates that there are multiple colors, and "1 to m" indicates their respective colors. As long as the line segment tree goes down at 0, other colors are directly counted.
However, if there is a problem with discretization in this question, it will squeeze out the color in the middle. However, it seems that poj does the same thing and does not consider the color in the middle.
For example, the correct result of the numbers of 1, 10, and 10 should be three colors, but if the vertices are directly arranged, the colors 5 to 6 will be squeezed out.
If there are still values in the two nodes, add one more node to represent the values in the middle of the two nodes.
#include
#include
#include using namespace std;#define maxn 30000#define lmin 1#define rmax n#define lson l,(l+r)/2,rt<<1#define rson (l+r)/2+1,r,rt<<11#define root lmin,rmax,1#define now l,r,rt#define int_now int l,int r,int rtint cl[maxn<<2] , lazy[maxn<<2] , a[maxn] ;struct node{ int id1 , id2 , k ;}p[maxn];bool cmp(node a,node b){ return a.k < b.k ;}void push_up(int_now){ if( !cl[rt<<1]  !cl[rt<<11]  cl[rt<<1] != cl[rt<<11] ) cl[rt] = 0 ; else cl[rt] = cl[rt<<11] ;}void push_down(int_now){ if( lazy[rt] != 1 ) { lazy[rt<<1] = lazy[rt<<11] = lazy[rt] ; cl[rt<<1] = cl[rt<<11] = lazy[rt] ; lazy[rt] = 1 ; }}void update(int ll,int rr,int x,int_now){ if( ll > r  rr < l ) return ; if( ll <= l && r <= rr ) { cl[rt] = lazy[rt] = x ; return ; } push_down(now); update(ll,rr,x,lson); update(ll,rr,x,rson); push_up(now);}void query(int ll,int rr,int_now,int *a){ if( cl[rt] == 1 ) return ; else if(cl[rt] > 0) { a[ cl[rt] ] = 1 ; return ; } push_down(now); query(ll,rr,lson,a); query(ll,rr,rson,a);}int main(){ int t , i , n , m , l , r , x ; scanf("%d", &t); while(t) { scanf("%d", &m); for(i = 0 ; i < m ; i++) { scanf("%d %d", &p[i].k, &p[i+m].k); p[i].id1 = i ; p[i+m].id1 = i+m ; } sort(p,p+2*m,cmp); int temp = 1 ; n = 0 ; for(i = 0 ; i < 2*m ; i++) { if( p[i].k == temp ) p[i].id2 = n ; else { p[i].id2 = ++n ; temp = p[i].k ; } a[ p[i].id1 ] = p[i].id2 ; } memset(cl,1,sizeof(cl)); memset(lazy,1,sizeof(lazy)); for(i = 0 ; i < m ; i++) { update(a[i],a[i+m],i+1,root); } memset(a,0,sizeof(a)); query(1,n,root,a); int num = 0; for(i = 1 ; i <= m ; i++) if(a[i]) num++ ; printf("%d\n", num); } return 0;}