結果

問題 No.900 aδδitivee
ユーザー Y17Y17
提出日時 2019-10-04 23:08:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 4,502 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 178,108 KB
実行使用メモリ 28,516 KB
最終ジャッジ日時 2024-04-14 11:35:05
合計ジャッジ時間 11,479 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
19,128 KB
testcase_01 AC 11 ms
19,752 KB
testcase_02 AC 12 ms
19,796 KB
testcase_03 AC 12 ms
19,296 KB
testcase_04 AC 11 ms
19,124 KB
testcase_05 AC 12 ms
19,012 KB
testcase_06 AC 11 ms
19,192 KB
testcase_07 AC 352 ms
22,900 KB
testcase_08 AC 349 ms
22,828 KB
testcase_09 AC 354 ms
22,812 KB
testcase_10 AC 345 ms
22,472 KB
testcase_11 AC 345 ms
22,432 KB
testcase_12 AC 345 ms
22,792 KB
testcase_13 AC 342 ms
22,960 KB
testcase_14 AC 341 ms
23,024 KB
testcase_15 AC 351 ms
22,852 KB
testcase_16 AC 349 ms
23,236 KB
testcase_17 AC 346 ms
22,704 KB
testcase_18 AC 343 ms
22,344 KB
testcase_19 AC 349 ms
22,444 KB
testcase_20 AC 361 ms
22,572 KB
testcase_21 AC 344 ms
22,860 KB
testcase_22 AC 342 ms
27,860 KB
testcase_23 AC 343 ms
28,420 KB
testcase_24 AC 346 ms
27,976 KB
testcase_25 AC 345 ms
27,584 KB
testcase_26 AC 342 ms
28,512 KB
testcase_27 AC 346 ms
27,736 KB
testcase_28 AC 346 ms
28,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

//SegmentTree(n, f, g, h, M1, OM0):= サイズ n の初期化。ここで f は2つの区間の要素をマージする二項演算, g は要素と作用素をマージする二項演算(第三引数は対応する区間の長さ), h は作用素同士をマージする二項演算, M1 はモノイドの単位元, OM0は 作用素の単位元である。
//update(a, b, x) := 区間 [a,b) に作用素 xを適用する。

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
    using F = function< Monoid(Monoid, Monoid) >;
    using G = function< Monoid(Monoid, OperatorMonoid, int) >;
    using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

    int sz;
    vector< Monoid > data;
    vector< OperatorMonoid > lazy;
    const F f;
    const G g;
    const H h;
    const Monoid M1;
    const OperatorMonoid OM0;


    LazySegmentTree(int n, const F f, const G g, const H h,
            const Monoid &M1, const OperatorMonoid OM0)
        : f(f), g(g), h(h), M1(M1), OM0(OM0) {
            sz = 1;
            while(sz < n) sz <<= 1;
            data.assign(2 * sz, M1);
            lazy.assign(2 * sz, OM0);
        }

    void set(int k, const Monoid &x) {
        data[k + sz] = x;
    }

    void build() {
        for(int k = sz - 1; k > 0; k--) {
            data[k] = f(data[2 * k + 0], data[2 * k + 1]);
        }
    }

    void propagate(int k, int len) {
        if(lazy[k] != OM0) {
            if(k < sz) {
                lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
                lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
            }
            data[k] = g(data[k], lazy[k], len);
            lazy[k] = OM0;
        }
    }

    Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
        propagate(k, r - l);
        if(r <= a || b <= l) {
            return data[k];
        } else if(a <= l && r <= b) {
            lazy[k] = h(lazy[k], x);
            propagate(k, r - l);
            return data[k];
        } else {
            return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
                    update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
        }
    }

    Monoid update(int a, int b, const OperatorMonoid &x) {
        return update(a, b, x, 1, 0, sz);
    }


    Monoid query(int a, int b, int k, int l, int r) {
        propagate(k, r - l);
        if(r <= a || b <= l) {
            return M1;
        } else if(a <= l && r <= b) {
            return data[k];
        } else {
            return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
                    query(a, b, 2 * k + 1, (l + r) >> 1, r));
        }
    }

    Monoid query(int a, int b) {
        return query(a, b, 1, 0, sz);
    }

    Monoid operator[](const int &k) {
        return query(k, k + 1);
    }
};

//SegmentTree(n, f, g, h, M1, OM0):= サイズ n の初期化。ここで f は2つの区間の要素をマージする二項演算, g は要素と作用素をマージする二項演算(第三引数は対応する区間の長さ), h は作用素同士をマージする二項演算, M1 はモノイドの単位元, OM0は 作用素の単位元である。
#define N 200010
#define int long long

struct LazySegmentTree<pair<int, int>, int> segtree(N,
    [](pair<int, int> a, pair<int, int> b){ return make_pair(a.first + b.first, a.second + b.second);},
    [](pair<int, int> a, int b, int z){ return make_pair(a.first + a.second * b, a.second);},
    [](int a, int b){ return a + b; },
    make_pair(0, 0), 0);

int ds[N];
int us[N];

vector<pair<int, int>> graph[100010];

void dfs(int v, int p, int &k){
    for(auto u : graph[v]){
        if(u.first == p) continue;
        ds[u.first] = k++;
        segtree.set(ds[u.first], {u.second, 1});
        dfs(u.first, v, k);
        us[u.first] = k++;
        segtree.set(us[u.first], {-u.second, -1});
    }
}

signed main(){
    int n;
    cin >> n;

    for(int i = 0;i < n-1;i++){
        int u, v, w;
        cin >> u >> v >> w;
        graph[u].push_back({v, w});
    }

    int num = 0;

    ds[0] = -1;
    dfs(0, -1, num);
    us[0] = num;

    segtree.build();

    int q; cin >> q;
    while(q--){
        int query, a, x;
        cin >> query;
        if(query == 1){
            cin >> a >> x;
            segtree.update(ds[a]+1, us[a], x);
        }else{
            cin >> a;
            cout << segtree.query(0, ds[a]+1).first << endl;
        }
    }

    return 0;
};

0