結果

問題 No.900 aδδitivee
ユーザー kcvlexkcvlex
提出日時 2019-10-05 02:33:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 7,410 bytes
コンパイル時間 2,584 ms
コンパイル使用メモリ 195,816 KB
実行使用メモリ 35,196 KB
最終ジャッジ日時 2024-10-04 08:01:08
合計ジャッジ時間 10,154 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 226 ms
28,272 KB
testcase_08 AC 198 ms
28,156 KB
testcase_09 AC 187 ms
28,300 KB
testcase_10 AC 191 ms
28,348 KB
testcase_11 AC 199 ms
28,308 KB
testcase_12 AC 202 ms
28,288 KB
testcase_13 AC 193 ms
28,300 KB
testcase_14 AC 181 ms
28,320 KB
testcase_15 AC 188 ms
28,300 KB
testcase_16 AC 193 ms
28,320 KB
testcase_17 AC 189 ms
28,308 KB
testcase_18 AC 190 ms
28,308 KB
testcase_19 AC 189 ms
28,272 KB
testcase_20 AC 201 ms
28,176 KB
testcase_21 AC 206 ms
28,312 KB
testcase_22 AC 242 ms
35,128 KB
testcase_23 AC 251 ms
35,152 KB
testcase_24 AC 255 ms
35,084 KB
testcase_25 AC 254 ms
35,196 KB
testcase_26 AC 248 ms
35,132 KB
testcase_27 AC 250 ms
35,156 KB
testcase_28 AC 253 ms
35,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define DEBUGGING
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
using ll = int64_t;
using ull = uint64_t;
using PLL = pair<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> const T& clamp(const T &t, const T &low, const T &high) { return max(low, min(high, t)); }
template <typename T> void chclamp(T &t, const T &low, const T &high) { t = clamp(t, low, high); }

namespace init__ {

struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

}

#ifdef DEBUGGING
// #include "../debug/debug.cpp"
#include "../../debug/debug.cpp"
#else
#define DEBUG(...) 0
#define DEBUG_SEPARATOR_LINE 0
#endif

template <typename T>
T make_v(T init) { return init; }

template <typename T, typename... Tail>
auto make_v(T init, size_t s, Tail... tail) {
#define rec make_v(init, tail...)
    return V<decltype(rec)>(s, rec);
#undef rec
}

template <typename T, typename L>
class LazySegmentTree {
private:
    using Merge = function<T(T, T)>;
    using Apply = function<T(T, L, ssize_t, ssize_t)>;
    using Update = function<L(L, L)>;
    using CalcLazyValue = function<L(ssize_t, ssize_t, L)>;
    using Prop = function<L(L)>;

    size_t arr_size;
    T init_node;
    L init_lazy;
    vector<T> node;
    vector<L> lazy;
    vector<bool> lazy_flag;
    Merge merge_node;
    Apply apply_lazy_value;
    Update update_lazy_value;
    CalcLazyValue calc_lazy_value;
    Prop prop_lazy_value;
    
public:
    LazySegmentTree(vector<T> v, 
                    T init_node,
                    L init_lazy, 
                    Merge merge_node,
                    Apply apply_lazy_value,
                    Update update_lazy_value,
                    CalcLazyValue calc_lazy_value,
                    Prop prop_lazy_value = [](L l) { return l; })
        : init_node(init_node),
          init_lazy(init_lazy),
          merge_node(merge_node),
          apply_lazy_value(apply_lazy_value),
          update_lazy_value(update_lazy_value),
          calc_lazy_value(calc_lazy_value),
          prop_lazy_value(prop_lazy_value)
    {
        {
            arr_size = 1;
            while(arr_size < v.size()) arr_size *= 2;
        }
        node.resize(2 * arr_size - 1, init_node);
        lazy.resize(2 * arr_size - 1, init_lazy);
        lazy_flag.resize(2 * arr_size - 1, false);
        for(ll i = 0; i < v.size(); i++) node[i + arr_size - 1] = v[i];
        for(ll i = arr_size - 2; 0 <= i; i--) node[i] = merge_node(node[i * 2 + 1], node[i * 2 + 2]);
    }

