Mayor ' s Posters
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 54067 |
|
Accepted: 15713 |
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 ' 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 the integer numbers liand RI which is the number of the wall segment occupied by the left end and the right end of the i-th poster, Respec Tively. 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
Solution: With color section that problem is very similar, but wrote the next WA, finally with the help of bin God's ideas to write out;
AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <vector>using namespace std;const int inf=0x3f3f3f3f; #define MEM (x, y) memset (x,y,sizeof (×)) # Define SI (x) scanf ("%d", &x) #define PI (x) printf ("%d", x) #define SD (y) scanf ("%lf%lf", &x,&y) #define P_ printf ("") #define LL Root<<1#define RR root<<1|1#define lson ll,l,mid#define rson rr,mid+1,r#define V (x) Tre E[x]typedef Long Long ll;const int maxn=100010;bool tree[maxn<<2];int h[10000010],seg[maxn<<1];struct Node {int A, B; Node init (int c,int d) {a=c;b=d;}}; Node dt[maxn];void Build (int root,int l,int r) {V (root) =false;int mid= (l+r) >>1;if (l==r) return;build (Lson); build (Rson);} BOOL Query (int root,int l,int r,int a,int B) {int mid= (l+r) >>1;if (V (Root)) return false;//the segment tree is accessed from the top, the covered segment is true, Returns false bool Bcover;if (l==a&&r==b) {V (root) =true;return true;} if (mid>=b) bcover=query (lson,a,b), else if (mid<a) bcover=query (Rson, b); Else{int b1=query (Lson,a,mid);//int b2=query (rson,mid+1,b);//bcover=b1| | B2;} if (V (LL) &&v (RR)) V (root) =true;return bcover;} int main () {int t,n;si (T), while (t--) {si (N), int a,b;int len=0,val=0;for (int i=0;i<n;i++) {si (a), si (b);d T[i].init (A, b); seg[len++]=a;seg[len++]=b;} Sort (seg,seg+len), int k=unique (Seg,seg+len)-seg;for (int i=0;i<k;i++) H[seg[i]]=i;int ans=0;build (1,0,k-1); for ( int i=n-1;i>=0;i--) {//from top down; if (query (1,0,k-1,h[dt[i].a],h[dt[i].b])) ans++;} printf ("%d\n", ans);} return 0;}
WA Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <vector>using namespace std;const int inf=0x3f3f3f3f; #define MEM (x, y) memset (x,y,sizeof (×)) # Define SI (x) scanf ("%d", &x) #define PI (x) printf ("%d", x) #define SD (y) scanf ("%lf%lf", &x,&y) #define P_ printf ("") #define LL Root<<1#define RR root<<1|1#define lson ll,l,mid#define rson rr,mid+1,r#define V (x) Tre E[x]typedef Long Long ll;const int maxn=20010;int color[maxn];int temp;int tree[maxn<<2];int seg[MAXN];struct Node {int A, B; Node init (int c,int d) {a=c;b=d;}}; Node dt[maxn];void pushdown (int root) {if (V (root) >0) {V (ll) =v (root); V (RR) =v (root); V (root) =-1;}} void build (int root,int l,int r) {int mid= (L+R) >>1; V (Root) =0;if (l==r) return;build (Lson); build (Rson);} void update (int root,int l,int r,int a,int B,int v) {if (l>=a&&r<=b) {V (root) =v;return;} int mid= (l+r) >>1;pushdown (Root), if (mid>=a) update (LSON,A,B,V), if (mid< B) update (RSON,A,B,V); V (root) =-1;} void query (int root,int l,int r) {int mid= (l+r) >>1;if (temp==v (Root)) return;if (! V (Root)) {Temp=0;return;} if (root) {!=-1) {temp!=v (root) {temp=v (root); color[temp]++;return;} return;} if (l==r) return;query (Lson); query (Rson);} int main () {int t,n;si (T), while (t--) {mem (color,0), Si (N), int a,b;int len=0;for (int i=0;i<n;i++) {si (a); Si (b);d T[i]. Init (A, b); seg[len++]=a;seg[len++]=b;} Sort (seg,seg+len), int k=unique (Seg,seg+len)-seg;build (1,1,k), for (int i=0;i<n;i++) {A=lower_bound (seg,seg+k,dt[ I].A)-seg;b=lower_bound (seg,seg+k,dt[i].b)-seg;update (1,1,k,a+1,b,i+1);} Temp=0;query (1,1,k); int ans=0;for (int i=1;i<=n;i++) {if (color[i]) ans++;} printf ("%d\n", ans);} return 0;}
Mayor ' s posters (discrete segment tree)