Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32662 | Accepted: 9488 |
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
http://poj.org/problem?id=2528
表示很坑爹的,Runtime Error6次,后来发现数组开小,是开QLEN的大小才对。
表示弱.......
离散化+二分+线段树延迟标记
亮点
{
离散化:
poj很多人理解这道题离散化的方法错误的才能过。有人这样举例了:
3 3 5 6 4 5 6 8 3 1 10 1 3 6 10 5 1 4 2 6 8 10 3 4 7 10
正确答案应该是: 3 3 4 很多人的答案是: 2 2 4 不知POJ为啥过了??
我问这样的答案 正确么?我的答案是 2 3 4
我想是对图的不可理,或者是常规的对[l,r]的是第一种答案,但是这道题要看清图啊...。
}
#include<stdio.h> #include<iostream> #include<cstdlib> #include<algorithm> using namespace std; #define HH 1 struct st { int l; int r; int num; int color; }f[50004*6]; int a[10003],b[10004],ss[50005],sslen,visit[50005],q[50002],qlen; void change() { int i; qlen=0; q[++qlen]=ss[1]; for(i=2;i<=sslen;i++) { if((ss[i]-ss[i-1])>1) q[++qlen]=ss[i-1]+1; if(q[qlen]!=ss[i]) q[++qlen]=ss[i]; } } int EF(int l,int r,int num) { int mid=(l+r)/2; while(l<=r) { if(q[mid]>num) r=mid-1; else if(q[mid]<num) l=mid+1; else if(q[mid]==num) return mid; mid=(l+r)/2; } return mid; } void build(int l,int r,int n) { int mid=(l+r)/2; f[n].l=l; f[n].r=r; f[n].color=0; f[n].num=0; if(l==r) return; build(l,mid,n*2); build(mid+1,r,n*2+1); } void down(int n) { f[n*2].color=f[n*2+1].color=HH; f[n*2].num =f[n*2+1].num =f[n].num; f[n].color=0; f[n].num=0; } void update(int l,int r,int num,int n) { int mid=(f[n].l+f[n].r)/2; if(f[n].l==l&&f[n].r==r) { f[n].color=HH; f[n].num=num; return; } if(f[n].color==HH) down(n); if(mid>=r) update(l,r,num,n*2); else if(mid<l) update(l,r,num,n*2+1); else { update(l,mid,num,n*2); update(mid+1,r,num,n*2+1); } } void query(int l,int r,int n) { int mid=(f[n].l+f[n].r)/2; if(f[n].l==l&&f[n].r==r) { if(f[n].color==HH) { visit[f[n].num]=1; return; } } if(f[n].l==f[n].r) return ; if(f[n].color==HH) down(n); if(mid>=r) query(l,r,n*2); else if(mid<l) query(l,r,n*2+1); else { query(l,mid,n*2); query(mid+1,r,n*2+1); } } int main() { int i,j,k,n,m,l,r; while(scanf("%d",&n)>0) { while(n--) { scanf("%d",&m); sslen=0; for(i=1;i<=m;i++) { scanf("%d%d",&a[i],&b[i]); ss[++sslen]=a[i]; ss[++sslen]=b[i]; } sort(ss+1,ss+sslen+1); change(); build(1,qlen,1); for(i=0;i<=qlen;i++) visit[i]=0; for(i=1;i<=m;i++) { l=EF(1,qlen,a[i]); r=EF(1,qlen,b[i]); update(l,r,i,1); } query(1,qlen,1); for(i=1,j=0;i<=qlen;i++) if(visit[i]==1) j++; printf("%d\n",j); } } return 0; }