結果

問題 No.900 aδδitivee
ユーザー koyoprokoyopro
提出日時 2020-01-03 01:13:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 945 ms / 2,000 ms
コード長 3,104 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 168,336 KB
実行使用メモリ 43,648 KB
最終ジャッジ日時 2024-05-02 07:01:49
合計ジャッジ時間 14,058 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,016 KB
testcase_01 AC 4 ms
6,016 KB
testcase_02 AC 4 ms
6,144 KB
testcase_03 AC 5 ms
6,016 KB
testcase_04 AC 4 ms
6,144 KB
testcase_05 AC 4 ms
6,144 KB
testcase_06 AC 4 ms
6,016 KB
testcase_07 AC 298 ms
40,576 KB
testcase_08 AC 295 ms
40,704 KB
testcase_09 AC 307 ms
40,576 KB
testcase_10 AC 307 ms
40,704 KB
testcase_11 AC 312 ms
40,576 KB
testcase_12 AC 308 ms
40,576 KB
testcase_13 AC 306 ms
40,576 KB
testcase_14 AC 305 ms
40,704 KB
testcase_15 AC 319 ms
40,576 KB
testcase_16 AC 306 ms
40,576 KB
testcase_17 AC 314 ms
40,576 KB
testcase_18 AC 310 ms
40,704 KB
testcase_19 AC 305 ms
40,576 KB
testcase_20 AC 303 ms
40,576 KB
testcase_21 AC 322 ms
40,576 KB
testcase_22 AC 781 ms
43,520 KB
testcase_23 AC 801 ms
43,520 KB
testcase_24 AC 798 ms
43,648 KB
testcase_25 AC 790 ms
43,520 KB
testcase_26 AC 779 ms
43,520 KB
testcase_27 AC 771 ms
43,520 KB
testcase_28 AC 945 ms
43,520 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 et = 0;
int LS[222222]; // iに対するTの添字(スタート時)
int RS[222222]; // iに対するTの添字(終了時)

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++;
}

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

/********************************
 *
 * 平方分割
 *
 *******************************/
class SquareRootDecomposition {
    vector<int> S; // 少ない方
    vector<int> T; // 多い方
    int K;
    
public:
    SquareRootDecomposition(int size) {
        T = vector<int>(size, 0);
        S = vector<int>(sqrt(size)+1, 0);
        K = sqrt(size);
    }
    
    void add(int l, int r, int w) {
        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;
        }
    }

    int query(int l) {
        return T[l] + S[l / K];
    }
};

/**********************************************************/


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);
    }
    
    SquareRootDecomposition A(N*2), B(N*2);

    setup_tree();
    
    cin >> Q;
    int c,a,b,x;
    REP(i,Q) {
        cin >> c;
        if (c == 1) {
            cin >> a >> x;
            A.add(LS[a]+1, RS[a], -x * D[a]);
            B.add(LS[a]+1, RS[a], x);
        } else {
            cin >> b;
            out("%lld\n", W[b] + A.query(LS[b]) + B.query(LS[b]) * D[b]);
        }
    }
    
    return 0;
}
0