結果

問題 No.900 aδδitivee
ユーザー face4face4
提出日時 2020-01-13 17:10:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 293 ms / 2,000 ms
コード長 2,570 bytes
コンパイル時間 2,723 ms
コンパイル使用メモリ 81,936 KB
実行使用メモリ 31,284 KB
最終ジャッジ日時 2023-08-22 22:06:39
合計ジャッジ時間 12,442 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,760 KB
testcase_01 AC 3 ms
5,664 KB
testcase_02 AC 3 ms
5,792 KB
testcase_03 AC 3 ms
5,716 KB
testcase_04 AC 3 ms
5,704 KB
testcase_05 AC 4 ms
5,728 KB
testcase_06 AC 3 ms
5,768 KB
testcase_07 AC 292 ms
23,588 KB
testcase_08 AC 291 ms
23,868 KB
testcase_09 AC 289 ms
23,780 KB
testcase_10 AC 290 ms
23,896 KB
testcase_11 AC 290 ms
23,836 KB
testcase_12 AC 285 ms
23,612 KB
testcase_13 AC 285 ms
23,652 KB
testcase_14 AC 284 ms
23,744 KB
testcase_15 AC 290 ms
23,684 KB
testcase_16 AC 288 ms
23,712 KB
testcase_17 AC 293 ms
23,644 KB
testcase_18 AC 291 ms
23,604 KB
testcase_19 AC 287 ms
23,648 KB
testcase_20 AC 290 ms
23,812 KB
testcase_21 AC 292 ms
23,636 KB
testcase_22 AC 279 ms
31,236 KB
testcase_23 AC 282 ms
31,196 KB
testcase_24 AC 284 ms
31,240 KB
testcase_25 AC 285 ms
31,228 KB
testcase_26 AC 282 ms
31,200 KB
testcase_27 AC 285 ms
31,240 KB
testcase_28 AC 281 ms
31,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

typedef long long ll;

class STlazy{
private:
    int n;
    vector<ll> node, lazy;
    vector<int> plus, minus;

public:
    STlazy(vector<int> v, vector<int> p){
        int siz = v.size();
        n = 1;
        while(n < siz)  n *= 2;
        node.resize(2*n-1, 0);
        lazy.resize(2*n-1, 0);
        plus.resize(2*n-1, 0);
        minus.resize(2*n-1, 0);
        for(int i = 0; i < siz; i++)    node[n-1+i] = v[i], plus[n-1+i] = p[i], minus[n-1+i] = 1-p[i];
        for(int i = n-2; i >= 0; i--)   node[i] = node[2*i+1] + node[2*i+2], plus[i] = plus[2*i+1]+plus[2*i+2], minus[i] = minus[2*i+1]+minus[2*i+2];
    }

    void eval(int k, int l, int r){
        if(lazy[k] == 0)    return;
        node[k] += lazy[k]*(plus[k]-minus[k]);
        if(r-l > 1){
            lazy[2*k+1] += lazy[k];
            lazy[2*k+2] += lazy[k];
        }
        lazy[k] = 0;
    }

    void add(int a, int b, ll x, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        eval(k, l, r);
        if(r <= a || b <= l)    return;
        if(a <= l && r <= b){
            lazy[k] += x;
            eval(k, l, r);
            return;
        }
        add(a, b, x, 2*k+1, l, (l+r)/2);
        add(a, b, x, 2*k+2, (l+r)/2, r);
        node[k] = node[2*k+1] + node[2*k+2];
    }

    ll query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0)   r = n;
        if(r <= a || b <= l)    return 0;
        eval(k, l, r);
        if(a <= l && r <= b)    return node[k];
        ll lx = query(a, b, 2*k+1, l, (l+r)/2);
        ll rx = query(a, b, 2*k+2, (l+r)/2, r);
        return lx+rx;
    }
};

typedef pair<int,int> pii;

int k = 0;
vector<int> etour, pl;
vector<pii> v[100000];
int be[100000], en[100000];

void euler_tour(int x, int p){
    be[x] = k;
    for(pii P : v[x]){
        if(P.first == p)   continue;
        pl.push_back(1);
        etour.push_back(P.second);
        k++;
        euler_tour(P.first, x);
        pl.push_back(0);
        etour.push_back(-P.second);
        k++;
    }
    en[x] = k;
}

int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n-1; i++){
        int a, b, c;
        cin >> a >> b >> c;
        v[a].push_back({b,c});
    }
    euler_tour(0, -1);
    STlazy seg(etour, pl);
    int q;
    cin >> q;
    while(q--){
        int op, a, b;
        cin >> op;
        if(op == 1){
            cin >> a >> b;
            seg.add(be[a], en[a], b);
        }else{
            cin >> a;
            cout << seg.query(0, be[a]) << endl;
        }
    }
    return 0;
}
0