結果

問題 No.900 aδδitivee
コンテスト
ユーザー detteiuu
提出日時 2026-01-27 02:09:02
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 5,954 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,742 ms
コンパイル使用メモリ 396,776 KB
実行使用メモリ 35,472 KB
最終ジャッジ日時 2026-01-27 02:09:17
合計ジャッジ時間 11,598 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

#define pass (void)0
#define INF (1<<30)-1
#define INFLL (1LL<<60)-1
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define YesNo(cond) cout << ((cond) ? "Yes\n" : "No\n")
#define YESNO(cond) cout << ((cond) ? "YES\n" : "NO\n")

using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;

template <typename T> void print(const T& value) { cout << value << "\n"; }
template <typename T> void print(const vector<T>& vec) { for (auto& v : vec) cout << v << " "; cout << "\n"; }
template <typename T> void input(vector<T>& vec) { for (auto& v : vec) cin >> v; };
template <typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }

#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using vm = vector<mint>;

struct LCA {
    int V;
    int LOG;
    vector<vector<int>> parent;
    vector<int> depth;
    vector<ll> d;

    LCA(const vector<vector<pair<int, ll>>>& G, int root = 0) {
        V = (int)G.size();
        LOG = 1;
        while ((1 << LOG) < V) LOG++;

        parent.assign(LOG, vector<int>(V, -1));
        depth.assign(V, -1);
        d.assign(V, -1);

        bfs(G, root);

        for (int i = 0; i + 1 < LOG; i++) {
            for (int v = 0; v < V; v++) {
                if (parent[i][v] != -1) {
                    parent[i + 1][v] = parent[i][ parent[i][v] ];
                }
            }
        }
    }

    void bfs(const vector<vector<pair<int, ll>>>& G, int root) {
        queue<int> q;
        depth[root] = 0;
        d[root] = 0;
        q.push(root);

        while (!q.empty()) {
            int u = q.front(); q.pop();
            for (auto [v, w] : G[u]) {
                if (depth[v] == -1) {
                    d[v] = d[u]+w;
                    depth[v] = depth[u] + 1;
                    parent[0][v] = u;
                    q.push(v);
                }
            }
        }
    }

    int lca(int a, int b) const {
        if (depth[a] < depth[b]) swap(a, b);

        int diff = depth[a] - depth[b];
        for (int i = 0; i < LOG; i++) {
            if (diff & (1 << i)) {
                a = parent[i][a];
            }
        }

        if (a == b) return a;

        for (int i = LOG - 1; i >= 0; i--) {
            if (parent[i][a] != parent[i][b]) {
                a = parent[i][a];
                b = parent[i][b];
            }
        }
        return parent[0][a];
    }

    ll dist(int a, int b) const {
        int c = lca(a, b);
        return d[a] + d[b] - 2 * d[c];
    }

    bool is_ancestor(int u, int v) const {
        return lca(u, v) == u;
    }

    int kth_ancestor(int v, int k) const {
        for (int i = 0; i < LOG; i++) {
            if (k & (1 << i)) {
                v = parent[i][v];
                if (v == -1) break;
            }
        }
        return v;
    }

    int jump(int u, int v) const {
        if (u == v) return u;
        int c = lca(u, v);
        if (c == u) {
            return kth_ancestor(v, dist(u, v) - 1);
        } else {
            return parent[0][u];
        }
    }

    int jump_k(int u, int v, int k) const {
        int d = dist(u, v);
        if (d <= k) return v;

        int c = lca(u, v);
        int du = depth[u] - depth[c];

        if (k <= du) {
            return kth_ancestor(u, k);
        } else {
            return kth_ancestor(v, d - k);
        }
    }

    bool on_path(int u, int v, int x) const {
        return dist(u, x) + dist(x, v) == dist(u, v);
    }

    pair<int,int> create_path(int u, int v, int x) const {
        if (on_path(u, v, x)) return {u, v};
        if (on_path(u, x, v)) return {u, x};
        if (on_path(v, x, u)) return {v, x};
        return {-1, -1};
    }
};

vi IN;
vi OUT;
vector<vector<pair<int, ll>>> G;
vi sign;
int dfs(int n, int time) {
    IN[n] = time;
    if (n != 0) sign.push_back(1);
    for (auto [v, _] : G[n]) {
        time = dfs(v, time+1);
    }
    OUT[n] = time;
    if (n != 0) sign.push_back(-1);
    return time+1;
}

struct Node {
    ll value;
    int sign;
};

Node op(Node left, Node right) {
    return {left.value+right.value, left.sign+right.sign};
}

Node e() {
    return {0, 0};
}

Node mapping(ll top, Node bottom) {
    bottom.value += top*bottom.sign;
    return bottom;
}

ll composition(ll top, ll bottom) {
    return bottom+top;
}

ll id() {
    return 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);

    int N;
    cin >> N;
    G.assign(N, vector<pair<int, ll>> ());
    rep (_, N-1) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        G[u].push_back({v, w});
    }

    LCA lca(G);
    IN.assign(N, -1);
    OUT.assign(N, -1);
    dfs(0, 0);

    vector<Node> def;
    rep (i, sz(sign)) {
        def.push_back({0, sign[i]});
    }
    lazy_segtree<Node, op, e, ll, mapping, composition, id> seg(def);

    int Q;
    cin >> Q;
    while (Q--) {
        int q;
        cin >> q;
        if (q == 1) {
            int a; ll x;
            cin >> a >> x;
            if (IN[a] == OUT[a]) continue;
            seg.apply(IN[a], OUT[a], x);
        } else {
            int b;
            cin >> b;
            if (b != 0) {
                print(lca.dist(0, b)+seg.prod(0, IN[b]).value);
            } else {
                print(0);
            }
        }
    }
}
0