結果

問題 No.2296 Union Path Query (Hard)
ユーザー t98slidert98slider
提出日時 2023-03-20 20:03:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 7,355 bytes
コンパイル時間 2,671 ms
コンパイル使用メモリ 208,772 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-05-02 14:20:42
合計ジャッジ時間 12,491 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of 'LinkCutTree<S, op, e>::LinkCutTree(const std::vector<_Tp>&) [with S = long long int; S (* op)(S, S) = op; S (* e)() = e]':
main.cpp:88:60:   required from 'LinkCutTree<S, op, e>::LinkCutTree(int) [with S = long long int; S (* op)(S, S) = op; S (* e)() = e]'
main.cpp:227:37:   required from here
main.cpp:89:71: warning: member 'LinkCutTree<long long int, op, e>::N' is used uninitialized [-Wuninitialized]
   89 |     LinkCutTree(const std::vector<S> &vec) : N(vec.size()), Node_info(N){
      |                                                                       ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <class S, S (*op)(S, S), S (*e)()> class LinkCutTree {
    struct Node {
        Node *l, *r, *p;
        S key, sum;
        bool rev;
        int sz;

        explicit Node(const S &v) : key(v), sum(v), sz(1), rev(false),
                                    l(nullptr), r(nullptr), p(nullptr) {}

        bool is_root() const { return not p or (p->l != this and p->r != this); }
    };

    void update(Node* t) {
        t->sz = 1;
        t->sum = t->key;
        if(t->l) t->sz += t->l->sz, t->sum = op(t->l->sum, t->sum);
        if(t->r) t->sz += t->r->sz, t->sum = op(t->sum, t->r->sum);
    }

    void rotate_right(Node* t) {
        Node *x = t->p, *y = x->p;
        if((x->l = t->r)) t->r->p = x;
        t->r = x, x->p = t;
        update(x), update(t);
        if((t->p = y)) {
            if(y->l == x) y->l = t;
            if(y->r == x) y->r = t;
            update(y);
        }
    }

    void rotate_left(Node* t) {
        Node *x = t->p, *y = x->p;
        if((x->r = t->l)) t->l->p = x;
        t->l = x, x->p = t;
        update(x), update(t);
        if((t->p = y)) {
            if(y->l == x) y->l = t;
            if(y->r == x) y->r = t;
            update(y);
        }
    }

    void toggle(Node* t) {
        std::swap(t->l, t->r);
        //t->sum = s(t->sum);
        t->rev ^= true;
    }

    void push(Node* t) {
        if(t->rev) {
            if(t->l) toggle(t->l);
            if(t->r) toggle(t->r);
            t->rev = false;
        }
    }

    void splay(Node *t) {
        push(t);
        while(not t->is_root()) {
            Node* q = t->p;
            if(q->is_root()) {
                push(q), push(t);
                q->l == t ? rotate_right(t) : rotate_left(t);
            } else {
                Node* r = q->p;
                push(r), push(q), push(t);
                if(r->l == q) {
                if(q->l == t) rotate_right(q), rotate_right(t);
                else rotate_left(t), rotate_right(t);
                } else {
                if(q->r == t) rotate_left(q), rotate_left(t);
                else rotate_right(t), rotate_left(t);
                }
            }
        }
    }

    std::vector<Node*> Node_info;

    public:
    int N;
    LinkCutTree(int n) : LinkCutTree(std::vector<S>(n, e())) {}
    LinkCutTree(const std::vector<S> &vec) : N(vec.size()), Node_info(N){
        for(int i = 0; i < N; i++) {
            Node_info[i] = new Node(vec[i]);
        }
    }

    //vと根との間を Heavy-edge で繋げてvを木の根にする
    Node* expose(int v) {
        Node* t = Node_info[v];
        Node* rp = nullptr;
        for(Node* cur = t; cur; cur = cur->p) {
            splay(cur);
            cur->r = rp;
            update(cur);
            rp = cur;
        }
        splay(t);
        return rp;
    }

    //vをもともの木の根にする
    void evert(int v) {
        expose(v);
        toggle(Node_info[v]);
        push(Node_info[v]);
    }

    void link(int _child, int _parent) {
        Node *child = Node_info[_child], *parent = Node_info[_parent];
        if(is_connected(_child, _parent)) {
            throw runtime_error("child and parent must be different connected components");
        }
        evert(_child);
        child->p = parent;
        parent->r = child;
        update(parent);
    }

    void cut(int _child) {
        Node *child = Node_info[_child];
        expose(_child);
        auto *parent = child->l;
        child->l = nullptr;
        parent->p = nullptr;
    }

    Node *lca(int u, int v) {
        expose(u);
        return expose(v);
    }

    bool is_connected(int u, int v) {
        expose(u), expose(v);
        return Node_info[u] == Node_info[v] or Node_info[u]->p;
    }

    const S &query(int u, int v) {
        evert(u);
        expose(v);
        return Node_info[v]->sum;
    }

    void set(int p, S v) {
        expose(p);
        Node_info[p]->key = v;
        update(Node_info[p]);
    }
};

ll op(ll lhs, ll rhs){return lhs + rhs;}
ll e(){return 0;}

struct dsu {
    public:
    int csz;
    dsu() : _n(0) {}
    dsu(int n) : _n(n), csz(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        csz--;
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

    private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};

int main() {
    int N, Q;
    ll X;
    cin >> N >> X >> Q;
    LinkCutTree<ll, op, e> LCT(N + Q);

    dsu uf(N);
    vector<ll> dia(N);
    vector<array<int,2>> sv(N);
    for(int i = 0; i < N; i++) sv[i] = {i, i};

    auto update_diameter = [&](int x, int y){
        ll mx;
        int u, v;
        if(dia[x] >= dia[y]){
            mx = dia[x], u = sv[x][0], v = sv[x][1];
        }else{
            mx = dia[y], u = sv[y][0], v = sv[y][1];
        }
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 2; j++){
                ll d = LCT.query(sv[x][i], sv[y][j]);
                if(d > mx){
                    mx = d, u = sv[x][i], v = sv[y][j];
                }
            }
        }
        int r = uf.merge(x, y);
        dia[r] = mx;
        sv[r] = {u, v};
    };

    int type, v, u, w;
    for(int i = 0; i < Q; i++){
        cin >> type;
        if(type == 1){
            cin >> v >> w;
            LCT.link(v, i + N);
            LCT.link(X, i + N);
            LCT.set(i + N, w);
            update_diameter(uf.leader(v), uf.leader(X));
        }else if(type == 2){
            cin >> v >> u;
            if(!uf.same(v, u)){
                cout << -1 << '\n';
                continue;
            }
            ll d = LCT.query(v, u);
            (X += d) %= N;
            cout << d << '\n';
        }else if(type == 3){
            cin >> v;
            cout << dia[uf.leader(v)] << '\n';
        }else{
            cin >> v;
            X += v;
            if(X >= N) X -= N;
        }
    }
}
0