結果

問題 No.1030 だんしんぐぱーりない
ユーザー 👑 hitonanodehitonanode
提出日時 2020-04-20 02:14:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 288 ms / 2,000 ms
コード長 8,999 bytes
コンパイル時間 3,180 ms
コンパイル使用メモリ 220,800 KB
実行使用メモリ 26,016 KB
最終ジャッジ日時 2024-04-16 00:29:08
合計ジャッジ時間 12,431 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 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 197 ms
22,840 KB
testcase_06 AC 148 ms
17,536 KB
testcase_07 AC 92 ms
9,216 KB
testcase_08 AC 99 ms
11,120 KB
testcase_09 AC 167 ms
22,828 KB
testcase_10 AC 60 ms
6,144 KB
testcase_11 AC 156 ms
13,140 KB
testcase_12 AC 175 ms
20,928 KB
testcase_13 AC 147 ms
18,760 KB
testcase_14 AC 188 ms
18,152 KB
testcase_15 AC 70 ms
5,376 KB
testcase_16 AC 163 ms
15,856 KB
testcase_17 AC 188 ms
23,664 KB
testcase_18 AC 213 ms
19,080 KB
testcase_19 AC 103 ms
8,448 KB
testcase_20 AC 133 ms
13,496 KB
testcase_21 AC 123 ms
17,208 KB
testcase_22 AC 132 ms
14,144 KB
testcase_23 AC 154 ms
12,768 KB
testcase_24 AC 104 ms
7,040 KB
testcase_25 AC 137 ms
12,288 KB
testcase_26 AC 77 ms
5,376 KB
testcase_27 AC 87 ms
5,376 KB
testcase_28 AC 169 ms
16,376 KB
testcase_29 AC 136 ms
20,476 KB
testcase_30 AC 116 ms
14,760 KB
testcase_31 AC 121 ms
13,228 KB
testcase_32 AC 162 ms
17,704 KB
testcase_33 AC 181 ms
21,464 KB
testcase_34 AC 61 ms
6,784 KB
testcase_35 AC 288 ms
26,016 KB
testcase_36 AC 267 ms
25,912 KB
testcase_37 AC 276 ms
26,012 KB
testcase_38 AC 276 ms
25,880 KB
testcase_39 AC 275 ms
25,888 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 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