• 模板_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);
    }
  • 相关阅读:
    codis安装手册
    引用对象的使用和易产生bug的示例
    shallow copy 和 deep copy 的示例
    [转载] 公知其实就是正常人嘛
    Spring GET请求实体中日期的转换
    [转载] 方方: 借陸遊三個字:錯,錯,錯(3月16日)
    [转载] 英国防疫怪招
    [记录] Disruptor 介绍
    [记录] 重要网址备忘
    [转载] 面对新冠病毒在全球的大流行,我们如何自保?
  • 原文地址:https://www.cnblogs.com/kczno1fans/p/9922274.html
Copyright © 2020-2023  润新知