Test instructions: Put a poster on the wall, give each of the intervals posted, ask the last to see a few posters. idea: Segment tree, but the given interval length is 1000000, must be discretized processing. discretization: The order of all points;Remove duplicate points, add a number between two numbers that are greater than one (avoid errors), sort, and find the position of the left and right intervals (i.e., the values corresponding to the discretization)segment tree: Can be solved with a template (a variable name is wrong, debugging a noon, whining ~ ~ Temperature failure)
AC Code:
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace STD; #define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1#define Maxx 10005int X[maxx<<2],col[maxx <<4],li[maxx],ri[maxx];bool flag[maxx];int n,m,ans;int bsearch (int key) {int left=0,right=m-1,mid; while (left<=right) {mid= (left+right) >>1; if (X[mid]==key) return mid; if (X[mid]<key) left=mid+1; else right=mid-1; } return-1;} void pushdown (int rt) {if (col[rt]!=-1) {col[rt<<1]=col[rt]; col[rt<<1|1]=col[rt]; Col[rt]=-1; }}void Update (int left,int right,int c,int l,int r,int RT) {if (left<=l&&right>=r) {col[rt]=c; Return } pushdown (RT); int mid= (L+R) >>1; if (left<=mid) update (Left,right,c,lson); if (right>mid) update (Left,right,c,rson);} void query (int l,int r,int RT) {if (col[rt]!=-1) {if (!flag[col[rt]]) ans++; Flag[col[rt]]=1; Return } if (L==R) return; int mid= (L+R) >>1; Query (Lson); Query (Rson);} int main () {int t; scanf ("%d", &t); while (t--) {Ans=0;memset (col,-1,sizeof (col)); memset (flag,0,sizeof (flag)); scanf ("%d", &n); for (int i=0;i<n;i++) scanf ("%d%d", &li[i],&ri[i]); int nn=0; for (int i=0;i<n;i++) {x[nn++]=li[i]; X[nn++]=ri[i]; } sort (x,x+nn); m=1; for (int i=1;i<nn;i++) {if (x[i]!=x[i-1]) x[m++]=x[i]; } for (int i=m-1;i>=0;i--) {if (x[i]!= (x[i-1]+1)) x[m++]=x[i-1]+1; } sort (x,x+m); for (int i=0;i<n;++i) {int l=bsearch (li[i]); int R=bsearch (ri[i]); Update (l,r,i,0,m-1,1); } query (0,m-1,1); printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2528 (segment tree + discretization)