    void lazy_eval(ll pos, ll left, ll right) {
        if(!lazy_flag[pos]) return;

        node[pos] = apply_lazy_value(node[pos], lazy[pos], left, right);
        lazy_flag[pos] = false;

        if(right - left > 1) {
            for(ll idx = 2 * pos + 1; idx <= 2 * pos + 2; idx++) {
                lazy[idx] = update_lazy_value(lazy[idx], prop_lazy_value(lazy[pos]));
                lazy_flag[idx] = true;
            }
        }

        lazy[pos] = init_lazy;
    }

    void update_query(ll left, ll right, L val, ll pos = 0, ll node_left = 0, ll node_right = -1) {
        if(node_right < 0) node_right = arr_size;

        lazy_eval(pos, node_left, node_right);

        if(right <= node_left || node_right <= left) return;
        
        if(left <= node_left && node_right <= right) {
            lazy[pos] = calc_lazy_value(node_left, node_right, val);
            lazy_flag[pos] = true;
            lazy_eval(pos, node_left, node_right);
        } else {
            ll mid = (node_left + node_right) / 2;
            update_query(left, right, val, 2 * pos + 1, node_left, mid);
            update_query(left, right, val, 2 * pos + 2, mid, node_right);
            node[pos] = merge_node(node[2 * pos + 1], node[2 * pos + 2]);
        }
    }

    T get_query(ll left, ll right, ll pos = 0, ll node_left = 0, ll node_right = -1) {
        if(node_right < 0) node_right = arr_size;
        
        lazy_eval(pos, node_left, node_right);

        if(node_right <= left || right <= node_left) return init_node;
        if(left <= node_left && node_right <= right) return node[pos];

        ll mid = (node_left + node_right) / 2;
        return merge_node(get_query(left, right, 2 * pos + 1, node_left, mid),
                          get_query(left, right, 2 * pos + 2, mid, node_right));
    }
};

ll dfs(ll cur, ll pre, const VV<PLL> &edges, V<ll> &l_idx, V<ll> &r_idx, V<ll> &weight, ll w, ll idx, V<ll> &depth, ll d) {
    l_idx[cur] = idx++;
    weight[cur] = w;
    depth[cur] = d;
    for(auto &&edge : edges[cur]) {
        ll nxt, cost;
        tie(nxt, cost) = edge;
        if(nxt == pre) continue;
        idx = dfs(nxt, cur, edges, l_idx, r_idx, weight, w + cost, idx, depth, d + 1);
    }
    r_idx[cur] = idx++;
    return idx;
}

int main() {
    ll N;
    cin >> N;
    V<ll> l_idx(N), r_idx(N), weight(N), depth(N);
    VV<PLL> edges(N);
    for(ll i = 1; i < N; i++) {
        ll u, v, w;
        cin >> u >> v >> w;
        edges[u].emplace_back(v, w);
    }
    
    dfs(0, -1, edges, l_idx, r_idx, weight, 0, 0, depth, 0);
    V<ll> depth_sum(2 * N + 1);
    {
        V<ll> rev_idx(2 * N);
        for(ll i = 0; i < N; i++) rev_idx[l_idx[i]] = rev_idx[r_idx[i]] = i;
        for(ll i = 0; i < 2 * N; i++) depth_sum[i + 1] = depth_sum[i] + depth[rev_idx[i]];
    }

    V<ll> lst_weight(2 * N);
    for(ll i = 0; i < N; i++) lst_weight[l_idx[i]] = lst_weight[r_idx[i]] = weight[i];

    LazySegmentTree<ll, PLL> lst(lst_weight, 0, PLL(0, 0),
                                [&](ll a, ll b) { return a + b; },
                                [&](ll node, PLL val, ll l, ll r) {
                                    ll dsum = depth_sum[r] - depth_sum[l];
                                    return node + dsum * val.first - val.second;
                                },
                                [&](PLL p, PLL q) {
                                    ll a, b, c, d;
                                    tie(a, b) = p;
                                    tie(c, d) = q;
                                    return PLL(a + c, b + d);
                                },
                                [&](ll l, ll r, PLL v) { return v; });

    DEBUG(lst_weight, l_idx, r_idx);

    ll Q;
    cin >> Q;
    while(Q--) {
        ll q;
        cin >> q;
        if(q == 1) {
            ll a, x;
            cin >> a >> x;
            lst.update_query(l_idx[a], r_idx[a], PLL(x, x * depth[a]));
        } else {
            ll a;
            cin >> a;
            cout << lst.get_query(l_idx[a], l_idx[a] + 1) << endl;
        }
    }
    return 0;
}
0