結果

問題 No.1030 だんしんぐぱーりない
ユーザー hitonanodehitonanode
提出日時 2020-04-20 02:14:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 2,000 ms
コード長 8,999 bytes
コンパイル時間 2,863 ms
コンパイル使用メモリ 228,368 KB
実行使用メモリ 25,888 KB
最終ジャッジ日時 2024-10-06 08:24:20
合計ジャッジ時間 10,767 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 160 ms
22,716 KB
testcase_06 AC 127 ms
17,540 KB
testcase_07 AC 81 ms
9,216 KB
testcase_08 AC 85 ms
11,256 KB
testcase_09 AC 135 ms
22,832 KB
testcase_10 AC 51 ms
6,144 KB
testcase_11 AC 136 ms
13,144 KB
testcase_12 AC 140 ms
20,932 KB
testcase_13 AC 127 ms
18,760 KB
testcase_14 AC 154 ms
18,280 KB
testcase_15 AC 63 ms
5,248 KB
testcase_16 AC 135 ms
15,860 KB
testcase_17 AC 152 ms
23,800 KB
testcase_18 AC 188 ms
18,960 KB
testcase_19 AC 90 ms
8,448 KB
testcase_20 AC 111 ms
13,496 KB
testcase_21 AC 110 ms
17,220 KB
testcase_22 AC 113 ms
14,020 KB
testcase_23 AC 136 ms
12,644 KB
testcase_24 AC 90 ms
7,040 KB
testcase_25 AC 120 ms
12,376 KB
testcase_26 AC 66 ms
5,248 KB
testcase_27 AC 76 ms
5,248 KB
testcase_28 AC 139 ms
16,252 KB
testcase_29 AC 106 ms
20,608 KB
testcase_30 AC 101 ms
14,508 KB
testcase_31 AC 105 ms
12,976 KB
testcase_32 AC 129 ms
17,712 KB
testcase_33 AC 143 ms
21,464 KB
testcase_34 AC 52 ms
6,816 KB
testcase_35 AC 231 ms
25,888 KB
testcase_36 AC 216 ms
25,884 KB
testcase_37 AC 227 ms
25,756 KB
testcase_38 AC 231 ms
25,764 KB
testcase_39 AC 226 ms
25,888 KB
testcase_40 AC 2 ms
6,820 KB
testcase_41 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'UndirectedWeightedTree& UndirectedWeightedTree::operator=(UndirectedWeightedTree&&)',
    inlined from 'int main()' at main.cpp:242:36:
main.cpp:17:8: warning: '<anonymous>.UndirectedWeightedTree::root' may be used uninitialized [-Wmaybe-uninitialized]
   17 | struct UndirectedWeightedTree
      |        ^~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:242:36: note: '<anonymous>' declared here
  242 |     tree = UndirectedWeightedTree(N);
      |                                    ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;



// lowest common ancestor (LCA) class for undirected weighted tree
// 無向重み付きグラフの最小共通祖先
// <https://yukicoder.me/submissions/392383>
struct UndirectedWeightedTree
{
    using T = long long int;   // Arbitrary data structure (operator+, operator- must be defined)
    int INVALID = -1;
    int V, lgV;
    int E;
    int root;
    vector<vector<pair<int, int>>> adj; // (nxt_vertex, edge_id)
    // vector<pint> edge;  // edges[edge_id] = (vertex_id, vertex_id)
    vector<T> weight;  // w[edge_id]
    vector<int> par;  // parent_vertex_id[vertex_id]
    vector<int> depth;  // depth_from_root[vertex_id]
    vector<T> acc_weight;  // w_sum_from_root[vertex_id]

    void _fix_root_dfs(int now, int prv, int prv_edge_id)
    {
        par[now] = prv;
        if (prv_edge_id != INVALID) acc_weight[now] = acc_weight[prv] + weight[prv_edge_id];
        for (auto nxt : adj[now]) if (nxt.first != prv)
        {
            depth[nxt.first] = depth[now] + 1;
            _fix_root_dfs(nxt.first, now, nxt.second);
        }
    }

