Title Link: http://poj.org/problem?id=2528
Give you n pieces of wood, and each plank has its starting and ending points, placed in order and asked to see a few pieces of wood at the end.
It is obvious that the line tree interval update problem, each time the board is placed to update the values in the interval. Because the range of L and R is large, memory is not enough, so using discretization techniques such as 1 4 to 1 2, the range shrinks, but does not affect the answer.
After writing this question, the understanding of the interval update is a bit deepened, focusing on the understanding of coverage (update the left and right two child nodes, then the value is emptied), or more to do the topic.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <map>6 using namespacestd;7 Const intMAXN = 2e4 +5;8 structData {9 intL, R, sum;Ten}T[MAXN <<2]; OneMap <int,int>MP; A intX[MAXN], Y[MAXN], cont, ID[MAXN]; - - voidInitintPintLintr) { the intMid = (L + r) >>1; -T[P].L = L, T[P].R =R; - if(L = =r) { -T[p].sum =0; + return ; - } +Init (P <<1, L, mid); AInit (P <<1)|1, Mid +1, R); atT[p].sum = T[p <<1].sum + t[(P <<1)|1].sum; - } - - intQueryintIndex,intp) { - intMid = (t[p].l + T[P].R) >>1; - if(Index = = T[p].l && T[P].R = =index) { in returnt[p].sum; - } to if(t[p].sum) { +T[p <<1].sum = t[(P <<1)|1].sum =t[p].sum; -T[p].sum =0; the } * if(Index <=mid) { $Query (index, p <<1);Panax Notoginseng } - Else { theQuery (index, p <<1)|1); + } A } the + voidUpdata (intPintLintRintval) { - intMid = (t[p].l + T[P].R) >>1; $ if(T[P].L >= L && t[p].r <= R) {//find the interval, update the interval . $T[p].sum =Val; - return ; - } the if(t[p].sum) {//It is important to note that if this interval is accessed and the interval is to be updated, the value of the interval will be updated to the left and right of the child's node, and the value of this interval will be emptied so as to cover -T[p <<1].sum = t[(P <<1)|1].sum =t[p].sum;WuyiT[p].sum =0; the } - if(R <=mid) { WuUpdata (P <<1, L, R, Val); - } About Else if(L >mid) { $Updata (P <<1)|1, L, R, Val); - } - Else { -Updata (P <<1, L, Mid, Val); AUpdata (P <<1)|1, Mid +1, R, Val); + } the } - $ intMain () the { the intt, N; thescanf"%d", &t); the while(t--) { -scanf"%d", &n); in mp.clear (); theCont =0; the for(inti =1; I <= N; i++) { Aboutscanf"%d%d", x + I, y +i); the if(!Mp[x[i]]) { theId[++cont] =X[i]; themp[x[i]]++; + } - if(!Mp[y[i]]) { theId[++cont] =Y[i];Bayimp[y[i]]++; the } the } -Sort (id +1, ID + cont +1); - intLen =-1;//The largest number after discretization the for(inti =1; I <= N; i++) {//discretization of theX[i] = Lower_bound (id +1, ID + cont +1, X[i])-ID; theLen =Max (X[i], Len); theY[i] = Lower_bound (id +1, ID + cont +1, Y[i])-ID; -Len =Max (Y[i], Len); the } theInit1,1, Len); the for(inti =1; I <= N; i++) {94Updata (1, X[i], y[i], i); the } the mp.clear (); the intres =0;98 for(inti =1; I <= Len; i++) { About inttemp = Query (i,1); - if(!mp[temp] &&temp) {101res++;102mp[temp]++;103 }104 } theprintf"%d\n", res);106 }107}
POJ 2528 Mayor ' s Posters (segment tree interval update + discretization)