結果
| 問題 |
No.900 aδδitivee
|
| コンテスト | |
| ユーザー |
yakamoto
|
| 提出日時 | 2019-10-04 23:54:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 282 ms / 2,000 ms |
| コード長 | 3,560 bytes |
| コンパイル時間 | 1,951 ms |
| コンパイル使用メモリ | 179,460 KB |
| 実行使用メモリ | 19,484 KB |
| 最終ジャッジ日時 | 2024-10-03 09:55:47 |
| 合計ジャッジ時間 | 9,792 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 27 |
ソースコード
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author
*/
#include <iostream>
#include <fstream>
#ifndef SOLUTION_COMMON_H
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PI = pair<int, int>;
template<class T> using V = vector<T>;
using VI = V<int>;
#define _1 first
#define _2 second
#ifdef MY_DEBUG
# define DEBUG(x) x
#else
# define DEBUG(x)
#endif
template<class T>
inline void debug(T &A) {
DEBUG(
for (const auto &a : A) {
cerr << a << " ";
}
cerr << '\n';
)
}
template<class T, class Func>
inline void debug_with_format(T &A, Func f) {
DEBUG(
for (const auto &a : A) {
cerr << f(a) << " ";
}
cerr << '\n';
)
}
template<class T>
inline void debug_dim2(T &A) {
DEBUG(
for (const auto &as : A) {
debug(as);
}
)
}
template<typename ... Args>
inline void debug(const char *format, Args const &... args) {
DEBUG(
fprintf(stderr, format, args ...);
cerr << '\n';
)
}
template<typename ... Args>
string format(const string &fmt, Args ... args) {
size_t len = snprintf(nullptr, 0, fmt.c_str(), args ...);
vector<char> buf(len + 1);
snprintf(&buf[0], len + 1, fmt.c_str(), args ...);
return string(&buf[0], &buf[0] + len);
}
template<class T1, class T2>
string fmtP(pair<T1, T2> a) {
stringstream ss;
ss << "(" << a._1 << "," << a._2 << ")";
return ss.str();
}
#define SOLUTION_COMMON_H
#endif //SOLUTION_COMMON_H
class BIT {
public:
int N;
V<ll> bit;
BIT(int n): N(n + 1), bit(N) {}
void add(int i, ll x) {
i++;
while(i < N) {
bit[i] += x;
i += i & -i;
}
}
ll query(int i) {
ll ans = 0ll;
while(i) {
ans += bit[i];
i -= i & -i;
}
return ans;
}
};
const int MOD = 1000000007;
class D {
public:
void solve(std::istream& in, std::ostream& out) {
int n;
in >> n;
V<V<PI>> g(n);
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
in >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
V<ll> dp(n);
VI tour;
V<PI> pos(n);
VI depth(n);
int mx_depth = 0;
function<void(int, int, int)> init = [&](int v, int p, int weight) {
pos[v]._1 = (int)tour.size();
tour.push_back(v);
if (p != -1) {
dp[v] = dp[p] + weight;
depth[v] = depth[p] + 1;
mx_depth = max(mx_depth, depth[v]);
}
for (const auto &e : g[v]) {
if (e._1 != p) init(e._1, v, e._2);
}
pos[v]._2 = (int)tour.size();
tour.push_back(v);
};
init(0, -1, 0);
debug(depth);
debug("%d", mx_depth);
// debug(tour);
// DEBUG(
// for (int i = 0; i < n; ++i) {
// cerr << "(" << pos[i]._1 << "," << pos[i]._2 << ") ";
// }
// )
BIT bit(2 * n);
BIT cum(2 * n);
int q;
in >> q;
for (int i = 0; i < q; ++i) {
int tpe;
in >> tpe;
if (tpe == 1) {
int a, x;
in >> a >> x;
bit.add(pos[a]._1, x);
bit.add(pos[a]._2, -x);
cum.add(pos[a]._1, ((ll)(mx_depth - depth[a])) * x);
cum.add(pos[a]._2, -((ll)(mx_depth - depth[a])) * x);
} else {
int b;
in >> b;
debug("%d %d", b, pos[b]._1);
ll v = cum.query(pos[b]._1) - bit.query(pos[b]._1) * (mx_depth - depth[b]) + dp[b];
out << v << '\n';
}
}
}
};
int main() {
D solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
yakamoto