模板_SPLAY

Posted kczno1fans

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模板_SPLAY相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
#define maxn 500005
//#define int long long
using namespace std;
inline int read()
{
    char x = getchar();
    int lin = 0, f = 1;
    while(x < 0 || x > 9)
    {
        if(x == -) f = -1;
        x = getchar();
    }
    while(x >= 0 && x <= 9)
    {
        lin = (lin << 1) + (lin << 3) + x - 0;
        x = getchar();
    }
    return lin * f;
}
#define PII pair<int,int>
#define fir first
#define sec second
#define ma(a,b) make_pair(a,b)
#define db double
#define inf 123123123
int son[maxn][2],fa[maxn],v[maxn],rev[maxn],sz[maxn];
int rt,n,m,a[maxn],ty,le,re,tot;
#define l son[x][0]
#define r son[x][1]
void revl(int x) { swap(l,r); rev[x] ^= 1; }
bool get(int x) { return son[fa[x]][1] == x; }
void pb(int x) { if(rev[x]) { revl(l); revl(r); rev[x] = 0; } }
void link(int now,int F,int t) { fa[now] = F; son[F][t] = now; }
void update(int x) { sz[x] = 1; if(l) sz[x] += sz[l]; if(r) sz[x] += sz[r]; }
void rotate(int x,int &k)
{
    int y = fa[x],z = fa[y],t = get(x);
    if(k == y) k = x;
    else son[z][get(y)] = x;
    link(son[x][t ^ 1],y,t); link(y,x,t ^ 1);
    fa[x] = z;
    update(y);
    update(x);
}
void splay(int x,int &k)
{
    while(x != k)
    {
        int y = fa[x];
        if(y != k)
            if(get(x) == get(y)) rotate(x,k);
            else rotate(y,k);
        rotate(x,k);
    }
}
int find(int x,int k)
{
    pb(x);
    if(sz[l] + 1 == k) return x;
    if(sz[l] + 1 < k) return find(r,k - sz[l] - 1);
    return find(l,k);
}
int split(int x,int y)
{
    x = find(rt,x);
    y = find(rt,y + 2);
    splay(x,rt); splay(y,r);
    return son[y][0];
}
int build(int le,int re,int F)
{
    ++tot;
    int x = tot;
    fa[x] = F;
    int mid = le + re >> 1;
    v[x] = a[mid];
    if(le < mid) l = build(le,mid - 1,x);
    if(re > mid) r = build(mid + 1,re,x);
    update(x);
    return x;
}
void reverse(int x,int y) { revl(split(x,y)); }
void print(int x)
{
    pb(x);
    if(l) print(l);
    if(v[x] != inf) printf("%d ",v[x]);
    if(r) print(r);
}
signed main(){
    n = read(); m = read();
    for(int i = 1; i <= n; i++) a[i + 1] = i;
    a[1] = a[n + 2] = inf;
    rt = build(1,n + 2,0);
    for(int i = 1; i <= m; i++)
    {
        le = read(); re = read();
        reverse(le,re);
    }
    print(rt);
}

 

以上是关于模板_SPLAY的主要内容,如果未能解决你的问题,请参考以下文章

文艺平衡树(splay模板)

模板Splay

bzoj 1588 splay模板题

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

[AndroidStudio]_[初级]_[配置自动完成的代码片段]

[AndroidStudio]_[初级]_[配置自动完成的代码片段]