地址: http://poj.org/problem?id=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:
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.
- 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.
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
给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张?
海报最多10000张,但是墙有10000000块瓷砖长,海报不会落在瓷砖中间。
题意很简单,但是如果是直接建立线段树的话肯定是超内存的,开始的时候不知道离散化,借鉴了kuangbin的代码才了解,用到离散化思想,其实离散化就是通过哈希的方式(这里用到的是最简单的哈希表,也就是直接映射的方式),把原来大的应射程小的,再根据映射完成建树过程
具体思路就是线段树+离散化
代码:
#include<stdio.h> #include<algorithm> #include<math.h> using namespace std; const int MAXN=10010; struct Cpost { int l,r; } posters[MAXN]; int x[MAXN*2]; int hash[10000005]; struct Node { int l,r; bool bCovered;//标记是否被完全覆盖 }segTree[MAXN*8]; void Build(int i,int l,int r)//建立线段树 { segTree[i].l=l , segTree[i].r=r , segTree[i].bCovered=false; if(l==r) return; int mid=(l+r)>>1; Build(i<<1,l,mid); Build(i<<1|1,mid+1,r); } bool query(int i,int l,int r)//贴上一个海报,同时判断是否被完全覆盖 返回是否已经覆盖 true为未覆盖 { if(segTree[i].bCovered) return false; ///如果到当前节点已经出现了覆盖的话,肯定就是已经覆盖了 if( segTree[i].l==l&&segTree[i].r==r ) { segTree[i].bCovered=true; return true; } bool bResult; int mid=(segTree[i].l+segTree[i].r)>>1; if(r<=mid) bResult=query(i<<1,l,r); else if(l>mid) bResult=query(i<<1|1,l,r); else // bResult = query(i<<1,l,mid) || query(i<<1|1,mid+1,r);//不能直接或上去,因为如果前面的真,后面的会不做的、、 { bool b1=query(i<<1,l,mid); bool b2=query(i<<1|1,mid+1,r); bResult=b1||b2; } //这个很重要,要反馈回原结点,如果左右儿子都被完全覆盖了,自然也完全覆盖了 if(segTree[i<<1].bCovered && segTree[i<<1|1].bCovered) ///i<<1|1 == i*2+1 segTree[i].bCovered=true; return bResult; } int main() { int T; int i,j,k; int n; scanf("%d",&T); while(T--) { scanf("%d",&n); int nCount=0; for(i=0; i<n; i++) { scanf("%d%d",&posters[i].l,&posters[i].r); x[nCount++]=posters[i].l; x[nCount++]=posters[i].r; } sort(x,x+nCount);//先排序 nCount=unique(x,x+nCount)-x; ///去除的是相邻的重复元素 返回的是num去重后的尾地址 for(i=0; i<nCount; i++) hash[ x[i] ] = i; ///建立哈希表 Build(1,0,nCount-1); int res=0; for(i=n-1; i>=0; i--) ///要从上面开始判断 if( query( 1 , hash[ posters[i].l ] , hash[ posters[i].r ]) ) ///从根节点向下看 res++; printf("%d ",res); } return 0; }
最后贴一下关于线段树+离散化的小拓展:
平衡树可以做的事情其实线段数也可以做。