    UndirectedWeightedTree() = default;
    UndirectedWeightedTree(int N) : V(N), E(0), adj(N)
    {
        lgV = 1;
        while (1 << lgV < V) lgV++;
    }

    void add_edge(int u, int v, T w)
    {
        adj[u].emplace_back(v, E);
        adj[v].emplace_back(u, E);
        // edge.emplace_back(u, v);
        weight.emplace_back(w);
        E++;
    }

    void fix_root(int r)
    {
        root = r;
        par.resize(V);
        depth.resize(V);
        depth[r] = 0;
        acc_weight.resize(V);
        acc_weight[r] = 0;
        _fix_root_dfs(root, INVALID, INVALID);
    }

    vector<vector<int>> doubling;
    void doubling_precalc()
    {
        doubling.assign(lgV, vector<int>(V));
        doubling[0] = par;
        for (int d = 0; d < lgV - 1; d++) for (int i = 0; i < V; i++)
        {
            if (doubling[d][i] == INVALID) doubling[d + 1][i] = INVALID;
            else doubling[d + 1][i] = doubling[d][doubling[d][i]];
        }
    }

    int kth_parent(int x, int k)
    {
        if (depth[x] < k) return INVALID;
        for (int d = 0; d < lgV; d++)
        {
            if (x == INVALID) return INVALID;
            if (k & (1 << d)) x = doubling[d][x];
        }
        return x;
    }

    int lowest_common_ancestor(int u, int v)
    {
        if (depth[u] > depth[v]) swap(u, v);

        v = kth_parent(v, depth[v] - depth[u]);
        if (u == v) return u;
        for (int d = lgV - 1; d >= 0; d--)
        {
            if (doubling[d][u] != doubling[d][v]) u = doubling[d][u], v = doubling[d][v];
        }
        return par[u];
    }

    T path_length(int u, int v)
    {
        // Not distance, but the sum of weights
        int r = lowest_common_ancestor(u, v);
        return (acc_weight[u] - acc_weight[r]) + (acc_weight[v] - acc_weight[r]);
    }
};

// Nonrecursive Segment Tree (point-update, range-get)
// - Conditions for operations:
//   - datamerge: [TDATA, TDATA] -> TDATA, e(x, y) == e(y, x)
//   - data2ret: [TDATA, TQUERY] -> TRET
//   - retmerge: [TRET, TRET] -> TRET, g(defaultRET, x) == x, g(x, y) = g(y, x)
//   - commutability f(e(x, y), q) == g(f(x, q), f(y, q))
template <typename TDATA, typename TRET, typename TQUERY>
struct NonrecursiveSegmentTree
{
    int N;
    TRET defaultRET;
    virtual TDATA datamerge(const TDATA &, const TDATA &) = 0;
    virtual TRET data2ret(const TDATA &, const TQUERY &) = 0;
    virtual TRET retmerge(const TRET &, const TRET &) = 0;
    std::vector<TDATA> data;
    inline TDATA& at(int i) { return data[i]; }

    inline void _merge(int i) { at(i) = datamerge(at(i << 1), at((i << 1) + 1)); }
    void initialize(const std::vector<TDATA> &seq, TRET RET_ZERO) {
        N = seq.size();
        defaultRET = RET_ZERO;
        data = seq;
        data.insert(data.end(), seq.begin(), seq.end());
        for (int i = N - 1; i; i--) _merge(i);
    }
    NonrecursiveSegmentTree() = default;
    void update(int pos, const TDATA &x) {
        assert(pos >= 0 and pos < N);
        at(pos + N) = x;
        for (int i = pos + N; i > 1;) i >>= 1, _merge(i);
    }

    // [l, r), 0-indexed
    TRET get(int l, int r, TQUERY query = NULL) {
        assert(l >= 0 and r <= N);
        TRET retl = defaultRET, retr = defaultRET;
        l += N, r += N;
        while (l < r) {
            if (l & 1) retl = retmerge(retl, data2ret(data[l++], query));
            if (r & 1) retr = retmerge(data2ret(data[--r], query), retr);
            l >>= 1, r >>= 1;
        }
        return retmerge(retl, retr);
    }

