結果

問題 No.900 aδδitivee
ユーザー koyoprokoyopro
提出日時 2020-01-03 00:58:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 956 ms / 2,000 ms
コード長 4,001 bytes
コンパイル時間 1,547 ms
コンパイル使用メモリ 150,492 KB
実行使用メモリ 43,488 KB
最終ジャッジ日時 2023-08-14 19:00:23
合計ジャッジ時間 15,190 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,584 KB
testcase_01 AC 4 ms
13,768 KB
testcase_02 AC 5 ms
13,752 KB
testcase_03 AC 5 ms
13,792 KB
testcase_04 AC 6 ms
13,896 KB
testcase_05 AC 5 ms
13,844 KB
testcase_06 AC 5 ms
13,900 KB
testcase_07 AC 284 ms
41,820 KB
testcase_08 AC 277 ms
40,220 KB
testcase_09 AC 276 ms
40,400 KB
testcase_10 AC 284 ms
40,216 KB
testcase_11 AC 277 ms
40,392 KB
testcase_12 AC 276 ms
40,340 KB
testcase_13 AC 289 ms
40,580 KB
testcase_14 AC 284 ms
40,572 KB
testcase_15 AC 273 ms
40,352 KB
testcase_16 AC 275 ms
40,392 KB
testcase_17 AC 287 ms
40,264 KB
testcase_18 AC 284 ms
40,336 KB
testcase_19 AC 295 ms
40,572 KB
testcase_20 AC 286 ms
40,400 KB
testcase_21 AC 286 ms
40,400 KB
testcase_22 AC 921 ms
43,476 KB
testcase_23 AC 898 ms
43,340 KB
testcase_24 AC 879 ms
43,476 KB
testcase_25 AC 917 ms
43,448 KB
testcase_26 AC 916 ms
43,296 KB
testcase_27 AC 934 ms
43,488 KB
testcase_28 AC 956 ms
43,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)

int dxy[] = {0, 1, 0, -1, 0};

/*================================*/

int N,Q;

/**********************************
 *
 * Graph
 *
 **********************************/

struct Edge {
    int src, to, w;
    Edge(int u, int v, int w): src(u), to(v), w(w) {};
};

vector< vector<Edge> > E(111111);
int W[111111];
int D[111111];
int P[111111][20];

// 平方分割: 各番号へ、追加の重み情報が入る
int S[2222];
int T[222222];
// 深さに掛ける分
int SB[2222];
int TB[222222];
// オイラーツアー:
int et = 0;
int LS[222222]; // iに対するTの添字(スタート時)
int RS[222222]; // iに対するTの添字(終了時)

void doubling() {
    FOR(j, 1, 20) {
        REP(i, N) {
            P[i][j] = (P[i][j-1] == -1) ? -1 : P[P[i][j-1]][j-1];
        }
    }
}

void add_edge(int u, int v, int w) {
    E[u].push_back(*new Edge(u,v,w));
    E[v].push_back(*new Edge(v,u,w));
}

void dfs(int parent=-1, int i=0, int weight=0, int depth=0) {
    P[i][0] = parent;
    W[i] = weight;
    D[i] = depth;
    LS[i] = et++;
    for (auto e : E[i]) {
        if (e.to == parent) continue;
        dfs(i, e.to, weight + e.w, depth+1);
    }
    RS[i] = et++;
}

int lca(int u, int v) {
    // depth調整
    if (D[u] > D[v]) swap(u, v);
    RFOR(i, 0, 20) {
        if ((D[v] - D[u])&(1<<i)) v = P[v][i];
    }
    if (u == v) return u;
    
    // lca
    RFOR(i, 0, 20) {
        if (P[v][i] != P[u][i]) {
            v = P[v][i];
            u = P[u][i];
        }
    }
    return P[u][0];
}

int route_weight(int u, int v) {
    return W[u] + W[v] - W[lca(u, v)] * 2;
}

void setup_tree() {
    REP(i,N)REP(j,20) P[i][j]=-1;
    dfs();
    doubling();
}

void add_weight(int i, int w) {
    int l = LS[i] + 1; // 範囲内
    int r = RS[i];     // 範囲外
    int k = sqrt(N);
    while(l < r) {
        int lk = l / k;
        int rk = r / k;
        if (lk == rk) {
            FOR(j, l, r) {
                T[j] += w;
            }
        }
        else if (l % k == 0) {
            S[lk] += w;
        }
        else {
            FOR(j, l, (lk+1)*k) {
                T[j] += w;
            }
        }
        l = (lk+1)*k;
    }
}
void add_weightb(int i, int w) {
    int l = LS[i] + 1; // 範囲内
    int r = RS[i];     // 範囲外
    int k = sqrt(N);
    while(l < r) {
        int lk = l / k;
        int rk = r / k;
        if (lk == rk) {
            FOR(j, l, r) {
                TB[j] += w;
            }
        }
        else if (l % k == 0) {
            SB[lk] += w;
        }
        else {
            FOR(j, l, (lk+1)*k) {
                TB[j] += w;
            }
        }
        l = (lk+1)*k;
    }
}

int query(int i) {
    int k = sqrt(N);
    int l = LS[i];
    return W[i] + (T[l] + S[l / k]) + (TB[l] + SB[l/k]) * D[i];
}
/**********************************************************/


signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin>>N;
    
    int u,v,w;
    REP(i,N-1) {
        cin >> u >> v >> w;
        add_edge(u, v, w);
    }
    REP(i, N*2) T[i] = TB[i] = 0;
    REP(i, sqrt(N)*2+2) S[i] = SB[i] = 0;

    setup_tree();
    
    cin >> Q;
    int c,a,b,x;
    REP(i,Q) {
        cin >> c;
        if (c == 1) {
            cin >> a >> x;
            add_weight(a, -x * D[a]);
            add_weightb(a, x);
        } else {
            cin >> b;
            out("%lld\n", query(b));
        }
    }
    
    return 0;
}
0