結果
| 問題 |
No.900 aδδitivee
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-04 22:04:14 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 585 ms / 2,000 ms |
| コード長 | 7,706 bytes |
| コンパイル時間 | 2,434 ms |
| コンパイル使用メモリ | 200,268 KB |
| 実行使用メモリ | 39,552 KB |
| 最終ジャッジ日時 | 2024-10-03 07:40:25 |
| 合計ジャッジ時間 | 14,270 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 27 |
ソースコード
#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(ll (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using Graph = vector<vector<ll>>;
using WGraph = vector<vector<pair<ll, ll>>>;
struct HLDecomposition {
using pii = pair<ll, ll>;
ll n;
Graph G;
vector<ll> vid, inv, par, depth, subsize, head, prev, next, type;
HLDecomposition(const Graph& G_) :
n(G_.size()), G(G_),
vid(n, -1), inv(n), par(n), depth(n), subsize(n, 1),
head(n), prev(n, -1), next(n, -1), type(n) {}
void build(vector<ll> roots = { 0 }) {
ll curtype = 0, pos = 0;
for (ll root : roots) {
decide_heavy_edge(root);
reconstruct(root, curtype++, pos);
}
}
void decide_heavy_edge(ll root) {
stack<pii> st;
par[root] = -1, depth[root] = 0;
st.emplace(root, 0);
while (!st.empty()) {
ll now = st.top().first;
ll& way = st.top().second;
if (way < G[now].size()) {
ll child = G[now][way++];
if (child == par[now])continue;
par[child] = now;
depth[child] = depth[now] + 1;
st.emplace(child, 0);
}
else {
st.pop();
ll maxsize = 0;
for (auto child : G[now]) {
if (child == par[now])continue;
subsize[now] += subsize[child];
if (maxsize < subsize[child]) {
maxsize = subsize[child];
prev[child] = now;
next[now] = child;
}
}
}
}
}
void reconstruct(ll root, ll curtype, ll& pos) {
stack<ll> st({ root });
while (!st.empty()) {
ll start = st.top(); st.pop();
for (ll v = start; v != -1; v = next[v]) {
type[v] = curtype;
vid[v] = pos++;
inv[vid[v]] = v;
head[v] = start;
for (auto child : G[v]) {
if (child != par[v] && child != next[v]) {
st.push(child);
}
}
}
}
}
// node query [u, v], f([left, right])
void foreach_nodes(ll u, ll v, const function<void(ll, ll)>& f) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
f(max(vid[head[v]], vid[u]), vid[v]);
if (head[u] != head[v])v = par[head[v]];
else break;
}
}
// edge query[u,v] f([left, right])
// seg_node[vid[i]] := edge(par[i] -> i)
void foreach_edges(ll u, ll v, const function<void(ll, ll)>& f) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
if (head[u] != head[v]) {
f(vid[head[v]], vid[v]);
v = par[head[v]];
}
else {
if (u != v)f(vid[u] + 1, vid[v]);
break;
}
}
}
ll lca(ll u, ll v) {
while (true) {
if (vid[u] > vid[v])swap(u, v);
if (head[u] == head[v])return u;
v = par[head[v]];
}
}
};
template<typename Monoid, typename OperatorMonoid = Monoid>
class LazySegmentTree {
private:
using F = function<Monoid(Monoid, Monoid)>;
using G = function<Monoid(Monoid, OperatorMonoid, ll)>;
using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
ll sz; // 対応する配列の幅
vector<Monoid> data;
vector<OperatorMonoid> lazy;
const F f; // 2区間マージ演算(data-data-ボトムアップマージ)
const G g; // 要素,作用素マージ演算(lazy->data同位置変換時の、(data,lazy,len)の計算)
const H h; // 作用素マージ演算 (query->lazyトップダウン伝搬時の、(lazy,query_value)の計算)
const Monoid M1; // モノイド単位元 (data単位元)
const OperatorMonoid OM0; // 作用素単位元 (lazy単位元)
void propagate(ll idx, ll len) {
// 幅lenのlazy[idx]が存在するとき、値を下に流す
if (lazy[idx] != OM0) {
if (idx < sz) {
lazy[(idx << 1) | 0] = h(lazy[(idx << 1) | 0], lazy[idx]);
lazy[(idx << 1) | 1] = h(lazy[(idx << 1) | 1], lazy[idx]);
}
data[idx] = g(data[idx], lazy[idx], len);
lazy[idx] = OM0;
}
}
Monoid update_impl(ll a, ll b, const OperatorMonoid& val, ll idx, ll l, ll r) {
propagate(idx, r - l);
if (r <= a || b <= l)return data[idx];
else if (a <= l && r <= b) {
lazy[idx] = h(lazy[idx], val);
propagate(idx, r - l);
return data[idx];
}
else return data[idx] = f(
update_impl(a, b, val, (idx << 1) | 0, l, (l + r) >> 1),
update_impl(a, b, val, (idx << 1) | 1, (l + r) >> 1, r)
);
}
Monoid query_impl(ll a, ll b, ll idx, ll l, ll r) {
propagate(idx, r - l);
if (r <= a || b <= l)return M1;
else if (a <= l && r <= b)return data[idx];
else return f(
query_impl(a, b, (idx << 1) | 0, l, (l + r) >> 1),
query_impl(a, b, (idx << 1) | 1, (l + r) >> 1, r)
);
}
public:
// init忘れに注意
LazySegmentTree(ll n, const F f, const G g, const H h,
const Monoid& M1, const OperatorMonoid OM0)
:f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
while (sz < n)sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void build(const vector<Monoid>& vals) {
rep(idx, vals.size())data[idx + sz] = vals[idx];
for (ll idx = sz - 1; idx > 0; idx--) {
data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
}
}
Monoid update(ll a, ll b, const OperatorMonoid& val) {
return update_impl(a, b, val, 1, 0, sz);
}
Monoid query(ll a, ll b) {
return query_impl(a, b, 1, 0, sz);
}
Monoid operator[](const ll& idx) {
return query(idx, idx + 1);
}
};
int main()
{
ll N;
cin >> N;
Graph gh(N);
map<pair<ll, ll>, ll> weight;
rep(i, N - 1) {
ll u, v, w;
cin >> u >> v >> w;
gh[u].push_back(v);
gh[v].push_back(u);
weight[{u, v}] = weight[{v, u}] = w;
}
HLDecomposition hld(gh); hld.build();
vector<ll> weights(N, 0);
REP(i, 1, N) {
auto p = make_pair(i, hld.par[i]);
if (weight.find(p) != weight.end()) {
weights[hld.vid[i]] = weight[p];
continue;
}
swap(p.first, p.second);
if (weight.find(p) != weight.end()) {
weights[hld.vid[i]] = weight[p];
continue;
}
assert(false);
}
auto f = [](ll vl, ll vr) {return vl + vr; };
auto g = [](ll data, ll lazy, int len) {return data + lazy * len; };
auto h = [](ll lazy, ll query) {return lazy + query; };
LazySegmentTree<ll> lst(N, f, g, h, 0, 0);
lst.build(weights);
ll Q;
cin >> Q;
while (Q--) {
ll type;
cin >> type;
if (type == 1) {
ll a, x;
cin >> a >> x;
ll siz = hld.subsize[a];
a = hld.vid[a];
lst.update(a + 1, a + siz, x);
}
else {
ll b, res = 0;
cin >> b;
hld.foreach_edges(0, b, [&](ll l, ll r) {
res += lst.query(l, r + 1);
});
cout << res << endl;
}
}
return 0;
}