Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ret2home/Library

:warning: structure/PartialPersistentUnionFind.cpp

Depends on

Code

#pragma once
#include "../template/template.cpp"

struct PartialPersistentUnionFind {
    ll version = 0;
    vector<ll> tim, par;
    vector<vector<PL>> siz;
    ll find(ll x, ll t) {
        if (tim[x] > t) return x;
        return find(par[x], t);
    }
    ll same(ll x, ll y, ll t) {
        return find(x, t) == find(y, t);
    }
    ll size(ll x, ll t) {
        x = find(x, t);
        return (--upper_bound(all(siz[x]), PL(t, inf)))->second;
    }
    void merge(ll x, ll y) {
        version++;
        x = find(x, version);
        y = find(y, version);
        if (x == y) return;
        ll sx = size(x, version), sy = size(y, version);
        if (sx > sy) swap(x, y);
        tim[x] = version;
        par[x] = y;
        siz[y].push_back({version, sx + sy});
    }
    PartialPersistentUnionFind(ll x) : tim(x, inf) {
        siz.resize(x);
        rep(i, x) {
            par.push_back(i);
            siz[i].push_back(PL(0, 1));
        }
    }
};
#line 2 "template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define REV(i, n) for (int i = n - 1; i > 0; i--)
#define all(v) v.begin(), v.end()
#define PL pair<ll, ll>
#define PI pair<int, int>
#define pi acos(-1)
#define len(s) (int)s.size()
#define compress(v) \
    sort(all(v));   \
    v.erase(unique(all(v)), v.end());
#define comid(v, x) lower_bound(all(v), x) - v.begin()

template<class T>
using prique=priority_queue<T,vector<T>,greater<>>;

template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
constexpr ll inf = 3e18;
#line 3 "structure/PartialPersistentUnionFind.cpp"

struct PartialPersistentUnionFind {
    ll version = 0;
    vector<ll> tim, par;
    vector<vector<PL>> siz;
    ll find(ll x, ll t) {
        if (tim[x] > t) return x;
        return find(par[x], t);
    }
    ll same(ll x, ll y, ll t) {
        return find(x, t) == find(y, t);
    }
    ll size(ll x, ll t) {
        x = find(x, t);
        return (--upper_bound(all(siz[x]), PL(t, inf)))->second;
    }
    void merge(ll x, ll y) {
        version++;
        x = find(x, version);
        y = find(y, version);
        if (x == y) return;
        ll sx = size(x, version), sy = size(y, version);
        if (sx > sy) swap(x, y);
        tim[x] = version;
        par[x] = y;
        siz[y].push_back({version, sx + sy});
    }
    PartialPersistentUnionFind(ll x) : tim(x, inf) {
        siz.resize(x);
        rep(i, x) {
            par.push_back(i);
            siz[i].push_back(PL(0, 1));
        }
    }
};
Back to top page