forked from bqi343/cp-notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
83 deletions.
There are no files selected for viewing
70 changes: 26 additions & 44 deletions
70
Implementations/09 - Range Queries (2)/15.3 - BBST (4)/Link-Cut Tree (5).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,48 @@ | ||
/** | ||
* Sources: Dhruv Rohatgi, | ||
* https://sites.google.com/site/kc97ble | ||
* /container/splay-tree/splaytree-cpp-3 | ||
* Verification: SPOJ DYNACON1, DYNALCA | ||
*/ | ||
* Sources: Dhruv Rohatgi, | ||
* https://sites.google.com/site/kc97ble | ||
* /container/splay-tree/splaytree-cpp-3 | ||
* Verification: SPOJ DYNACON1, DYNALCA | ||
*/ | ||
|
||
using namespace splayTree; | ||
|
||
template<int SZ> struct LCT { | ||
ps S[SZ]; | ||
LCT () { F0R(i,SZ) S[i] = new snode(i); } | ||
|
||
// disconnect x from d-th child | ||
void dis(ps x, int d) { | ||
ps y = x->c[d]; | ||
if (x) x->c[d] = NULL, recalc(x); | ||
if (y) y->p = NULL, y->pp = x; | ||
} | ||
|
||
void con(ps x, int d) { setLink(x->pp,x,d); x->pp = NULL; } | ||
|
||
void setPref(ps x) { splay(x->pp), dis(x->pp,1), con(x,1); splay(x); } | ||
|
||
ps access(ps x) { // x is brought to the root of auxiliary tree | ||
dis(splay(x),1); | ||
while (x->pp) setPref(x); | ||
return x; | ||
if (y) { y->p = NULL; if (d) y->pp = x; } | ||
} | ||
// set x to be child of pp | ||
void makeChild(ps x) { setLink(x->pp,x,1); x->pp = NULL; } | ||
// unlink x->pp from its preferred child, then set x to be preferred child | ||
void setPref(ps x) { splay(x->pp), dis(x->pp,1), makeChild(x), splay(x); } | ||
// x is brought to the root of auxiliary tree | ||
ps access(ps x) { dis(splay(x),1); while (x->pp) setPref(x); return x; } | ||
|
||
//////// UPDATES | ||
|
||
ps makeRoot(ps v) { access(v)->flip = 1; return access(v); } | ||
|
||
void link(ps v, ps w) { | ||
access(w)->pp = makeRoot(v); | ||
con(w,0); | ||
} | ||
|
||
void cut(ps x) { // cut link between x and its parent | ||
ps y = access(x)->c[0]; | ||
dis(x,0); y->pp = NULL; | ||
} | ||
// make y the parent of x | ||
void link(ps x, ps y) { makeRoot(x)->pp = y; } | ||
// cut link between x and its parent | ||
void cut(ps x) { dis(access(x),0); } | ||
|
||
//////// QUERIES | ||
|
||
int getDepth(ps v) { access(v); return getsz(v->c[0]); } | ||
|
||
int getRoot(ps v) { return getExtreme(access(v),0)->id; } | ||
|
||
int lca(ps x, ps y) { | ||
ps root = getExtreme(access(y),0); | ||
|
||
dis(splay(x),1); | ||
auto z = getExtreme(x,0); | ||
if (z == root) return x->val; | ||
splay(x); | ||
|
||
while (x->pp) { | ||
auto z = getExtreme(splay(x->pp),0); | ||
if (z == root) return x->pp->val; | ||
setPref(x); | ||
int getDepth(ps x) { access(x); return getsz(x->c[0]); } | ||
ps getRoot(ps x) { return farthest(access(x),0); } | ||
ps lca(ps x, ps y) { | ||
ps root = getRoot(y); | ||
if (farthest(splay(x),0) == root) return x; | ||
while (splay(x)->pp) { | ||
if (farthest(splay(x->pp),0) == root) return x->pp; | ||
setPref(splay(x)); | ||
} | ||
|
||
return -1; | ||
return NULL; | ||
} | ||
}; |
69 changes: 30 additions & 39 deletions
69
Implementations/09 - Range Queries (2)/15.3 - BBST (4)/Splay Tree (5).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters