splay伸展树模板

 

普通版本:

 struct SplayTree
{ const static int maxn = 1e5 + ; int tot,root,ch[maxn][], key[maxn], val[maxn], sz[maxn], lz[maxn], fa[maxn]; void init( int x, int ky, int v = , int par = )
{
ch[x][]=ch[x][]=, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = ;
} void init()
{
init( , , );
sz[] = ;
tot = root = ;
} inline void push_up(int x)
{
sz[x] = sz[ch[x][]] + sz[ch[x][]] + ;
} inline void push_down(int x)
{ } void rotate( int x, int d )
{
int y = fa[x];
ch[y][d ^ ] = ch[x][d];
if ( ch[x][d]) fa[ch[x][d]] = y;
fa[x] = fa[y];
if (fa[y])
{
if (y == ch[fa[y]][d]) ch[fa[y]][d] = x;
else ch[fa[y]][d ^ ] = x;
}
ch[x][d] = y, fa[y] = x;
push_up( y ), push_up( x );
} // Splay x to target's son
void Splay( int x, int target )
{
while( fa[x] != target )
{
int y = fa[x];
if( x == ch[y][] )
{
if( fa[y] != target && y == ch[fa[y]][])
rotate( y, );
rotate( x, );
}
else
{
if( fa[y] != target && y == ch[fa[y]][])
rotate( y, );
rotate( x, );
}
}
if( !target ) root = x;
} void Insert( int & t, int ky, int v, int par = )
{
if( t == )
{
t = ++ tot;
init( t, ky, v, par );
Splay( tot, );
}
else
{
int cur = t;
if( v < key[t] ) Insert( ch[t][], ky, v, cur );
else Insert( ch[t][], ky, v, cur );
push_up( cur );
}
} // Return point
int find( int t, int v )
{
if( t == ) return ;
else if( key[t] == v )
{
Splay( t, );
return t;
}
else if( v < key[t] ) return find( ch[t][], v );
return find( ch[t][], v );
} // Delete Root
void Delete()
{
if( !ch[root][] )
{
fa[ ch[root][] ] = ;
root = ch[root][];
}
else
{
int cur = ch[root][];
while( ch[cur][] ) cur = ch[cur][];
Splay( cur, root );
ch[cur][] = ch[root][];
root = cur, fa[cur] = , fa[ch[root][]] = root;
push_up( root );
}
} int size()
{
return sz[root];
}
// 查第 k 小 , 必须保证合法
int kth( int x, int k )
{
if( k == sz[ch[x][]] + )
{
Splay( x, );
return key[x];
}
else if( k <= sz[ch[x][]] ) return kth( ch[x][], k );
else return kth( ch[x][], k - sz[ch[x][]] - );
} //找前驱
int pred( int t, int v )
{
if( t == ) return v;
else
{
if( v <= key[t] ) return pred( ch[t][], v );
else
{
int ans = pred( ch[t][], v );
if( ans == v )
{
ans = key[t];
Splay( t, );
}
return ans;
}
}
} /*int less( int t , int v ){
if( t == 0 ) return 0;
int rs = 0;
if( v <= key[t] ) rs = less( ch[t][0] , v );
else rs = sz[ch[t][0]] + 1 + less( ch[t][1] , v );
if( Tl ){
Splay( t , 0 );
Tl = 0;
}
return rs;
}*/ //找后继
int succ( int t, int v )
{
if( t == ) return v;
else
{
if( v >= key[t] ) return succ( ch[t][], v );
else
{
int ans = succ( ch[t][], v );
if( ans == v )
{
ans = key[t];
Splay( t, );
}
return ans;
}
}
} void Preorder( int t )
{
if( !t ) return;
Preorder( ch[t][] );
printf("%d ", key[t] );
Preorder( ch[t][] );
} } sp;

区间更新版本-

 #include <bits/stdc++.h>

 using namespace std;

 #define lc ch[x][0]
#define rc ch[x][1]
#define pr fa[x] struct SplayTree
{ const static int maxn = 1e5 + ; int tot,root,ch[maxn][], key[maxn], val[maxn], sz[maxn], rev[maxn], fa[maxn]; inline int wh(int x){ return ch[pr][] == x;} inline void init( int x, int ky, int v = , int par = )
{
lc=rc=, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = , rev[x] = ;
} inline void init()
{
init( , , );
sz[] = ;
tot = root = ;
} inline void push_up(int x)
{
sz[x] = sz[lc] + sz[rc] + ;
} inline void reverse(int x)
{
rev[x] ^= , swap( lc, rc);
} inline void push_down(int x)
{
if(rev[x])
{
if(lc) reverse(lc);
if(rc) reverse(rc);
rev[x] = ;
}
} void rotate( int x)
{
int f = fa[x], gf = fa[f], t1 = wh(x);
if( gf ) ch[gf][wh(f)] = x;
fa[x] = gf, ch[f][t1] = ch[x][^t1], fa[ch[f][t1]] = f;
ch[x][t1^] = f, fa[f] = x;
push_up( f ), push_up( x );
} void splay( int x, int tar )
{
for(; pr != tar; rotate(x))
if(fa[pr] != tar)
rotate( wh(x) == wh(pr) ? pr: x);
if( !tar ) root = x;
} void insert( int ky, int v)
{
int x = root, ls = root;
while(x)
{
push_down(x);
sz[x] ++, ls = x;
x = ch[x][ky > key[x]];
}
init( ++tot, ky, v, ls);
ch[ls][ky > key[ls]] = tot;
splay( tot, );
} int find( int ky)
{
int x = root;
while(x)
{
push_down(x);
if(key[x] == ky) break;
x = ch[x][ky > key[x]];
}
if(x) splay(x,);
else x = -;
return x;
} // Delete Root
void Delete()
{
if( !ch[root][] )
{
fa[ ch[root][] ] = ;
root = ch[root][];
}
else
{
int cur = ch[root][];
while( ch[cur][] ) cur = ch[cur][];
splay( cur, root );
ch[cur][] = ch[root][];
root = cur, fa[cur] = , fa[ch[root][]] = root;
push_up( root );
}
} int kth( int k)
{
int x = root;
if(sz[x] < k) return -;
while(x)
{
push_down(x);
if(k == sz[lc] + ) break;
if(k > sz[lc])
k -= sz[lc] + , x = rc;
else
x = lc;
}
if(x) splay(x,);
else x = -;
return x;
} int pred( void)
{
int x = root;
if(!x || !lc) return -;
x = lc;
while(rc) push_down(x), x = rc;
splay( x, );
return x;
} int succ( void)
{
int x = root;
if(!x || !rc) return -;
x = rc;
while(lc) push_down(x), x = lc;
splay( x, );
return x;
} void debug( int x )
{
if( !x ) return;
if(lc) debug( lc );
printf("%d ", key[x] );
if(rc) debug( rc );
} } sp; int main(void)
{
sp.init();
for(int i=,x;i<=;i++)
scanf("%d",&x),sp.insert(x,);
sp.debug(sp.root);
return ;
}

启发式合并:

 /**************************************************************
Problem: 2733
User: weeping
Language: C++
Result: Accepted
Time:4908 ms
Memory:4436 kb
****************************************************************/ #include <bits/stdc++.h> using namespace std; #define lc ch[x][0]
#define rc ch[x][1] int n,m,q,f[]; int fd(int x)
{
return f[x]==x?x:f[x]=fd(f[x]);
} struct SplayTree
{ const static int maxn = 1e5 + ; int tot,root,ch[maxn][], key[maxn], val[maxn], sz[maxn], rev[maxn], fa[maxn]; inline void init( int x, int ky, int v = , int par = )
{
lc=rc=, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = , rev[x] = ;
} inline void init()
{
init( , , );
sz[] = ;
tot = root = ;
} inline void push_up(int x)
{
sz[x] = sz[lc] + sz[rc] + ;
} inline void reverse(int x)
{
rev[x] ^= , swap( lc, rc);
} inline void push_down(int x)
{
if(rev[x])
{
if(lc) reverse(lc);
if(rc) reverse(rc);
rev[x] = ;
}
} void rotate( int x)
{
int f = fa[x], gf = fa[f];
int t1 = (ch[f][] == x), t2 = (ch[gf][] == f);
if( gf ) ch[gf][t2] = x;
fa[x] = gf, ch[f][t1] = ch[x][^t1], fa[ch[f][t1]] = f;
ch[x][t1^] = f, fa[f] = x;
push_up( f ), push_up( x );
} void splay( int x, int tar )
{
for(int f = fa[x], gf = fa[f]; f != tar; rotate(x), f = fa[x], gf = fa[f])
if(gf != tar)
rotate( ((ch[f][] == x) == (ch[gf][] == f) )? f: x);
if( !tar ) root = x;
} void insert( int ky, int v)
{
int x = root, ls = root;
while(x)
{
push_down(x);
sz[x] ++, ls = x;
x = ch[x][ky > key[x]];
}
init( ++tot, ky, v, ls);
ch[ls][ky > key[ls]] = tot;
splay( tot, );
} int find( int ky)
{
int x = root;
while(x)
{
push_down(x);
if(key[x] == ky) break;
x = ch[x][ky > key[x]];
}
if(x) splay(x,);
else x = -;
return x;
} // Delete Root
void Delete()
{
if( !ch[root][] )
{
fa[ ch[root][] ] = ;
root = ch[root][];
}
else
{
int cur = ch[root][];
while( ch[cur][] ) cur = ch[cur][];
splay( cur, root );
ch[cur][] = ch[root][];
root = cur, fa[cur] = , fa[ch[root][]] = root;
push_up( root );
}
} int kth( int k)
{
int x = root;
if(sz[x] < k) return -;
while(x)
{
push_down(x);
if(k == sz[lc] + ) break;
if(k > sz[lc])
k -= sz[lc] + , x = rc;
else
x = lc;
}
if(x) splay(x,);
else x = -;
return x;
} int pred( void)
{
int x = root;
if(!x || !lc) return -;
x = lc;
while(rc) push_down(x), x = rc;
splay( x, );
return x;
} int succ( void)
{
int x = root;
if(!x || !rc) return -;
x = rc;
while(lc) push_down(x), x = lc;
splay( x, );
return x;
} void debug( int x )
{
if( !x ) return;
if(lc) debug( lc );
printf("%d ", key[x] );
if(rc) debug( rc );
} void qinsert(int y)
{
int x = root, ls = root, ky = key[y];
while(x)
ls = x, x = ch[x][ky > key[x]];
x = ls;
ch[x][ky > key[x]] = y,fa[y] = x, sz[y] = ;
splay(y, );
} void qmerge(int x)
{
if(!x) return;
int tl = lc, tr = rc;
lc = rc = ;
qmerge(tl);
qinsert(x);
qmerge(tr);
}
void merge(int u,int v)
{
if(u == v) return ;
if(sz[u]>sz[v]) swap(u,v);
f[u] = v, splay( v, );
qmerge(u);
}
} sp; int main(void)
{
scanf("%d%d",&n,&m);
for(int i=,x;i<=n;i++)
scanf("%d",&x),f[i]=i,sp.key[i]=x,sp.sz[i]=;
for(int i=,u,v;i<=m;i++)
scanf("%d%d",&u,&v),sp.merge(fd(u),fd(v));
scanf("%d",&q);
char op[];
for(int i=,x,y;i<=q;i++)
{
scanf("%s%d%d",op,&x,&y);
if(op[]=='B')
sp.merge(fd(x),fd(y));
else
sp.splay(x,),printf("%d\n",sp.kth(y));
}
return ;
}
上一篇:讨论下python中全局变量的使用


下一篇:面试常问MySQL性能优化问题