結果

問題 No.1030 だんしんぐぱーりない
ユーザー finefine
提出日時 2020-04-17 23:59:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 5,395 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 178,748 KB
実行使用メモリ 16,280 KB
最終ジャッジ日時 2024-04-14 16:16:49
合計ジャッジ時間 8,077 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 121 ms
14,292 KB
testcase_06 AC 97 ms
11,692 KB
testcase_07 AC 59 ms
7,424 KB
testcase_08 AC 65 ms
9,888 KB
testcase_09 AC 112 ms
15,352 KB
testcase_10 AC 39 ms
6,944 KB
testcase_11 AC 96 ms
10,360 KB
testcase_12 AC 111 ms
12,668 KB
testcase_13 AC 94 ms
12,780 KB
testcase_14 AC 113 ms
11,876 KB
testcase_15 AC 47 ms
6,940 KB
testcase_16 AC 98 ms
9,468 KB
testcase_17 AC 112 ms
14,284 KB
testcase_18 AC 124 ms
14,236 KB
testcase_19 AC 63 ms
6,940 KB
testcase_20 AC 79 ms
8,740 KB
testcase_21 AC 87 ms
10,608 KB
testcase_22 AC 81 ms
8,900 KB
testcase_23 AC 91 ms
9,268 KB
testcase_24 AC 66 ms
7,040 KB
testcase_25 AC 83 ms
10,180 KB
testcase_26 AC 48 ms
6,944 KB
testcase_27 AC 55 ms
6,940 KB
testcase_28 AC 106 ms
10,392 KB
testcase_29 AC 96 ms
12,740 KB
testcase_30 AC 79 ms
8,752 KB
testcase_31 AC 78 ms
8,848 KB
testcase_32 AC 101 ms
11,740 KB
testcase_33 AC 112 ms
13,704 KB
testcase_34 AC 42 ms
6,940 KB
testcase_35 AC 158 ms
16,268 KB
testcase_36 AC 156 ms
16,236 KB
testcase_37 AC 159 ms
16,280 KB
testcase_38 AC 156 ms
16,168 KB
testcase_39 AC 156 ms
16,272 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

template <class Monoid>
struct SegmentTree {
    using T = typename Monoid::T;

    int n;
    vector<T> data;

    SegmentTree() {}

    SegmentTree(int size, T initial_value = Monoid::unit()) {
        n = 1;
        while (n < size) n <<= 1;
        data.assign(2 * n - 1, initial_value);

        if (initial_value != Monoid::unit()) {
            for (int i = n - 2; i >= 0; i--) data[i] = Monoid::merge(data[i * 2 + 1], data[i * 2 + 2]);
        }
    }

    SegmentTree(const vector<T>& v) {
        int size = v.size();
        n = 1;
        while (n < size) n <<= 1;
        data.assign(2 * n - 1, Monoid::unit());

        for (int i = 0; i < size; i++) data[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) data[i] = Monoid::merge(data[i * 2 + 1], data[i * 2 + 2]);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void update(int k, T x) {
        k += n - 1; //葉の節点
        Monoid::update(data[k], x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = Monoid::merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b, int k, int l, int r) {
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return Monoid::unit();
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return Monoid::merge(vl, vr);
        }
    }

    //外から呼ぶ用
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }

    //非再帰版: バグってるかもしれないので定数倍高速化する時以外使わないで
    //区間[a, b)に対するクエリに答える
    T query_fast(int a, int b) {
        T vl = Monoid::unit(), vr = Monoid::unit();
        for (int l = a + n, r = b + n; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = Monoid::merge(vl, data[l++ - 1]);
            if (r & 1) vr = Monoid::merge(data[--r - 1], vr);
        }
        return Monoid::merge(vl, vr);
    }
};

// 以下、Monoidの例
template <class U = ll>
struct RangeMin {
    using T = U;
    static T merge(T x, T y) { return min(x, y); }
    static void update(T& target, T x) { target = x; }
    static constexpr T unit() { return numeric_limits<T>::max(); }
};

template<>
struct RangeMin<pair<int, int> > {
    using T = pair<int, int>;
    static T merge(T x, T y) { return min(x, y); }
    static void update(T& target, T x) { target = x; }
    static constexpr T unit() { return make_pair(1145141919, 1145141919); }
};

template <class U = ll>
struct RangeMax {
    using T = U;
    static T merge(T x, T y) { return max(x, y); }
    static void update(T& target, T x) { target = x; }
    static constexpr T unit() { return numeric_limits<T>::min(); }
};

struct LCA {
    using Graph = vector< vector<int> >;
    using Seg = SegmentTree< RangeMin<pair<int, int> > >;

    const Graph& g;
    const int root;

    vector<int> depth;
    vector<int> vs;
    vector<int> fs;

    Seg st_lca;

    LCA(const Graph& g, int root = 0) : g(g), root(root) {
        int V = g.size();
        depth.resize(V);
        fs.resize(V);

        dfs(root, -1, 0);
        
        st_lca = Seg(vs.size());
        for (int i = 0; i < vs.size(); ++i) {
            st_lca.update(i, make_pair(depth[vs[i]], i));
        }
    }

    void dfs(int v, int p, int d) {
        fs[v] = vs.size();
        depth[v] = d;
        vs.push_back(v);
        for (int i : g[v]) {
            if (i != p) {
                dfs(i, v, d + 1);
                vs.push_back(v);
            }
        }
    }

    int operator()(int u, int v) {
        return vs[st_lca.query_fast(u, v).second];
    }
};

void dfs2(int cur, int par, const LCA::Graph& g, vector<int>& maxc) {
    for (int nex : g[cur]) {
        if (nex == par) continue;
        maxc[nex] = max(maxc[nex], maxc[cur]);
        dfs2(nex, cur, g, maxc);
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, K, Q;
    cin >> n >> K >> Q;
    vector<int> c(n);
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
    }

    vector<int> a(K);
    for (int i = 0; i < K; ++i) {
        cin >> a[i];
        --a[i];
    }

    LCA::Graph g(n);
    for (int i = 1; i < n; ++i) {
        int e, f;
        cin >> e >> f;
        --e; --f;
        g[f].push_back(e);
    }

    vector<int> maxc = c;
    dfs2(0, -1, g, maxc);

    LCA lca(g);

    SegmentTree< RangeMin<int> > st_min(K);
    SegmentTree< RangeMax<int> > st_max(K);
    for (int i = 0; i < K; ++i) {
        st_min.update(i, lca.fs[a[i]]);
        st_max.update(i, lca.fs[a[i]]);
    }

    for (int i = 0; i < Q; ++i) {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1) {
            --x; --y;
            a[x] = y;
            st_min.update(x, lca.fs[a[x]]);
            st_max.update(x, lca.fs[a[x]]);
        } else {
            --x;
            int tmp = lca(st_min.query_fast(x, y), st_max.query_fast(x, y) + 1);
            cout << maxc[tmp] << "\n";
        }
    }
    return 0;
}
0