compro_library

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

View the Project on GitHub siro53/compro_library

:heavy_check_mark: test/mytest/data-structure/persistent-api.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../template/template.cpp"
#include "../../../data-structure/persistent/persistent-array.hpp"
#include "../../../data-structure/persistent/persistent-unionfind.hpp"
#include "../../../data-structure/persistent/persistent-queue.hpp"


int main() {
    PersistentArray<int> empty;
    assert(empty.root && empty.depth == 0 && empty.identity == 0);
    PersistentArray<int> zeros(3); assert(zeros.get(1) == 0);
    using Array = PersistentArray<int, 2>;
    Array a(vector<int>{1, 2, 3, 4, 5});
    assert(a.full_bit == 3);
    vector<Array::Node*> roots{a.root};
    vector<vector<int>> values{ {1, 2, 3, 4, 5} };
    mt19937 rng(61);
    for(int t = 0; t < 100; ++t) {
        int version = rng() % roots.size(), p = rng() % 5, x = rng() % 100;
        auto next = values[version]; next[p] = x;
        auto root = a.set(roots[version], p, x);
        assert(a.root == root && a.get(p) == x);
        roots.push_back(root); values.push_back(next);
        for(int v = 0; v < int(roots.size()); ++v) for(int i = 0; i < 5; ++i) assert(a.get(roots[v], i) == values[v][i]);
    }
    a.set(0, 20); assert(a.get(0) == 20);
    Array::Node node; assert(node.val == 0);
    node.val = 7; node.childs[2] = &node;
    Array::Node by_pointer(&node), by_reference(node);
    assert(by_pointer.val == 7 && by_reference.val == 7);
    assert(by_pointer.childs[2] == &node && by_reference.childs[2] == &node);
    PersistentUnionFind uf(6);
    auto initial = uf.get_root();
    assert(uf.merge(0, 1)); auto first = uf.get_root();
    assert(uf.merge(2, 3, initial)); auto second = uf.get_root();
    for(auto root : {initial, first, second}) for(int i = 0; i < 6; ++i) {
        int size = (root == first && i < 2) || (root == second && 2 <= i && i < 4) ? 2 : 1;
        auto [leader, count] = uf.leader_and_size(i, root);
        assert(uf.leader(i, root) == leader && uf.size(i, root) == size && count == size);
        for(int j = 0; j < 6; ++j) {
            bool same = i == j || (root == first && i < 2 && j < 2) || (root == second && 2 <= i && i < 4 && 2 <= j && j < 4);
            assert(uf.same(i, j, root) == same);
        }
    }
    assert(!uf.merge(2, 3) && uf.get_root() == second);
    assert(!uf.merge(0, 1, first) && uf.get_root() == second);
    for(int i = 0; i < 6; ++i) {
        assert(uf.leader(i) == uf.leader(i, second));
        assert(uf.size(i) == uf.size(i, second));
        assert(uf.leader_and_size(i) == uf.leader_and_size(i, second));
        assert(uf.same(i, 2) == uf.same(i, 2, second));
    }
    using Queue = PersistentQueue<int, 10>;
    vector<Queue> queues(1); vector<deque<int>> expected(1);
    assert(queues[0].empty() && queues[0].pop().empty());
    Queue::Node qnode(7, 4); assert(qnode.val == 7 && qnode.idx == 4);
    for(int t = 0; t < 300; ++t) {
        int version = rng() % queues.size();
        auto q = queues[version]; auto ref = expected[version];
        if(ref.empty() || rng() % 2) { q = q.push(t); ref.push_back(t); }
        else { q = q.pop(); ref.pop_front(); }
        Queue rebuilt(q.front_node, q.back_node);
        assert(rebuilt.empty() == ref.empty());
        queues.push_back(q); expected.push_back(ref);
        for(int v = 0; v < int(queues.size()); ++v) {
            assert(queues[v].empty() == expected[v].empty());
            if(!expected[v].empty()) assert(queues[v].front() == expected[v].front() && queues[v].back() == expected[v].back());
        }
    }
    INT(a0, b0); print(a0 + b0);
}
#line 1 "test/mytest/data-structure/persistent-api.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "template/template.cpp"
// #region Macros
#include <bits/stdc++.h>
using namespace std;
// input output utils
namespace siro53_io {
    // https://maspypy.github.io/library/other/io_old.hpp
    struct has_val_impl {
        template <class T>
        static auto check(T &&x) -> decltype(x.val(), std::true_type{});

