We know that the middle sequence traversal of a binary search tree is an already sequenced sequence, knowing that we cannot determine the shape of the tree (because there are many).
However, if Treap tells us its keywords and weights, then it is possible to uniquely determine the shape of the tree (Treap's O (logn) 's desired time complexity is not too deep to rely on a random heap depth)
Specific, known keyword sequence: k1,k2,k3...kn and priority sequence: P1,P2,P3,... pn,
If we want to find Ki's father, we just need to look for "the first p on the left is bigger than it and the first p on the right is bigger than it, the smaller of the P"
As for LCA (KI,KJ), it is the minimum value in the corresponding PI~PJ (because it is a small Gan)
As for the depth, many times to find the father, the path length is depth, and then draw one can find the characteristics of the trajectory, and then you can do (to find two increments of the length of the sub-sequence)
1 /**************************************************************2 problem:27703 user:idy0024 language:c++5 result:accepted6 time:6140 Ms7 memory:46124 KB8 ****************************************************************/9 Ten#include <cstdio> One#include <vector> A#include <algorithm> - #defineOO 0x6fffffff - #defineMAXN 400010 the using namespacestd; - -typedef pair<int,int>DPR; - + structNode { - intLF, RG, mid; + DPR St; A BOOLLeaf; atNode *ls, *rs; -} pool[maxn*3], *tail=pool, *Root; -Vector<node*>Stk; - - intDISC[MAXN], Ntot; - intSV[MAXN]; in intpr[maxn][3]; - toNode *build (intLfintRG) { +Node *nd = + +tail; -ND->LF=LF, Nd->rg=rg, nd->mid= (LF+RG) >>1; the if(lf==RG) { *Nd->st = DPR (OO,0 ); $Nd->leaf =true;Panax Notoginseng}Else { -Nd->ls = Build (LF, nd->mid); theNd->rs = Build (nd->mid+1, RG); +Nd->st = DPR (OO,0 ); ANd->leaf =false; the } + returnnd; - } $DPR Qu_min (Node *nd,intLfintRG) { $ if(LF <= nd->lf && nd->rg <= RG)returnNd->St; -DPR RT = DPR (OO,0 ); - if(LF <= nd->mid) theRT = Qu_min (nd->ls, LF, RG); - if(RG > Nd->mid)WuyiRT = min (RT, Qu_min (nd->RS, LF, RG)); the returnRT; - } Wu voidPushup (Node *nd) { -nd->st = min (nd->ls->st, nd->rs->St); About } $ voidModify (Node *nd,intPosintval) { - if(nd->leaf) { -Nd->st =DPR (Val, POS); - return; A } + if(Pos <= nd->mid) theModify (nd->ls, POS, val); - Else $Modify (nd->RS, POS, Val); the pushup (ND); the } the intLcaintUintv) { the if(u>v) swap (u,v); - returnqu_min (Root, U, v). Second; in } the the intMain () { About intN, M, T; thescanf"%d%d", &n, &m); theT = n+m; the for(intI=1; i<=n; i++ ) { +pr[i][0] =0; -scanf"%d", pr[i]+1 ); theDisc[++ntot] = pr[i][1];Bayi } the for(intI=1; i<=n; i++ ) thescanf"%d", pr[i]+2 ); - for(inti=n+1; i<=t; i++ ) { - Charopt[Ten]; thescanf"%s", opt); thepr[i][0] = opt[0]=='I'?0 : theopt[0]=='D'?1:2; the if(pr[i][0]==0 ) { -scanf"%d%d", pr[i]+1, pr[i]+2 ); theDisc[++ntot] = pr[i][1]; the}Else if(pr[i][0]==1 ) { thescanf"%d", pr[i]+1 );94}Else { thescanf"%d%d", pr[i]+1, pr[i]+2 ); the } the }98Sort (disc+1, disc+1+Ntot); AboutNtot = Unique (disc+1, disc+1+ntot)-Disc-1; - for(intI=1; i<=t; i++ ) {101pr[i][1] = Lower_bound (disc+1, disc+1+ntot, pr[i][1] )-disc;102 if(pr[i][0]==2 )103pr[i][2] = Lower_bound (disc+1, disc+1+ntot, pr[i][2] )-disc;104 } theRoot = Build (1, Ntot);106 for(intI=1; i<=t; i++ ) {107 if(pr[i][0]==0 ) {108Modify (Root, pr[i][1], pr[i][2] );109}Else if(pr[i][0]==1 ) { theModify (Root, pr[i][1], oo);111}Else { the intU = pr[i][1];113 intv = pr[i][2]; theprintf"%d\n", Disc[lca (u,v)]); the } the }117}
View Code
Bzoj of the sequence traversal properties of the 2770 heap