题意:
在1~10000000这个区间中读取n个海报的区间信息,后面的海报会覆 盖前面的海报,问最后能看到几张海报.(本题是一道bug题下面会提)
题目:
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
分析
分析:先离散化、再线段树。
【1】离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率。
数据的离散化有些数据本身很大, 自身无法作为数组的下标保存对应的属性。如果这时只是需要这堆数据的相对属性, 那 么可以对其进行离散化处理。当数据只与它们之间的相对大小有关,而与具体是多少无关时,可以进行离散化。
(1)离散化,将所有出现过的点存储在排序,然后去掉相同的点。如下面的例子 (题目的样例),因为单位1是一个单位长度,将下面的
1 2 3 4 6 7 8 10
— — — — — — — —
1 2 3 4 5 6 7 8
离散化 X[1] = 1; X[2] = 2; X[3] = 3; X[4] = 4; X[5] = 6; X[7] = 8; X[8] = 10
于是将一个很大的区间映射到一个较小的区间之中了,然后再对每一张海报依 次更新在宽度为1~8的墙上(用线段树),最后统计不同颜色的段数。
1.先把每条线段的单独存下来,同时把每个端点的值存在另一个数组里,
在此把图放上for(int i=1; i<=m; i++){scanf("%d%d",&a[i],&b[i]);c[k++]=a[i];c[k++]=b[i];}
2.来一发排序,sort即可,线段树是以点的形式存储了线段,所以难免会有局限,(在这里我们只看点,其实
是有问题的,正确着做反而是错误的,因为若【2,3】,【4,5】,线段树只对点操作,认为是连续的,实际
不连续,所有应该两次sort,第一次需判断两点之间是否间隔为一,若是在两点之间加一个数,第二次sort
去重。)sort(c,c+k);
3.然后把数组中重复的元素去掉,因为接下来我们要用线段树查找来确定每条线段的标号,
去重才能保证标号唯一。for(int i=0; i<k; i++){if(c[i]!=c[i+1])d[c[i]]=++tot;}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e4+10;//线段树开4倍空间
const int N=1e7+10;
int a[M],b[M],c[M*2],d[N],dp[M*8],add[M*8],book[M*2],ans;
void dfs(int o)
{if(add[o])//懒惰标记下移{add[o<<1]=add[o<<1|1]=add[o];dp[o<<1]=dp[o<<1|1]=add[o];add[o]=0;}
}
void update(int o,int l,int r,int x,int y,int c)//区间更新
{if(x<=l&&y>=r)/*对值覆盖*/{add[o]=c;dp[o]=c;return ;}dfs(o);int mid=(r+l)>>1;if(x<=mid)update(o<<1,l,mid,x,y,c);if(y>mid)update(o<<1|1,mid+1,r,x,y,c);
}
void query(int o,int l,int r)
{if(l==r){if(!book[dp[o]]){ans++;book[dp[o]]=1;}return ;}dfs(o);int mid=(l+r)>>1;query(o<<1,l,mid);query(o<<1|1,mid+1,r);
}
int main()
{int t;scanf("%d",&t);while(t--){int m,k=0,tot=0;memset(book,0,sizeof(book));memset(add,0,sizeof(add));scanf("%d",&m);for(int i=1; i<=m; i++){scanf("%d%d",&a[i],&b[i]);c[k++]=a[i];c[k++]=b[i];}sort(c,c+k);/*排序操作,目的去重*/for(int i=0; i<k; i++){if(c[i]!=c[i+1]) /*离散化操作,去重*/d[c[i]]=++tot;}for(int i=1; i<=m; i++){int x=d[a[i]];int y=d[b[i]];update(1,1,tot,x,y,i);//由于每次的海报不同所以直接给海报一个编号}ans=0;query(1,1,tot);printf("%d\n",ans);}return 0;
}