結果

問題 No.1030 だんしんぐぱーりない
ユーザー knshnbknshnb
提出日時 2020-04-17 22:33:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 5,326 bytes
コンパイル時間 3,046 ms
コンパイル使用メモリ 219,324 KB
実行使用メモリ 20,804 KB
最終ジャッジ日時 2024-04-14 14:51:26
合計ジャッジ時間 10,098 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 169 ms
17,976 KB
testcase_06 AC 134 ms
14,424 KB
testcase_07 AC 86 ms
8,320 KB
testcase_08 AC 93 ms
9,600 KB
testcase_09 AC 139 ms
18,132 KB
testcase_10 AC 56 ms
6,944 KB
testcase_11 AC 148 ms
11,240 KB
testcase_12 AC 147 ms
15,724 KB
testcase_13 AC 125 ms
14,764 KB
testcase_14 AC 172 ms
14,640 KB
testcase_15 AC 71 ms
6,944 KB
testcase_16 AC 144 ms
12,496 KB
testcase_17 AC 143 ms
18,368 KB
testcase_18 AC 191 ms
15,424 KB
testcase_19 AC 95 ms
7,168 KB
testcase_20 AC 117 ms
10,600 KB
testcase_21 AC 108 ms
13,912 KB
testcase_22 AC 114 ms
10,992 KB
testcase_23 AC 146 ms
10,496 KB
testcase_24 AC 101 ms
6,940 KB
testcase_25 AC 130 ms
10,368 KB
testcase_26 AC 73 ms
6,940 KB
testcase_27 AC 85 ms
6,944 KB
testcase_28 AC 158 ms
13,232 KB
testcase_29 AC 114 ms
15,768 KB
testcase_30 AC 104 ms
11,520 KB
testcase_31 AC 111 ms
10,368 KB
testcase_32 AC 146 ms
14,672 KB
testcase_33 AC 151 ms
16,844 KB
testcase_34 AC 59 ms
6,944 KB
testcase_35 AC 251 ms
20,676 KB
testcase_36 AC 239 ms
20,668 KB
testcase_37 AC 249 ms
20,804 KB
testcase_38 AC 254 ms
20,676 KB
testcase_39 AC 243 ms
20,668 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Fri Apr 17 22:21:54 JST 2020
 **/

template <class T> inline bool chmin(T& a, const T& b) {
    if (a <= b) return false;
    a = b;
    return true;
}
template <class T> inline bool chmax(T& a, const T& b) {
    if (a >= b) return false;
    a = b;
    return true;
}

// edgeを貼ったあとにbuild()を忘れない!
struct TreeDoubling {
    struct Edge {
        int to, len;
    };  // 場合に応じて書き換える、toは必須
    std::vector<std::vector<Edge>> g;
    int n, size;  // MSB + 1
    int root;
    std::vector<int> depth;
    std::vector<std::vector<int>> parent;  // ダミー頂点n(親もn)
    TreeDoubling(int n_) : g(n_), n(n_), size(64 - __builtin_clzll(n_) + 1), depth(n_) {
        parent.resize(size, std::vector<int>(n + 1, n));
    }
    void build(int root_ = 0) {
        auto dfs = [&](auto f, int v, int prv) -> void {
            for (Edge& e : g[v]) {
                if (e.to == prv) continue;
                depth[e.to] = depth[v] + e.len;
                parent[0][e.to] = v;
                f(f, e.to, v);
            }
        };
        root = root_;
        depth[root] = 0;
        dfs(dfs, root, -1);
        for (int k = 0; k < size - 1; k++) {
            for (int i = 0; i < n; i++) {
                parent[k + 1][i] = parent[k][parent[k][i]];
            }
        }
    }

    // vからd個分親にさかのぼった頂点、rootよりも上はnを返す
    int query(int v, int d) {
        int ret = v;
        for (int j = 0; j < size; j++) {
            if (d >> j & 1) ret = parent[j][ret];
        }
        return ret;
    }
    int lca(int u, int v) {
        if (depth[u] > depth[v]) std::swap(u, v);
        v = query(v, depth[v] - depth[u]);
        if (u == v) return u;

        for (int j = size - 1; j >= 0; j--) {
            if (parent[j][u] == parent[j][v]) continue;
            u = parent[j][u];
            v = parent[j][v];
        }
        assert(parent[0][u] == parent[0][v]);
        return parent[0][u];
    }
    int dist(int u, int v) { return depth[u] + depth[v] - depth[lca(u, v)] * 2; }
};

/// @docs src/DataStructure/SegmentTree.md
template <class T, class F> struct SegmentTree {
    const F op;
    const T e;
    SegmentTree(F op_, T e_) : op(op_), e(e_) {}
    int n;
    std::vector<T> t;
    void set_by_identity(int n_) {
        n = n_;
        t.clear(), t.resize(2 * n, e);
    }
    void set_by_vector(const std::vector<T>& a) {
        n = a.size();
        t.clear(), t.resize(2 * n, e);
        for (int i = 0; i < n; i++) t[i + n] = a[i];
        build();
    }
    void build() {
        for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]);
    }
    T& operator[](int i) { return t[i + n]; }
    // [l, r)
    T query(int l, int r) const {
        assert(0 <= l && l <= r && r <= n);
        T resl = e, resr = e;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) resl = op(resl, t[l++]);
            if (r & 1) resr = op(t[--r], resr);
        }
        return op(resl, resr);
    }
    // i番目をxに変更
    void update(int i, const T& x) {
        assert(0 <= i && i < n);
        t[i += n] = x;
        while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
    }
    // i番目にxを作用 (a[i] = op(a[i], x))
    void operate(int i, const T& x) {
        assert(0 <= i && i < n);
        i += n;
        t[i] = op(t[i], x);
        while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
    }
    friend std::string to_string(const SegmentTree<T, F>& seg) {
        return to_string(std::vector<T>(seg.t.begin() + seg.n, seg.t.end()));
    }
};
template <class T, class F> auto make_segment_tree(F op, T e) { return SegmentTree<T, F>(op, e); }

signed main() {
    int n, K, Q;
    std::cin >> n >> K >> Q;
    std::vector<Int> c(n), a(K);
    REP(i, n) std::cin >> c[i];
    REP(i, K) std::cin >> a[i], a[i]--;
    TreeDoubling g(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        g.g[u].push_back({v, 1});
        g.g[v].push_back({u, 1});
    }
    g.build();

    std::vector<Int> mas(n, -1);
    auto dfs = [&](auto f, int v, int prv) -> void {
        chmax(mas[v], c[v]);
        for (auto& p : g.g[v]) {
            Int s = p.to;
            if (s == prv) continue;

            chmax(mas[s], mas[v]);
            f(f, s, v);
        }
    };
    dfs(dfs, 0, -1);

    auto seg = make_segment_tree(
        [&g](Int i, Int j) -> Int {
            if (i == -1) return j;
            if (j == -1) return i;
            return g.lca(i, j);
        },
        -1LL);
    seg.set_by_vector(a);
    REP(q, Q) {
        Int t, x, y;
        std::cin >> t >> x >> y;
        if (t == 1) {
            seg.update(x - 1, y - 1);
        } else {
            std::cout << mas[seg.query(x - 1, y)] << "\n";
        }
    }
}
0