結果

問題 No.900 aδδitivee
ユーザー finefine
提出日時 2019-10-04 23:09:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 5,175 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 177,996 KB
実行使用メモリ 27,264 KB
最終ジャッジ日時 2024-04-14 11:35:54
合計ジャッジ時間 8,534 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 218 ms
20,864 KB
testcase_08 AC 221 ms
20,864 KB
testcase_09 AC 214 ms
20,864 KB
testcase_10 AC 215 ms
20,864 KB
testcase_11 AC 230 ms
20,864 KB
testcase_12 AC 209 ms
20,864 KB
testcase_13 AC 210 ms
20,864 KB
testcase_14 AC 208 ms
20,864 KB
testcase_15 AC 209 ms
20,864 KB
testcase_16 AC 212 ms
20,864 KB
testcase_17 AC 213 ms
20,992 KB
testcase_18 AC 213 ms
20,992 KB
testcase_19 AC 207 ms
20,864 KB
testcase_20 AC 212 ms
20,864 KB
testcase_21 AC 203 ms
20,736 KB
testcase_22 AC 175 ms
27,264 KB
testcase_23 AC 181 ms
27,264 KB
testcase_24 AC 181 ms
27,264 KB
testcase_25 AC 183 ms
27,136 KB
testcase_26 AC 180 ms
27,264 KB
testcase_27 AC 185 ms
27,244 KB
testcase_28 AC 176 ms
27,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, ll>;

void dfs(int v,int p, int &idx, const vector<vector<P> >& G, vector<int>& ls, vector<int>& rs, vector<int>& ds, vector<ll>& ws){
    ls[v]=idx++;
    for(const P& e : G[v]) {
        int u = e.first;
        ll w = e.second;
        if(u!=p) {
            ds[u] = ds[v] + 1;
            ws[u] = ws[v] + w;
            dfs(u,v,idx,G,ls,rs, ds, ws);
        }
    }
    rs[v]=idx;
}

template <typename T>
struct LazySegmentTree {
    int n;
    vector<T> data;
    vector<T> lazy;
    T INITIAL_DATA_VALUE;
    T INITIAL_LAZY_VALUE;

    //使うときは、この3つを適宜変更する
    static T merge(T x, T y);
    void updateNode(int k, T x);
    void apply(int k, int seg_len);

    void init(int size, T initial_data_value, T initial_lazy_value) {
        n = 1;
        INITIAL_DATA_VALUE = initial_data_value;
        INITIAL_LAZY_VALUE = initial_lazy_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_DATA_VALUE);
        lazy.resize(2 * n - 1, INITIAL_LAZY_VALUE);
    }

    void init(const vector<T>& v, T initial_data_value, T initial_lazy_value) {
        int size = v.size();
        n = 1;
        INITIAL_DATA_VALUE = initial_data_value;
        INITIAL_LAZY_VALUE = initial_lazy_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_DATA_VALUE);
        lazy.resize(2 * n - 1, INITIAL_LAZY_VALUE);

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

    LazySegmentTree(int size, T initial_data_value, T initial_lazy_value) {
        init(size, initial_data_value, initial_lazy_value);
    }

    LazySegmentTree(int size, T initial_value) {
        init(size, initial_value, initial_value);
    }

    LazySegmentTree(const vector<T>& v, T initial_data_value, T initial_lazy_value) {
        init(v, initial_data_value, initial_lazy_value);
    }

    LazySegmentTree(const vector<T>& v, T initial_value) {
        init(v, initial_value, initial_value);
    }

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

    void push(int k, int l, int r) {
        if (lazy[k] == INITIAL_LAZY_VALUE) return;
        apply(k, r - l);
        if (r - l > 1) {
            updateNode(2 * k + 1, lazy[k]);
            updateNode(2 * k + 2, lazy[k]);
        }
        lazy[k] = INITIAL_LAZY_VALUE;
    }

    //区間[a, b)に対する更新
    //k:節点番号, [l, r):節点に対応する区間
    void update(int a, int b, T x, int k, int l, int r) {
        push(k, l, r);
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return;
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) {
            updateNode(k, x);
            push(k, l, r);
        } else {
            update(a, b, x, k * 2 + 1, l, (l + r) / 2);
            update(a, b, x, k * 2 + 2, (l + r) / 2, r);
            data[k] = merge(data[2 * k + 1], data[2 * k + 2]);
        }
    }

    void update(int a, int b, T x) {
        update(a, b, x, 0, 0, n);
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b, int k, int l, int r) {
        push(k, l, r);
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return INITIAL_DATA_VALUE;
        //[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 merge(vl, vr);
        }
    }

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

//使うときは以下3つを変更
template <typename T>
T LazySegmentTree<T>::merge(T x, T y) {
    return x + y;
}

template <typename T>
void LazySegmentTree<T>::updateNode(int k, T x) {
    lazy[k] += x;
}

template <typename T>
void LazySegmentTree<T>::apply(int k, int seg_len) {
    data[k] += lazy[k] * seg_len;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector< vector<P> > G(n);
    for (int i = 1; i < n; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
    }

    int idx = 0;
    vector<int> ls(n), rs(n), ds(n, 0);
    vector<ll> ws(n, 0);
    dfs(0, -1, idx, G, ls, rs, ds, ws);

    LazySegmentTree<ll> st1(n, 0), st2(n, 0);
    for (int i = 0; i < n; i++) {
        st1.update(ls[i], ls[i] + 1, ws[i]);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int com;
        int a;
        cin >> com >> a;
        if (com == 1) {
            ll w;
            cin >> w;
            st1.update(ls[a], rs[a], -ds[a] * w);
            st2.update(ls[a], rs[a], w);
        } else {
            cout << st1.query(ls[a], ls[a] + 1) + st2.query(ls[a], ls[a] + 1) * ds[a] << "\n";
        }
    }
    return 0;
}
0