        template <class T> static auto check(...) -> std::false_type;
    };

    template <class T>
    class has_val : public decltype(has_val_impl::check<T>(std::declval<T>())) {
    };

    // debug
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void dump(const T t) {
        cerr << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void dump(const T &t) {
        cerr << t.val();
    }
    void dump(__int128_t n) {
        if(n == 0) {
            cerr << '0';
            return;
        } else if(n < 0) {
            cerr << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cerr << s;
    }
    void dump(const string &s) { cerr << s; }
    void dump(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cerr << s[i];
    }
    template <class T1, class T2> void dump(const pair<T1, T2> &p) {
        cerr << '(';
        dump(p.first);
        cerr << ',';
        dump(p.second);
        cerr << ')';
    }
    template <class T> void dump(const vector<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const set<T> &s) {
        cerr << '{';
        for(auto it = s.begin(); it != s.end(); it++) {
            dump(*it);
            if(next(it) != s.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value> void dump(const map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class Key, class Value>
    void dump(const unordered_map<Key, Value> &mp) {
        cerr << '{';
        for(auto it = mp.begin(); it != mp.end(); it++) {
            dump(*it);
            if(next(it) != mp.end()) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(const deque<T> &v) {
        cerr << '{';
        for(int i = 0; i < (int)v.size(); i++) {
            dump(v[i]);
            if(i < (int)v.size() - 1) cerr << ',';
        }
        cerr << '}';
    }
    template <class T> void dump(queue<T> q) {
        cerr << '{';
        while(!q.empty()) {
            dump(q.front());
            if((int)q.size() > 1) cerr << ',';
            q.pop();
        }
        cerr << '}';
    }

    void debug_print() { cerr << endl; }
    template <class Head, class... Tail>
    void debug_print(const Head &h, const Tail &...t) {
        dump(h);
        if(sizeof...(Tail)) dump(' ');
        debug_print(t...);
    }
    // print
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void print_single(const T t) {
        cout << t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void print_single(const T t) {
        cout << t.val();
    }
    void print_single(__int128_t n) {
        if(n == 0) {
            cout << '0';
            return;
        } else if(n < 0) {
            cout << '-';
            n = -n;
        }
        string s;
        while(n > 0) {
            s += (char)('0' + n % 10);
            n /= 10;
        }
        reverse(s.begin(), s.end());
        cout << s;
    }
    void print_single(const string &s) { cout << s; }
    void print_single(const char *s) {
        int n = (int)strlen(s);
        for(int i = 0; i < n; i++) cout << s[i];
    }
    template <class T1, class T2> void print_single(const pair<T1, T2> &p) {
        print_single(p.first);
        cout << ' ';
        print_single(p.second);
    }
    template <class T> void print_single(const vector<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(const set<T> &s) {
        for(auto it = s.begin(); it != s.end(); it++) {
            print_single(*it);
            if(next(it) != s.end()) cout << ' ';
        }
    }
    template <class T> void print_single(const deque<T> &v) {
        for(int i = 0; i < (int)v.size(); i++) {
            print_single(v[i]);
            if(i < (int)v.size() - 1) cout << ' ';
        }
    }
    template <class T> void print_single(queue<T> q) {
        while(!q.empty()) {
            print_single(q.front());
            if((int)q.size() > 1) cout << ' ';
            q.pop();
        }
    }

    void print() { cout << '\n'; }
    template <class Head, class... Tail>
    void print(const Head &h, const Tail &...t) {
        print_single(h);
        if(sizeof...(Tail)) print_single(' ');
        print(t...);
    }

    // input
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
    void input_single(T &t) {
        cin >> t;
    }
    template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
    void input_single(T &t) {
        cin >> t;
    }
    void input_single(__int128_t &n) {
        string s;
        cin >> s;
        if(s == "0") {
            n = 0;
            return;
        }
        bool is_minus = false;
        if(s[0] == '-') {
            s = s.substr(1);
            is_minus = true;
        }
        n = 0;
        for(int i = 0; i < (int)s.size(); i++) n = n * 10 + (int)(s[i] - '0');
        if(is_minus) n = -n;
    }
    void input_single(string &s) { cin >> s; }
    template <class T1, class T2> void input_single(pair<T1, T2> &p) {
        input_single(p.first);
        input_single(p.second);
    }
    template <class T> void input_single(vector<T> &v) {
        for(auto &e : v) input_single(e);
    }
    void input() {}
    template <class Head, class... Tail> void input(Head &h, Tail &...t) {
        input_single(h);
        input(t...);
    }
}; // namespace siro53_io
#ifdef DEBUG
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debug_print(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
// io setup
struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;
using namespace siro53_io;
// types
using ll = long long;
using i128 = __int128_t;
// input macros
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    input(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    input(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    input(__VA_ARGS__)
#define DBL(...)                                                               \
    double __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define LD(...)                                                                \
    long double __VA_ARGS__;                                                   \
    input(__VA_ARGS__)
#define UINT(...)                                                              \
    unsigned int __VA_ARGS__;                                                  \
    input(__VA_ARGS__)
#define ULL(...)                                                               \
    unsigned long long __VA_ARGS__;                                            \
    input(__VA_ARGS__)
#define VEC(name, type, len)                                                   \
    vector<type> name(len);                                                    \
    input(name);
#define VEC2(name, type, len1, len2)                                           \
    vector name(len1, vector<type>(len2));                                     \
    input(name);
// other macros
// https://trap.jp/post/1224/
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(RALL(v))
#define UNIQUE(v)                                                              \
    sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()), v.shrink_to_fit()
#define REV(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
#define MIN(v) (*min_element(ALL(v)))
#define MAX(v) (*max_element(ALL(v)))
// util const
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// util functions
void Case(int i) { cout << "Case #" << i << ": "; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
template <class T> inline bool chmax(T &a, T b) {
    return (a < b ? a = b, true : false);
}
template <class T> inline bool chmin(T &a, T b) {
    return (a > b ? a = b, true : false);
}
template <class T, int dim>
auto make_vector_impl(vector<int>& sizes, const T &e) {
    if constexpr(dim == 1) {
        return vector(sizes[0], e);
    } else {
        int n = sizes[dim - 1];
        sizes.pop_back();
        return vector(n, make_vector_impl<T, dim - 1>(sizes, e));
    }
}
template <class T, int dim>
auto make_vector(const int (&sizes)[dim], const T &e = T()) {
    vector<int> s(dim);
    for(int i = 0; i < dim; i++) s[i] = sizes[dim - i - 1];
    return make_vector_impl<T, dim>(s, e);
}
vector<int> iota_gen(int n, int start = 0) {
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), start);
    return ord;
}
template<typename T>
vector<int> ord_sort(const vector<T>& v, bool greater = false) {
    auto ord = iota_gen((int)v.size());
    sort(ALL(ord), [&](int i, int j) {
        if(greater) return v[i] > v[j];
        return v[i] < v[j];
    });
    return ord;
}
// #endregion Macros
#line 2 "data-structure/persistent/persistent-array.hpp"

#line 6 "data-structure/persistent/persistent-array.hpp"

template <class T, int LOG = 4> struct PersistentArray {
    struct Node {
        T val;
        Node *childs[1 << LOG];

        Node(): val{} { memset(childs, 0, sizeof(childs)); }
        Node(const Node *node): val(node->val) { memcpy(childs, node->childs, sizeof(childs)); }
        Node(const Node &node): val(node.val) { memcpy(childs, node.childs, sizeof(childs)); }
    };

    Node *root;
    int depth;
    T identity;
    const int full_bit = (1 << LOG) - 1;

    PersistentArray(): PersistentArray(0) {}
    explicit PersistentArray(int N, const T &id = 0)
        : PersistentArray(std::vector<T>(N, id)) {
        identity = id;
    }
    PersistentArray(const std::vector<T> &v) : root(new Node()), depth(0), identity{} {
        int N = (int)v.size();
        while(N > 0) {
            N >>= LOG;
            depth++;
        }
        N = (int)v.size();
        for(int i = 0; i < N; i++) {
            Node *now = root;
            for(int mask = i, d = depth; d > 0; d--) {
                if(!now->childs[mask & full_bit]) {
                    now->childs[mask & full_bit] = new Node();
                }
                now = now->childs[mask & full_bit];
                mask >>= LOG;
            }
            now->val = v[i];
        }
    }

    T get(int idx) { return get(root, idx); }

    Node *set(int idx, const T &new_value) { return set(root, idx, new_value); }

    T get(Node *node, int idx) {
        for(int i = 0; i < depth; i++) {
            node = node ? node->childs[idx & full_bit] : nullptr;
            idx >>= LOG;
        }
        return (node ? node->val : identity);
    }

    Node *set(Node *node, int idx, T new_value) {
        std::vector<std::pair<Node *, int>> st;
        for(int i = 0; i < depth; i++) {
            st.emplace_back(node, idx & full_bit);
            node = node ? node->childs[idx & full_bit] : nullptr;
            idx >>= LOG;
        }
        Node *new_child = new Node();
        new_child->val = new_value;
        while(!st.empty()) {
            auto [par, k] = st.back();
            st.pop_back();
            Node *new_par = par ? new Node(par) : new Node();
            new_par->childs[k] = new_child;
            new_child = new_par;
        }
        return (root = new_child);
    }
};
#line 2 "data-structure/persistent/persistent-unionfind.hpp"

#line 5 "data-structure/persistent/persistent-unionfind.hpp"

#line 7 "data-structure/persistent/persistent-unionfind.hpp"

struct PersistentUnionFind {
    PersistentArray<int> par;
    using Node = decltype(par)::Node;
    Node* root;

    explicit PersistentUnionFind(int n): par(std::vector<int>(n, -1)) {
        root = par.root;
    }
    // (leader, size)
    std::pair<int, int> leader_and_size(int u, Node* r) {
        int p = par.get(r, u);
        return (p < 0 ? std::make_pair(u, -p) : leader_and_size(p, r));
    }
    std::pair<int, int> leader_and_size(int u) {
        return leader_and_size(u, root);
    }
    inline int leader(int u, Node* r) { return leader_and_size(u, r).first; }
    inline int leader(int u) { return leader_and_size(u, root).first; }
    inline int size(int u, Node* r) { return leader_and_size(u, r).second; }
    inline int size(int u) { return leader_and_size(u, root).second; }
    inline bool same(int u, int v, Node* r) { return (leader(u, r) == leader(v, r)); }
    inline bool same(int u, int v) { return same(u, v, root); }
    bool merge(int u, int v, Node* r) {
        int sz_u, sz_v;
        std::tie(u, sz_u) = leader_and_size(u, r);
        std::tie(v, sz_v) = leader_and_size(v, r);
        if(u == v) return false;
        if(sz_u < sz_v) {
            std::swap(u, v);
            std::swap(sz_u, sz_v);
        }
        r = par.set(r, u, -(sz_u + sz_v));
        r = par.set(r, v, u);
        root = r;
        return true;
    }
    bool merge(int u, int v) { return merge(u, v, root); }
    Node* get_root() const { return root; }
};
#line 2 "data-structure/persistent/persistent-queue.hpp"

template <class T, int LOG = 20> struct PersistentQueue {
    struct Node {
        T val;
        int idx;
        Node *pre[LOG]; // pre[k] := このノードの2^k個前のノード

        Node(T val, int idx) : val(val), idx(idx) {}
    };
    Node *front_node;
    Node *back_node;

    PersistentQueue() : front_node(nullptr), back_node(nullptr) {}
    explicit PersistentQueue(Node *front_node, Node *back_node)
        : front_node(front_node), back_node(back_node) {}
    PersistentQueue push(const T &x) {
        Node *new_node = new Node(x, back_node ? back_node->idx + 1 : 0);
        new_node->pre[0] = back_node;
        for(int i = 1; i < LOG; i++) {
            Node *tmp = new_node->pre[i - 1];
            if(!tmp) break;
            new_node->pre[i] = tmp->pre[i - 1];
        }
        return PersistentQueue(front_node ? front_node : new_node, new_node);
    }
    PersistentQueue pop() {
        if(!front_node or !back_node or front_node->idx == back_node->idx) {
            return PersistentQueue();
        }
        int sz = back_node->idx - front_node->idx - 1;
        Node *node = back_node;
        for(int i = 0; i < LOG; i++)
            if(sz >> i & 1) node = node->pre[i];
        return PersistentQueue(node, back_node);
    }
    bool empty() const { return (front_node == nullptr and back_node == nullptr); }
    T front() const { return front_node->val; }
    T back() const { return back_node->val; }
};
#line 6 "test/mytest/data-structure/persistent-api.test.cpp"


int main() {
    PersistentArray<int> empty;
    assert(empty.root && empty.depth == 0 && empty.identity == 0);
    PersistentArray<int> zeros(3); assert(zeros.get(1) == 0);
    using Array = PersistentArray<int, 2>;
    Array a(vector<int>{1, 2, 3, 4, 5});
    assert(a.full_bit == 3);
    vector<Array::Node*> roots{a.root};
    vector<vector<int>> values{ {1, 2, 3, 4, 5} };
    mt19937 rng(61);
    for(int t = 0; t < 100; ++t) {
        int version = rng() % roots.size(), p = rng() % 5, x = rng() % 100;
        auto next = values[version]; next[p] = x;
        auto root = a.set(roots[version], p, x);
        assert(a.root == root && a.get(p) == x);
        roots.push_back(root); values.push_back(next);
        for(int v = 0; v < int(roots.size()); ++v) for(int i = 0; i < 5; ++i) assert(a.get(roots[v], i) == values[v][i]);
    }
    a.set(0, 20); assert(a.get(0) == 20);
    Array::Node node; assert(node.val == 0);
    node.val = 7; node.childs[2] = &node;
    Array::Node by_pointer(&node), by_reference(node);
    assert(by_pointer.val == 7 && by_reference.val == 7);
    assert(by_pointer.childs[2] == &node && by_reference.childs[2] == &node);
    PersistentUnionFind uf(6);
    auto initial = uf.get_root();
    assert(uf.merge(0, 1)); auto first = uf.get_root();
    assert(uf.merge(2, 3, initial)); auto second = uf.get_root();
    for(auto root : {initial, first, second}) for(int i = 0; i < 6; ++i) {
        int size = (root == first && i < 2) || (root == second && 2 <= i && i < 4) ? 2 : 1;
        auto [leader, count] = uf.leader_and_size(i, root);
        assert(uf.leader(i, root) == leader && uf.size(i, root) == size && count == size);
        for(int j = 0; j < 6; ++j) {
            bool same = i == j || (root == first && i < 2 && j < 2) || (root == second && 2 <= i && i < 4 && 2 <= j && j < 4);
            assert(uf.same(i, j, root) == same);
        }
    }
    assert(!uf.merge(2, 3) && uf.get_root() == second);
    assert(!uf.merge(0, 1, first) && uf.get_root() == second);
    for(int i = 0; i < 6; ++i) {
        assert(uf.leader(i) == uf.leader(i, second));
        assert(uf.size(i) == uf.size(i, second));
        assert(uf.leader_and_size(i) == uf.leader_and_size(i, second));
        assert(uf.same(i, 2) == uf.same(i, 2, second));
    }
    using Queue = PersistentQueue<int, 10>;
    vector<Queue> queues(1); vector<deque<int>> expected(1);
    assert(queues[0].empty() && queues[0].pop().empty());
    Queue::Node qnode(7, 4); assert(qnode.val == 7 && qnode.idx == 4);
    for(int t = 0; t < 300; ++t) {
        int version = rng() % queues.size();
        auto q = queues[version]; auto ref = expected[version];
        if(ref.empty() || rng() % 2) { q = q.push(t); ref.push_back(t); }
        else { q = q.pop(); ref.pop_front(); }
        Queue rebuilt(q.front_node, q.back_node);
        assert(rebuilt.empty() == ref.empty());
        queues.push_back(q); expected.push_back(ref);
        for(int v = 0; v < int(queues.size()); ++v) {
            assert(queues[v].empty() == expected[v].empty());
            if(!expected[v].empty()) assert(queues[v].front() == expected[v].front() && queues[v].back() == expected[v].back());
        }
    }
    INT(a0, b0); print(a0 + b0);
}
Back to top page