    // Calculate smallest r that satisfies g(f(x_l, q), ..., f(x_{r - 1}, q)) >= threshold
    // Assumption: Monotonicity of g(x_l, ..., x_r) about r (l: fixed)
    // Complexity: O(log N)
    int binary_search(int l, const TRET &threshold, TQUERY query = NULL) {
        std::stack<int> rs;
        l += N;
        int r = N * 2;
        TRET retl = defaultRET;
        if (threshold <= retl) return l - N;
        while (l < r) {
            if (l & 1) {
                TRET ret_tmp = retmerge(retl, data2ret(data[l], query));
                if (threshold <= ret_tmp) {
                    while (l * 2 < N * 2) {
                        ret_tmp = retmerge(retl, data2ret(data[l * 2], query));
                        if (threshold <= ret_tmp) l *= 2;
                        else retl = ret_tmp, l = l * 2 + 1;
                    }
                    return l - N;
                }
                l++;
                retl = ret_tmp;
            }
            if (r & 1) rs.push(--r);
            l >>= 1, r >>= 1;
        }
        while (!rs.empty()) {
            l = rs.top();
            rs.pop();
            TRET ret_tmp = retmerge(retl, data2ret(data[l], query));
            if (threshold <= ret_tmp) {
                while (l * 2 < N * 2) {
                    ret_tmp = retmerge(retl, data2ret(data[l * 2], query));
                    if (threshold <= ret_tmp) l *= 2;
                    else retl = ret_tmp, l = l * 2 + 1;
                }
                return l - N;
            }
            retl = ret_tmp;
        }
        return N;
    }

    template<typename T1, typename T2, typename T3>
    friend std::ostream &operator<<(std::ostream &os, NonrecursiveSegmentTree<T1, T2, T3> s) {
        os << "[SegmentTree (len: " << s.N << ')';
        for (int i = 0; i < s.N; i++) os << s.at(i + s.N) << ',';
        os << "]";
        return os;
    }
};

UndirectedWeightedTree tree;

// Range Minimum Query
// - get: return min(x_l, ..., x_{r - 1})
struct RangeLCAQuery : public NonrecursiveSegmentTree<int, int, bool>
{
    using T = int;
    using SegTree = NonrecursiveSegmentTree<T, T, bool>;
    T datamerge(const T &vl, const T &vr) override {
        if (vl == -1) return vr;
        if (vr == -1) return vl;
        return tree.lowest_common_ancestor(vl, vr);
    };
    T data2ret(const T &v, const bool &q) override { return v; }
    T retmerge(const T &vl, const T &vr) override { return datamerge(vl, vr); };
    RangeLCAQuery(const std::vector<T> &seq) : SegTree::NonrecursiveSegmentTree() {
        SegTree::initialize(seq, -1);
    };
};

vector<vector<int>> to;

vector<int> C;
void dfs(int now, int par, int am)
{
    chmax(C[now], am);
    for (auto nxt : to[now]) if (nxt != par) dfs(nxt, now, C[now]);
}
int main()
{
    int N, K, Q;
    cin >> N >> K >> Q;
    tree = UndirectedWeightedTree(N);
    C.resize(N);
    vector<int> A(K);
    cin >> C >> A;
    for (auto &a : A) a--;

    to.resize(N);
    REP(_, N - 1)
    {
        int e, f;
        cin >> e >> f;
        e--, f--;
        tree.add_edge(e, f, 1);
        to[e].push_back(f);
        to[f].push_back(e);
    }
    tree.fix_root(0);
    tree.doubling_precalc();
    RangeLCAQuery lca(A);

    dfs(0, -1, 0);

    while (Q--)
    {
        int t;
        cin >> t;
        if (t == 1)
        {
            int X, Y;
            cin >> X >> Y;
            X--, Y--;
            A[X] = Y;
            lca.update(X, A[X]);
        }
        else
        {
            int L, R;
            cin >> L >> R;
            cout << C[lca.get(L - 1, R)] << '\n';
        }
    }
}
0