結果

問題 No.1030 だんしんぐぱーりない
ユーザー MisterMister
提出日時 2020-05-02 01:16:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 371 ms / 2,000 ms
コード長 5,822 bytes
コンパイル時間 1,469 ms
コンパイル使用メモリ 105,832 KB
実行使用メモリ 28,800 KB
最終ジャッジ日時 2024-06-07 14:57:36
合計ジャッジ時間 12,468 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 253 ms
25,344 KB
testcase_06 AC 198 ms
19,584 KB
testcase_07 AC 129 ms
9,856 KB
testcase_08 AC 130 ms
12,544 KB
testcase_09 AC 200 ms
25,600 KB
testcase_10 AC 88 ms
6,400 KB
testcase_11 AC 227 ms
14,464 KB
testcase_12 AC 236 ms
23,296 KB
testcase_13 AC 186 ms
20,736 KB
testcase_14 AC 257 ms
20,096 KB
testcase_15 AC 112 ms
5,376 KB
testcase_16 AC 243 ms
17,792 KB
testcase_17 AC 213 ms
26,624 KB
testcase_18 AC 286 ms
21,120 KB
testcase_19 AC 157 ms
9,344 KB
testcase_20 AC 191 ms
15,232 KB
testcase_21 AC 161 ms
19,328 KB
testcase_22 AC 180 ms
15,744 KB
testcase_23 AC 225 ms
13,952 KB
testcase_24 AC 158 ms
7,808 KB
testcase_25 AC 200 ms
13,824 KB
testcase_26 AC 122 ms
5,376 KB
testcase_27 AC 138 ms
5,376 KB
testcase_28 AC 240 ms
18,304 KB
testcase_29 AC 169 ms
22,912 KB
testcase_30 AC 168 ms
16,384 KB
testcase_31 AC 180 ms
14,464 KB
testcase_32 AC 202 ms
19,712 KB
testcase_33 AC 217 ms
23,680 KB
testcase_34 AC 91 ms
7,168 KB
testcase_35 AC 371 ms
28,800 KB
testcase_36 AC 343 ms
28,800 KB
testcase_37 AC 349 ms
28,752 KB
testcase_38 AC 356 ms
28,800 KB
testcase_39 AC 351 ms
28,672 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }

    int size() const { return graph.size(); }
};

template <class Cost = int>
struct LevelAncestor {
    Graph<Cost> tree;
    std::vector<std::vector<int>> par;
    std::vector<int> depth;
    std::vector<Cost> cdepth;
    int kmax;

    void dfs(int v, int p = -1, int d = 0, Cost c = 0) {
        par[v][0] = p;
        depth[v] = d;
        cdepth[v] = c;

        for (const auto& e : tree[v]) {
            if (e.dst == p) continue;
            dfs(e.dst, v, d + 1, c + e.cost);
        }
    }

    LevelAncestor(const Graph<Cost>& tree, int root)
        : tree(tree), par(tree.size()), depth(tree.size(), -1), cdepth(tree.size()) {
        kmax = 0;
        while ((1 << kmax) < (int)tree.size()) ++kmax;
        for (auto& v : par) v.resize(kmax + 1);

        dfs(root);

        for (int k = 1; k <= kmax; ++k) {
            for (int v = 0; v < tree.size(); ++v) {
                int p = par[v][k - 1];
                par[v][k] = (p == -1 ? -1 : par[p][k - 1]);
            }
        }
    }

    int climb(int v, int d) const {
        for (int k = kmax; k >= 0 && v != -1; --k) {
            if ((1 << k) > d) continue;

            v = par[v][k];
            d -= (1 << k);
        }
        return v;
    }

    int lca(int u, int v) const {
        if (depth[u] < depth[v]) std::swap(u, v);

        if (depth[u] > depth[v]) {
            u = climb(u, depth[u] - depth[v]);
        }

        if (u == v) return u;

        for (int k = kmax; k >= 0; --k) {
            if (par[u][k] != par[v][k]) {
                u = par[u][k];
                v = par[v][k];
            }
        }
        return par[u][0];
    }

    int dist(int u, int v) const {
        int p = lca(u, v);
        return depth[u] + depth[v] - depth[p] * 2;
    }

    Cost cdist(int u, int v) const {
        int p = lca(u, v);
        return cdepth[u] + cdepth[v] - cdepth[p] * 2;
    }
};

template <class T>
struct SegmentTree {
    using Merger = std::function<T(T, T)>;

    int length;
    std::vector<T> dat;
    T unit;
    Merger merge;

    explicit SegmentTree(int n, const T& unit, const Merger& merge)
        : length(1), unit(unit), merge(merge) {
        while (length < n) length <<= 1;
        dat.assign(length * 2, unit);
    }

    template <class Container>
    explicit SegmentTree(const Container& elems, const T& unit, const Merger& merge)
        : length(1), unit(unit), merge(merge) {
        int n = elems.size();
        while (length < n) length <<= 1;

        dat.assign(length * 2, unit);

        std::copy(elems.begin(), elems.end(), dat.begin() + length);

        for (int nidx = length - 1; nidx >= 1; --nidx) {
            T vl = dat[nidx * 2 + 0];
            T vr = dat[nidx * 2 + 1];
            dat[nidx] = merge(vl, vr);
        }
    }

    void update(int nidx, const T& elem) {
        nidx += length;
        dat[nidx] = elem;

        while (nidx > 0) {
            nidx >>= 1;
            T vl = dat[nidx * 2 + 0];
            T vr = dat[nidx * 2 + 1];
            dat[nidx] = merge(vl, vr);
        }
    }

    T fold(int ql, int qr) {
        ql = std::max(ql, 0);
        qr = std::min(qr, length);
        ql += length, qr += length;

        T lacc = unit, racc = unit;
        while (ql < qr) {
            if (ql & 1) {
                lacc = merge(lacc, dat[ql]);
                ++ql;
            }
            if (qr & 1) {
                --qr;
                racc = merge(dat[qr], racc);
            }
            ql >>= 1, qr >>= 1;
        }

        return merge(lacc, racc);
    }

    T get(int idx) { return dat[idx + length]; }
    T whole() { return dat[1]; }
};

void solve() {
    int n, k, q;
    std::cin >> n >> k >> q;

    std::vector<int> cs(n);
    for (auto& c : cs) std::cin >> c;

    std::vector<int> vs(k);
    for (auto& v : vs) {
        std::cin >> v;
        --v;
    }

    Graph<> graph(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        std::cin >> u >> v;
        graph.span(false, --u, --v);
    }

    {
        std::function<void(int, int)>
            dfs = [&](int v, int p) -> void {
            for (auto e : graph[v]) {
                int u = e.dst;
                if (u == p) continue;
                cs[u] = std::max(cs[u], cs[v]);
                dfs(u, v);
            }
        };
        dfs(0, -1);
    }

    LevelAncestor<> la(graph, 0);

    SegmentTree<int>
        seg(vs, -1,
            [&](int u, int v) {
                if (u > v) std::swap(u, v);
                return (u == -1 ? v : la.lca(u, v));
            });

    while (q--) {
        int t;
        std::cin >> t;

        if (t == 1) {
            int i, v;
            std::cin >> i >> v;
            seg.update(--i, --v);

        } else {
            int l, r;
            std::cin >> l >> r;
            std::cout << cs[seg.fold(--l, r)] << std::endl;
        }
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0