Mayor ' s Posters
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 45031 |
|
Accepted: 13080 |
Description
The citizens of Bytetown, AB, could not stand then the candidates in the mayoral election campaign has been placing their Electoral posters at all places at their whim. The city council have finally decided to build a electoral wall for placing the posters and introduce the following rules:
- Every candidate can place exactly one poster on the wall.
- All posters is of the same height equal to the height of the wall; The width of a poster can be any integer number of bytes (byte was the unit of length in Bytetown).
- The wall is divided to segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They has built a wall 10000000 bytes long (such that there are enough place for all candidates). When the electoral campaign is restarted, the candidates were placing their posters on the wall and their posters differe D widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown is curious whose posters would 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 was placed given the information about posters ' Si Ze, 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 the integer numbers Li and ri which is the number of the wall segment occupied b Y 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 are 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 is placed.
The picture below illustrates the case of the sample input.
Sample Input
151 42 68 103 47 10
Sample Output
4
This problem has also been done for a long time. First of all, each number can represent a lattice, the last not covered by the number of squares, the same period of the lattice to calculate one. Discretization of the interval, but there is a problem that can lead to errors.
For example [1,10], [1 4], [6 10], after discretization is {1,4,6,10} corresponding to the following table is {0,1,2,3}, the interval becomes [0,3][0,1][2,3], the first interval is completely covered, but the first paragraph in the original interval will not be completely covered. Therefore, you can add a number in the middle of the number of adjacent two difference values greater than 1, so that they do not go wrong.
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < Vector> #include <algorithm>using namespace std; #define N 10005#define ll long longstruct node{int x,id;} A[N*4]; 2*n endpoints + (2*n-1) int pos[n][2],ans;struct st{int l,r,x; x record color, initialize -1}f[n*20];int mark[n*4];bool cmp (node A,node b) {return a.x<b.x;} void creat (int t,int l,int r) {f[t].l=l; F[t].r=r; F[t].x=-1; if (l==r) return; int tmp=t<<1,mid= (L+R) >>1; Creat (Tmp,l,mid); Creat (tmp|1,mid+1,r);} void update (int t,int l,int r,int x) {if (L==F[T].L&&R==F[T].R) {f[t].x=x; return; } int tmp=t<<1,mid= (F[T].L+F[T].R) >>1; if (f[t].x!=-1) {f[tmp].x=f[tmp|1].x=f[t].x; F[t].x=-1; } if (mid>=r) update (TMP,L,R,X); else if (l>mid) update (TMP|1,L,R,X); else {update (TMP,L,MID,X); Update (TMP|1,MID+1,R,X); }}void query (int t, int l,int r) {int tmp=t<<1,mid= (F[T].L+F[T].R) >>1; if (l==r) {if (!mark[f[t].x]&&f[t].x!=-1)//note here {mark[f[t].x]=1; ans++; } return; } if (f[t].x!=-1) {f[tmp].x=f[tmp|1].x=f[t].x; F[t].x=-1; } if (r<=mid) query (TMP,L,R); else if (l>mid) query (TMP|1,L,R); else {query (TMP,L,MID); Query (TMP|1,MID+1,R); }}int Main () {int i,t,n; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (i=0;i<n;i++) {scanf ("%d%d", &pos[i][0],&pos[i][1]);//left and right border a[i*2].x=pos[i][0]; a[i*2].id=-(i+1); A[I*2+1].X=POS[I][1]; a[i*2+1].id=i+1; } sort (a,a+n*2,cmp); int m=n*2; for (i=m-1;i>0;i--)//Add number, {if (a[i].x-a[i-1].x>1) {a[m].x=a[i].x-1; A[m++].id=0; ID set to 0 and original interval difference }} sort (a,a+m,cmp); int t=1,x=a[0].x; T is the interval renumber for (i=0;i<m;i++) {if (x!=a[i].x) {t++; x=a[i].x; } if (a[i].id<0) pos[-a[i].id-1][0]=t; else if (a[i].id>0) pos[a[i].id-1][1]=t; } memset (Mark,0,sizeof (Mark)); Creat (1,1,t); for (i=0;i<n;i++) {int l=pos[i][0]; int r=pos[i][1]; Update (1,L,R,I); } ans=0; Query (1,1,T); printf ("%d\n", ans); } return 0;}
POJ 2528 Mayor ' s Posters (segment tree + interval discretization)