結果
問題 |
No.900 aδδitivee
|
ユーザー |
![]() |
提出日時 | 2020-01-03 01:13:22 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 800 ms / 2,000 ms |
コード長 | 3,104 bytes |
コンパイル時間 | 1,363 ms |
コンパイル使用メモリ | 168,072 KB |
実行使用メモリ | 43,648 KB |
最終ジャッジ日時 | 2024-11-22 18:51:39 |
合計ジャッジ時間 | 13,363 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 27 |
ソースコード
#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; }