"The main topic"
Put a poster on the wall and ask how many posters can you see at the end?
"Attention Point"
1 First note that this is a segment tree, not a point segment tree. Pay attention to the observation chart when reading the question. look at the following set of data in the discuss area:
3
5 6
4 5
6 8
The answer to the above data should be 2, pay attention to the observation chart, covering the interval.
2. discretization
because the coverage is the interval, can not be simple discretization, otherwise there will be errors. For example, the following set of data:
1 5
1 3
4 5
And
1 5
1 2
4 5
If simple discretization becomes:
1 4
1 2
3 4
Finally, we can only see the conclusion of the two posters, and in fact the first set of data can see three posters, in order to solve this problem, if the adjacent two numbers between the difference is greater than 1, fill up with an intermediate number.
For example, 1 4, discretization becomes 1 2 3
3. Data range: Segment tree to open to <<4, as to why has not been studied. X to three times times, because not only to consider the left and right boundaries are put in, but also to consider the middle of the addition of the number of discretization.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #defineLson l,m,rt<<16 #defineRson m+1,r,rt<<1|17 /*There is absolutely no semicolon behind define .*/8 using namespacestd;9 Const intmaxn=10000+ -;Ten intL[MAXN],R[MAXN]; One intx[maxn*3],t,m; A intcol[maxn<<4]; - intHASH[MAXN]; - /*delay Mark overwrite of the current area is numbered*/ the intans; - - voidPushdown (intRT) - { + if(col[rt]!=-1) - { +col[rt<<1]=Col[rt]; Acol[rt<<1|1]=Col[rt]; atcol[rt]=-1; - } - } - - intBin (intkey) - { in intub=1, lb=m+1; - while(lb>UB) to { + intM= (UB+LB)/2; - if(X[m]==key)returnm; the if(X[m]>key) lb=m; * Else*u =m); $ }Panax Notoginseng return-1; - } the + voidUpdateintLintRintLintRintRtintcit) A { the if(L<=l && r<=R) + { -col[rt]=cit; $ return; $ } - pushdown (RT); - intM= (l+r) >>1; the if(L <=m) update (L,R,LSON,CIT); - if(M <R) Update (L,R,RSON,CIT);Wuyi } the - voidQueryintLintRintRT) Wu { - if(col[rt]!=-1) About { $ if(hash[col[rt]]!=1) ans++; -hash[col[rt]]=1; - return; - } A if(L==R)return; + intM= (l+r) >>1; the query (Lson); - query (Rson); $ } the the the voidInit () the /*input and discretization of data*/ - { in intnum=0; thescanf"%d",&t); the for(intI=0; i<t;i++) About { thescanf"%d%d",&l[i],&r[i]); thex[++num]=L[i]; thex[++num]=R[i]; + } -Sort (x+1, x+num+1); the /*m to record the discretization after a total of several numbers*/Bayim=1; the /*Note that because the FOR loop below starts at 2, the initial value of M is 1, otherwise the first number is overwritten .*/ the for(intI=2; i<=num;i++) - { - if(x[i]!=x[i-1]) x[++m]=X[i]; the /*duplicate numbers are recorded only once*/ the } thenum=M; the for(intI=2; i<=num;i++) - { the if(x[i]-x[i-1]>1) x[++m]=x[i]-1; the /*when discretization, be aware that if the two point difference is greater than 1, then insert a number in the middle*/ the }94Sort (x+1, x+m+1); the } the the voidSubmain ()98 { Aboutmemset (col,-1,sizeof(col)); - for(intI=0; i<t;i++)101 {102 intlp=Bin (L[i]);103 intrp=Bin (R[i]);104Update (LP,RP,1M1, i); the /*binary finds the corresponding values after discretization of left and right boundary*/106 }107 }108 109 intMain () the {111 intT; thescanf"%d",&T);113 for(intKase=0; kase<t;kase++) the { the init (); the Submain ();117ans=0;118memset (Hash,0,sizeof(hash));119Query1M1); -cout<<ans<<Endl;121 }122 return 0;123}
"Segment tree + discretization" Poj2528-mayor ' s posters