萌新刚学OI 1ms,简单平衡树板子求条
查看原帖
萌新刚学OI 1ms,简单平衡树板子求条
542389
Fcersoka楼主2024/10/31 12:03

RT

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, INF = 2147483647;
class Treap
{
private:
    class TreapNode
    {
    public:
        int ls, rs, siz, cnt, val, dat;
    }treap[N];
    int tot;
    void update(int p)
    {
        treap[p].siz = treap[treap[p].ls].siz + treap[treap[p].rs].siz + treap[p].cnt;
    }
    int create(int val)
    {
        treap[++tot].val = val;
        treap[tot].dat = rand();
        treap[tot].cnt = treap[tot].siz = 1;
        return tot;
    }
    void zig(int &p)
    {
        int q = treap[p].ls;
        treap[p].ls = treap[q].rs;
        treap[q].rs = p;
        p = q;
        update(treap[p].rs);
        update(p);
    }
    void zag(int &p)
    {
        int q = treap[p].rs;
        treap[p].rs = treap[q].ls;
        treap[q].ls = p;
        p = q;
        update(treap[p].ls);
        update(p);
    }

public:
    int root[N];
    int build()
    {
        int p1 = create(-INF), p2 = create(INF);
        treap[p1].rs = p2;
        update(p1);
        return p1;
    }
    int get_val(int p, int rank)
    {
        if (!p)
        {
            return INF;
        }
        if (treap[treap[p].ls].siz >= rank)
        {
            return get_val(treap[p].ls, rank);
        }
        if (treap[treap[p].ls].siz + treap[p].cnt >= rank)
        {
            return treap[p].val; 
        }
        return get_val(treap[p].rs, rank - treap[treap[p].ls].siz - treap[p].cnt);
    }
    void insert(int &p, int val, int num = 0)
    {
        if (!p)
        {
            p = num ? num : create(val);
            treap[p].ls = treap[p].rs = 0;
            return;
        }
        if (val < treap[p].val)
        {
            insert(treap[p].ls, val, num);
            if (treap[p].dat < treap[treap[p].ls].dat)
            {
                zig(p);
            }
        }
        else 
        {
            insert(treap[p].rs, val, num);
            if (treap[p].dat < treap[treap[p].rs].dat)
            {
                zag(p);
            }
        }
        update(p);
    }
    void merge(int p, int x)
    {
        if (treap[p].ls)
        {
            merge(treap[p].ls, x);
        }
        if (treap[p].rs)
        {
            merge(treap[p].rs, x);
        }
        if (treap[p].val != INF && treap[p].val != -INF)
        {
            insert(x, treap[p].val, p);
        }
    }
};
int n, m, q, fa[N], siz[N], id[N];
int find(int x)
{
    return x == fa[x] ? x : find(fa[x]);
}
void merge_union(int x, int y)
{
    fa[y] = x;
    siz[x] += siz[y];
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    Treap t;
    for (int i = 1; i <= n; i++)
    {
        fa[i] = i;
        siz[i] = 1;
        int p;
        cin >> p;
        id[p] = i;
        t.root[i] = t.build();
        t.insert(t.root[i], p);
    }
    for (int i = 1; i <= m; i++)
    {
        int u, v, fu, fv;
        cin >> u >> v;
        fu = find(u);
        fv = find(v);
        if (fu == fv)
        {
            continue;
        }
        if (siz[fu] < siz[fv])
        {
            swap(fu, fv);
        }
        merge_union(fu, fv);
        t.merge(t.root[fv], t.root[fu]);
    }
    cin >> q;
    while (q--)
    {
        char op;
        int x, y;
        cin >> op >> x >> y;
        if (op == 'Q')
        {
            int val = t.get_val(t.root[find(x)], y + 1);
            if (val == INF)
            {
                cout << -1 << '\n';
            }
            else
            {
                cout << id[val] << '\n';
            }
        }
        else
        {
            int fx, fy;
            fx = find(x);
            fy = find(y);
            if (fx == fy)
            {
                continue;
            }
            // cerr << fx << ''
            if (siz[fx] < siz[fy])
            {
                swap(fx, fy);
            }
            merge_union(fx, fy);
            t.merge(t.root[fy], t.root[fx]);
        }
    }
    return 0;
}
2024/10/31 12:03
加载中...