POJ 2528 线段树成段跟新
Mayor's posters
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 35574 | Accepted: 10323 |
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.
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
由于数据范围很大,需要简单离散化,然后成段跟新,最后计数。
数组版本代码:
#include<iostream> #include<stdio.h> #include<string.h> #include<string> #include<algorithm> #include<map> using namespace std; const int maxn=110010; int col[maxn],a[maxn],b[maxn],c[maxn],ans,hash[maxn]; void pushdown(int p) { if(col[p]!=-1) { col[2*p]=col[2*p+1]=col[p]; col[p]=-1; } } void update(int l,int r,int L,int R,int c,int p) { if(l>=L&&r<=R) { col[p]=c; return; } pushdown(p); int m=(l+r)>>1; if(L<=m)update(l,m,L,R,c,2*p); if(R>m)update(m+1,r,L,R,c,2*p+1); } void query(int l,int r,int p) { if(col[p]!=-1) { if(!hash[col[p]]) { ans++; hash[col[p]]=1; } return; } if(l==r)return; int m=(l+r)>>1; query(l,m,2*p); query(m+1,r,2*p+1); } int main() { int i,j,k,m,n,T; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(col,-1,sizeof(col)); int indexx=0; for(i=1;i<=n;i++) { scanf("%d%d",&b[i],&c[i]); a[indexx++]=b[i]; a[indexx++]=c[i]; } sort(a,a+indexx); indexx=unique(a,a+indexx)-a; // for(i=indexx-1;i>0;i--)if(a[i]!=a[i-1]+1)a[indexx++]=a[i-1]+1; sort(a,a+indexx); for(i=1;i<=n;i++) { j=lower_bound(a,a+indexx,b[i])-a; k=lower_bound(a,a+indexx,c[i])-a; update(0,indexx,j,k,i,1); } memset(hash,0,sizeof(hash));ans=0; query(0,indexx,1); printf("%d\n",ans); } return 0; }
结构体版本代码:
#include<iostream> #include<stdio.h> #include<string.h> #include<string> #include<algorithm> #include<map> using namespace std; const int maxn=20010; #define lson 2*p #define rson 2*p+1 struct node { int l,r,mark; int mid(){return (l+r)>>1;} }tree[5*maxn]; int a[maxn],hash[maxn],b[maxn],c[maxn],ans; void pushdown(int p) { if(tree[p].mark!=-1) { tree[lson].mark=tree[rson].mark=tree[p].mark; tree[p].mark=-1; } } void build(int l,int r,int p) { tree[p].l=l; tree[p].r=r; tree[p].mark=-1; if(l==r)return; int mid=tree[p].mid(); build(l,mid,lson); build(mid+1,r,rson); } void update(int p,int l,int r,int c) { if(tree[p].l>=l&&tree[p].r<=r) { tree[p].mark=c; return; } pushdown(p); int mid=tree[p].mid(); if(l<=mid)update(lson,l,r,c); if(r>mid)update(rson,l,r,c); } void query(int l,int r,int p) { if(tree[p].mark!=-1) { if(!hash[tree[p].mark]) { ans++; hash[tree[p].mark]=1; // cout<<tree[p].mark<<endl; } return; } if(l==r)return; int m=tree[p].mid(); query(l,m,lson); query(m+1,r,rson); } int main() { int T,i,j,k,m,n; scanf("%d",&T); while(T--) { scanf("%d",&n); int indexx=0; for(i=0;i<n;i++) { scanf("%d%d",&b[i],&c[i]); a[indexx++]=b[i]; a[indexx++]=c[i]; } sort(a,a+indexx); indexx=unique(a,a+indexx)-a; // for(i=indexx-1;i>0;i--)if(a[i]!=a[i-1]+1)a[indexx++]=a[i-1]+1; sort(a,a+indexx); //cout<<indexx<<endl; build(1,indexx,1); for(i=0;i<n;i++) { j=lower_bound(a,a+indexx,b[i])-a; k=lower_bound(a,a+indexx,c[i])-a; // cout<<j<<" "<<k<<endl; update(1,j+1,k+1,i); } ans=0; memset(hash,0,sizeof(hash)); query(1,indexx,1); printf("%d\n",ans); } return 0; }

浙公网安备 33010602011771号