ACM专题学习|Mayor‘s posters--线段树(区间修改)+离散化

ACM专题学习五
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.
ACM专题学习|Mayor‘s posters--线段树(区间修改)+离散化
文章图片

Sample Input

1 5 1 4 2 6 8 10 3 4 7 10

Sample Output
4

题意 给出n张海报贴在一面墙上,每张海报都覆盖一个范围,问最后可以看到多少张海报
分析 有最多10000张海报,也就是说最多出现20000个点,直接存数组建树的话会太大太慢,所以可以使用离散化,将每个点离散后,重新对给出区间,这样区间最大是1到20000。可以直接使用线段树,每次更新一段,最后遍历所有的段,标记可见海报并统计。为节省时间,用懒标记。
下面用一种离散化的方法
把铺的区间和查询区间的端点(这题没有查询)都放入数组进行离散化,这里用结构体数组p[i].k,比较好表示。然后用p[i].id1存次序,再按p[i].k升序排序,用p[i].id2存排序后的次序,最后用数组a存p[i].id1对应的p[i].id2。
样例演示
0 1 2 3 4 5 6789
p[i].k1 2 8 3 7 4 6 10 4 10
p[i].id11 2 3 4 5 6 789 10
排序
p[i].k1 2 3 4 4 6 7 8 10 10
p[i].id21 2 3 4 4 5 6 788
【ACM专题学习|Mayor‘s posters--线段树(区间修改)+离散化】a[i]1 2 7 3 6 4 5 848

代码
#include #include #include #define maxn 30000 using namespace std; int c[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 up(int l,int r,int rt)//下分支相同则标记为海报次序,不同则标记为0 { if( !c[rt<<1] || !c[rt<<1|1] || c[rt<<1] != c[rt<<1|1] ) c[rt] = 0 ; else c[rt] = c[rt<<1|1] ; } void down(int l,int r,int rt)//遇到懒标记就往下更新一次 { if( lazy[rt] != -1 )//lazy[rt]=-1说明没有铺过这里,不用再向下更新 {//lazy[rt]!=-1说明之前铺到了这里,下面铺了或没铺,反正要往下走,先向下更新一次就对了 lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt] ; c[rt<<1] = c[rt<<1|1] = lazy[rt] ; lazy[rt] = -1 ; } } void update(int ll,int rr,int x,int l,int r,int rt) { if( ll > r || rr < l ) return ; if( ll <= l && r <= rr ) { c[rt] = lazy[rt] = x ; //懒标记 return ; } down(l,r,rt); update(ll,rr,x,l,(l+r)/2,rt<<1); update(ll,rr,x,(l+r)/2+1,r,rt<<1|1); up(l,r,rt); } void query(int ll,int rr,int l,int r,int rt,int *a) { if( c[rt] == -1 ) return ; else if(c[rt] > 0)//看得见海报的地方,就标记海报序号 { a[ c[rt] ] = 1 ; return ; } // down(l,r,rt); query(ll,rr,l,(l+r)/2,rt<<1,a); query(ll,rr,(l+r)/2+1,r,rt<<1|1,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); //输入l,r离散点 p[i].id1 = i ; //id1 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 )//重复数字不更新id2 p[i].id2 = n ; //id2 else { p[i].id2 = ++n ; temp = p[i].k ; } a[ p[i].id1 ] = p[i].id2 ; //新数组存 } memset(c,-1,sizeof(c)); memset(lazy,-1,sizeof(lazy)); for(i = 0 ; i < m ; i++) { update(a[i],a[i+m],i+1,1,n,1); //铺海报 } memset(a,0,sizeof(a)); //a数组用完了,清空回收再利用(再开另一个数组也可以) query(1,n,1,n,1,a); //记录可见海报 int num = 0; for(i = 1 ; i <= m ; i++) if(a[i])//数可见海报数 num++ ; printf("%d\n", num); } return 0; }

    推荐阅读