Topic Links: http://poj.org/problem?id=2528
thought Analysis: segment tree processing interval coverage problem, also can be regarded as a period of time to dye a different color, and finally to the whole interval of the number of colors contained in the range;
Note that because the interval is too large, it needs to be discretized.
Interval update: For each node of the segment tree, the marker color, initially without color, marked 0; When updating, using the delay tag, the tag is passed to the child node;
interval query: Use depth first to query the segment tree, when a child node color is not 0 o'clock, that is, stop depth first search, and in the map to query whether the interval has been recorded the color;
The code is as follows:
#include <iostream>#include<algorithm>using namespaceStd;typedefstruct{intx, y;} Pos;Const intMax_n =10000+ -;Const intMax_m =10000000+ -;intHash_val[max_m];int Set[ -* Max_n], arr[8*Max_n];intmap[Ten*Max_n]; Pos position[8*Max_n];intans;voidPushdown (into) { intLC =2* O, rc =2* O +1; if(Set[O]! =0) { Set[LC] =Set[RC] =Set[O]; Set[O] =0; }}voidUpdata (intOintLintRintColorintQlintqr) { if(QL <= l && R <=qr)Set[O] =color; Else { intMid = (L + r)/2; Pushdown (o); if(QL <=mid) Updata (2*O, L, Mid, color, QL, QR); if(Mid <qr) Updata (2* O +1, Mid +1, R, Color, QL, QR); }}voidQuery (intOintLintR) { if(Set[O]! =0) { if(map[Set[O]] = =0) {map[Set[O]] =1; Ans++; } } Else if(L <r) {intMid = (L + r)/2; Query (2*O, L, mid); Query (2* O +1, Mid +1, R); }}intMain () {intcase_times, num; intMin_len, Max_len; scanf ("%d", &case_times); while(case_times--) {memset (Set,0,sizeof(Set)); memset (Map,0,sizeof(map)); memset (Hash_val,0,sizeof(Hash_val)); scanf ("%d", &num); for(inti =0; i < num; ++i) {scanf ("%d%d", &arr[2* I], &arr[2* i +1]); position[i].x= arr[2*i]; Position[i].y= arr[2* i +1]; } sort (arr,&arr[2*num]); for(inti =0; I <2* NUM; ++i) {if(i = =0) Hash_val[arr[i]]=1; Else if(Arr[i] = = Arr[i-1]) Hash_val[arr[i]]= Hash_val[arr[i-1]]; Else if(Arr[i] = = Arr[i-1] +1) Hash_val[arr[i]]= Hash_val[arr[i-1]] +1; ElseHash_val[arr[i]]= Hash_val[arr[i-1]] +2; } Min_len=1; Max_len= hash_val[arr[2* Num-1]]; for(inti =0; i < num; ++i) {intQL =hash_val[position[i].x]; intQR =HASH_VAL[POSITION[I].Y]; Updata (1, Min_len, Max_len, i +1, QL, QR); } ans=0; Query (1, Min_len, Max_len); printf ("%d\n", ans); } return 0;}
POJ 2528 Mayor ' s posters (segment tree)