http://poj.org/problem?id=2528
#include <cstdio>#include<iostream>#include<Set>#include<cstring>#include<string>#defineLeft Rt<<1#defineRight rt<<1|1using namespacestd;Const intMAXN =32768+5;intSETV[MAXN <<2]; //Very unscientific, here must open big one o'clock only AC, according to reason say 2*maxn-1 space is enough ... WA to crying inthash[10000005];structinterval{intL, R;} a[10005];BOOLcolor[10005];intans;voidPushdown (intRT) { if(SETV[RT]! =-1) {Setv[left]= Setv[right] =Setv[rt]; SETV[RT]= -1; } }voidUpdateintQlintQrintXintRtintLintR) { if(QL <= l && R <=qr) {Setv[rt]=x; return; } pushdown (RT); intm = (L + r) >>1; if(QL <=m) Update (QL, QR, X, left, L, M); if(QR > M) update (QL, QR, X, right, m+1, R);}voidQueryintQlintQrintRtintLintR) { if(SETV[RT]! =-1) { if(!Color[setv[rt]]) {ans++; COLOR[SETV[RT]]=1; } return; } if(L==R)return; Pushdown (RT); intm = (L + r) >>1; if(QL <=m) query (QL, QR, left, L, M); if(QR > M) query (QL, QR, right, m+1, R);}intMain () {intc, N; scanf ("%d", &c); while(c--) {scanf ("%d", &N); Set<int>St; for(intI=0; i<n; i++) {scanf ("%d%d", &A[I].L, &A[I].R); St.insert (A[I].L); St.insert (A[I].R); } intPre, tot=0; for(Set<int>::iterator P=st.begin (); P! = St.end (); ++p) {if(p==St.begin ()) {hash[*P] = + +tot; } Else { if(*p-pre = =1) {hash[*P] = + +tot; } Else{hash[*p-1] = ++tot; hash[*P] = + +tot; }} Pre= *p; } memset (Setv,-1,sizeof(Setv)); memset (color,false,sizeof(color)); for(intI=0; i<n; i++) {update (HASH[A[I].L], HASH[A[I].R], I+1,1,1, tot); } ans=0; Query (1Tot1,1, tot); printf ("%d\n", ans); } return 0;}
A more space-saving version, eliminating the hash array, because it is already sorted, so you can find the corresponding subscript by two points:
#include <cstdio>#include<iostream>#include<Set>#include<cstring>#include<string>#include<algorithm>#defineLeft Rt<<1#defineRight rt<<1|1using namespacestd;Const intMAXN =32768+5;intSETV[MAXN <<2];structinterval{intL, R;} a[10005];BOOLcolor[10005];intans;intt[10005*3];voidPushdown (intRT) { if(SETV[RT]! =-1) {Setv[left]= Setv[right] =Setv[rt]; SETV[RT]= -1; } }voidUpdateintQlintQrintXintRtintLintR) { if(QL <= l && R <=qr) {Setv[rt]=x; return; } pushdown (RT); intm = (L + r) >>1; if(QL <=m) Update (QL, QR, X, left, L, M); if(QR > M) update (QL, QR, X, right, m+1, R);}voidQueryintQlintQrintRtintLintR) { if(SETV[RT]! =-1) { if(!Color[setv[rt]]) {ans++; COLOR[SETV[RT]]=1; } return; } if(L==R)return; Pushdown (RT); intm = (L + r) >>1; if(QL <=m) query (QL, QR, left, L, M); if(QR > M) query (QL, QR, right, m+1, R);}intBin (intKeyintNintx[]) { intL =1, r =N; while(L <= R) {//[L, R] intm = (l+r) >>1; if(X[m] = = key)returnm; if(X[m] < key) l=m+1; Elser=m-1; } return-1; }intMain () {intc, N; scanf ("%d", &c); while(c--) {scanf ("%d", &N); Set<int>St; for(intI=0; i<n; i++) {scanf ("%d%d", &A[I].L, &A[I].R); St.insert (A[I].L); St.insert (A[I].R); } memset (Setv,-1,sizeof(Setv)); memset (color,false,sizeof(color)); inttot =0; for(Set<int>::iterator P=st.begin (); P! = St.end (); ++p) {t[++tot] = *p; } inttemp_tot=tot; for(intI=2; i<=temp_tot; i++) { if(t[i]-t[i-1] >1) T[++tot] = t[i]-1; } sort (T+1, t+tot+1); for(inti =0; I < n; i++) { intL =Bin (A[I].L, tot, t); intR =Bin (A[I].R, tot, t); Update (L, R, I,1,1, tot); } ans=0; Query (1Tot1,1, tot); printf ("%d\n", ans); } return 0;}
Poj-2528-mayor ' s Posters "segment tree + discretization + fill Point"