Description
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
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
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
线段树 区间更新 区间求和 + 离散化
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxn 44000 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 struct Node{ int L,R; }num[11000]; int cnt[maxn],Hash[maxn]; int flag[maxn<<2]; int find(int star,int last,int x) { int m=(star+last)>>1; while(star<=last) { if(Hash[m]==x) return m; if(Hash[m]>x) last=m-1; else star=m+1; m=(star+last)>>1; } return m; } void PushDown(int rt) { flag[rt<<1]=flag[rt<<1|1]=flag[rt]; flag[rt]=0; } void build(int l,int r,int rt) { flag[rt]=0; if(l==r) { return ; } int m=(l+r)>>1; build(lson); build(rson); } void updata(int l,int r,int rt,int L,int R,int x) { if(l>=L && r<=R) { flag[rt]=x; return ; } if(flag[rt]) PushDown(rt); int m=(l+r)>>1; if(L<=m) updata(lson,L,R,x); if(R>m) updata(rson,L,R,x); } void query(int l,int r,int rt) { if(l==r) { cnt[flag[rt]]=1; return ; } if(flag[rt]) PushDown(rt); int m=(l+r)>>1; query(lson); query(rson); } int main() { int T,N; int i; scanf("%d",&T); while(T--) { scanf("%d",&N); for(i=1;i<=N;i++) { scanf("%d%d",&num[i].L,&num[i].R); cnt[2*i-1]=num[i].L; cnt[2*i]=num[i].R; } sort(cnt+1,cnt+2*N+1); int j=1; Hash[1]=cnt[1]; for(i=2;i<=2*N;i++) //去重 { if(cnt[i]!=Hash[j]) { if(cnt[i]>Hash[j]+1) Hash[j+1]=Hash[j++]+1; Hash[++j]=cnt[i]; } } for(i=1;i<=N;i++) //离散化 { num[i].L=find(1,j,num[i].L); num[i].R=find(1,j,num[i].R); } build(1,j,1); for(i=1;i<=N;i++) { updata(1,j,1,num[i].L,num[i].R,i); } memset(cnt,0,sizeof(cnt)); query(1,j,1); int s=0; for(i=1;i<=j;i++) { if(cnt[i]) s++; } printf("%d\n",s); } return 0; }