看了好多关于线段树离散化的资料,对离散化还是很陌生,于是尝试着做一道需要离散化的线段树题,来找一找感觉。资料上的离散化的知识,用不到题中(我好笨。。。),于是在网上找到了这题的代码,经过一番研究后,顿悟。
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------------------------------------------------------------
- #include<iostream>
- #include<algorithm>
- using namespace std;
- const int MAXN = 30000 ;
- struct NODE
- {
- int l,r,c;
- }tree[MAXN*5];
- struct L
- {
- int li,num;
- bool operator < ( const L& r ) const
- {
- return li<r.li;
- };
- };
- L line[MAXN];
- int post[MAXN][2];
- bool vis[MAXN];
- int cnt;
- void build ( int v , int l, int r )
- {
- int m;
- tree[v].c=0,tree[v].l=l,tree[v].r=r;
- if ( l>=r )
- return;
- m=(l+r)/2;
- build( v*2, l,m );
- build(v*2+1,m+1,r);
- }
- void ins (int v , int l , int r , int c )
- {
- int m;
- if ( tree[v].l==l && tree[v].r==r )
- {
- tree[v].c=c;
- return;
- }
- if ( tree[v].c>0 && tree[v].c!=c )
- {
- tree[v*2].c=tree[v].c;
- tree[v*2+1].c=tree[v].c;
- tree[v].c=0;
- }
- m=(tree[v].l+tree[v].r)>>1;
- if ( r<=m )
- ins(v*2,l,r,c);
- else
- if ( l>m )
- ins( v*2+1,l,r,c);
- else
- {
- ins(v*2,l,m,c);
- ins(v*2+1,m+1,r,c);
- }
- }
- void sum ( int v )
- {
- if ( tree[v].c )
- {
- if ( !vis[tree[v].c] )
- {
- vis[tree[v].c]=true;
- cnt++;
- }
- return;
- }
- sum(v*2);
- sum(v*2+1);
- }
- int main ( )
- {
- int n,N,i;
- scanf("%d",&N);
- while ( N-- )
- {
- cnt=0;
- scanf("%d",&n);
- memset(vis,false,sizeof(vis));
-
- for ( i=0 ; i<n ; i++ )
- {
- scanf("%d%d",&post[i][0],&post[i][1]);
- 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;
- }
-
- sort(line,line+n*2);
- int tp=1,temp=line[0].li;
-
- for(i=0;i<2*n;i++)
- {
- if(line[i].li!=temp)
- {
- tp++;
- temp=line[i].li;
- }
- if(line[i].num<0)
- post[-line[i].num-1][0]=tp;
- else
- post[line[i].num-1][1]=tp;
- }
- build(1,1,tp);
- for ( i=0 ; i<n ; i++ )
- ins(1,post[i][0],post[i][1],i+1);
- sum(1);
- printf("%d\n",cnt);
- }
- }
渐渐掌握线段树了。。。O(∩_∩)O哈哈~,接下来学习扩展的线段树了~~加油~
发表于 @
2008年10月23日 23:15:00 | | 编辑|
举报| 收藏