結果
問題 | No.1197 モンスターショー |
ユーザー |
|
提出日時 | 2020-11-04 07:59:40 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 661 ms / 3,000 ms |
コード長 | 8,174 bytes |
コンパイル時間 | 1,456 ms |
コンパイル使用メモリ | 114,580 KB |
最終ジャッジ日時 | 2025-01-15 19:39:40 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 41 |
ソースコード
#include <iostream>#include <vector>#include <functional>template <class Cost = int>struct Edge {int src, dst;Cost cost;Edge() = default;Edge(int src, int dst, Cost cost = 1): src(src), dst(dst), cost(cost){};bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }};template <class Cost = int>struct Graph : public std::vector<std::vector<Edge<Cost>>> {Graph(int n = 0) : std::vector<std::vector<Edge<Cost>>>(n) {}void span(bool direct, int src, int dst, Cost cost = 1) {(*this)[src].emplace_back(src, dst, cost);if (!direct) (*this)[dst].emplace_back(dst, src, cost);}};template <class Cost>struct HeavyLightDecomposition {// indexing// v: a vertex in original graph// i: assigned label of a vertexGraph<Cost> graph;std::vector<int> id, vs; // id: v -> i, vs: i -> vstd::vector<int> par, sz, head, dep, out;// these are all v-indexed// in equals to idint time;explicit HeavyLightDecomposition(const Graph<Cost>& graph): graph(graph),id(graph.size()),vs(graph.size()),par(graph.size()),sz(graph.size()),head(graph.size()),dep(graph.size()),out(graph.size()),time(0) {dfs_sz(0, -1, 0);head[0] = 0;dfs_hld(0, -1);}void dfs_sz(int v, int p, int d) {par[v] = p;sz[v] = 1;dep[v] = d;if (!graph[v].empty() && graph[v].front().dst == p) {std::swap(graph[v].front(), graph[v].back());}for (auto& e : graph[v]) {if (e.dst == p) continue;dfs_sz(e.dst, v, d + 1);sz[v] += sz[e.dst];// heavy edge firstif (sz[graph[v].front().dst] < sz[e.dst]) {std::swap(graph[v].front(), e);}}}void dfs_hld(int v, int p) {id[v] = time++;vs[id[v]] = v;bool first = true;for (auto e : graph[v]) {if (e.dst == p) continue;head[e.dst] = (first ? head[v] : e.dst);first = false;dfs_hld(e.dst, v);}out[v] = time;}int lca(int u, int v) {while (true) {if (id[u] > id[v]) std::swap(u, v);if (head[u] == head[v]) return u;v = par[head[v]];}}int dist(int u, int v) {return dep[u] + dep[v] - dep[lca(u, v)] * 2;}std::vector<std::pair<int, int>> path(int u, int v, bool is_edge) {std::vector<std::pair<int, int>> segs;while (true) {if (id[u] > id[v]) std::swap(u, v);if (head[u] == head[v]) {// when edge path, the lca has to be excludedsegs.emplace_back(id[u] + is_edge, id[v] + 1);return segs;}segs.emplace_back(id[head[v]], id[v] + 1);v = par[head[v]];}}std::pair<int, int> subtree(int v, bool is_edge) {// when edge path, the root has to be excludedreturn {id[v] + is_edge, out[v]};}};template <class T, class E>struct LazySegmentTree {using DMerger = std::function<T(T, T)>;using OMerger = std::function<E(E, E)>;using Applier = std::function<T(T, E, int)>;int length;T d_unit;E o_unit;std::vector<T> dat;std::vector<E> ope;DMerger dmerge;OMerger omerge;Applier app;explicit LazySegmentTree(int n,T d_unit, E o_unit,DMerger dmerge,OMerger omerge,Applier app): length(1),d_unit(d_unit),o_unit(o_unit),dmerge(dmerge),omerge(omerge),app(app) {while (length < n) length <<= 1;dat.assign(length * 2, d_unit);ope.assign(length * 2, o_unit);}template <class Container>explicit LazySegmentTree(const Container& elems,T d_unit, E o_unit,DMerger dmerge,OMerger omerge,Applier app): length(1),d_unit(d_unit),o_unit(o_unit),dmerge(dmerge),omerge(omerge),app(app) {int n = elems.size();while (length < n) length <<= 1;dat.assign(length * 2, d_unit);ope.assign(length * 2, o_unit);std::copy(elems.begin(), elems.end(), dat.begin() + length);for (int nidx = length - 1; nidx >= 1; --nidx) {T vl = dat[nidx * 2 + 0];T vr = dat[nidx * 2 + 1];dat[nidx] = dmerge(vl, vr);}}void propagate(int nidx, int len) {if (ope[nidx] == o_unit) return;// propagateif (len > 1) {ope[nidx * 2 + 0] = omerge(ope[nidx * 2 + 0], ope[nidx]);ope[nidx * 2 + 1] = omerge(ope[nidx * 2 + 1], ope[nidx]);}// update datadat[nidx] = app(dat[nidx], ope[nidx], len);ope[nidx] = o_unit;}void update(int ql, int qr, E e, int nidx, int nl, int nr) {propagate(nidx, nr - nl);if (nr <= ql || qr <= nl) return;if (ql <= nl && nr <= qr) {ope[nidx] = omerge(ope[nidx], e);propagate(nidx, nr - nl);return;}int nm = (nl + nr) / 2;update(ql, qr, e, nidx * 2 + 0, nl, nm);update(ql, qr, e, nidx * 2 + 1, nm, nr);// update datadat[nidx] = dmerge(dat[nidx * 2 + 0], dat[nidx * 2 + 1]);}void update(int ql, int qr, E e) { return update(ql, qr, e, 1, 0, length); }T fold(int ql, int qr, int nidx, int nl, int nr) {propagate(nidx, nr - nl);if (nr <= ql || qr <= nl) return d_unit;if (ql <= nl && nr <= qr) return dat[nidx];int nm = (nl + nr) / 2;T vl = fold(ql, qr, nidx * 2 + 0, nl, nm);T vr = fold(ql, qr, nidx * 2 + 1, nm, nr);return dmerge(vl, vr);}T fold(int ql, int qr) { return fold(ql, qr, 1, 0, length); }T get(int idx) { return fold(idx, idx + 1); }T fold_all() { return fold(0, length); }};using lint = long long;void solve() {int n, m, q;std::cin >> n >> m >> q;std::vector<int> ps(m);for (auto& p : ps) {std::cin >> p;--p;}Graph<> graph(n);for (int i = n - 1; i--;) {int u, v;std::cin >> u >> v;graph.span(false, --u, --v);}HeavyLightDecomposition hld(graph);// range add range sumLazySegmentTree<lint, lint>seg(n, 0, 0,[](auto a, auto b) { return a + b; },[](auto e, auto f) { return e + f; },[](auto a, auto e, int k) { return a + e * k; });lint base = 0;auto vadd = [&](int v, int d) {base += hld.dep[v] * d;for (auto [l, r] : hld.path(0, v, false)) {seg.update(l, r, d);}};for (auto p : ps) vadd(p, 1);while (q--) {int t;std::cin >> t;switch (t) {case 1: {int i, v;std::cin >> i >> v;--i, --v;auto& p = ps[i];vadd(p, -1);p = v;vadd(p, 1);break;}case 2: {int v;std::cin >> v;--v;lint ans = base + lint(m) * hld.dep[v];for (auto [l, r] : hld.path(0, v, false)) {ans -= seg.fold(l, r) * 2;}ans += seg.get(0) * 2;std::cout << ans << "\n";break;}}}}int main() {std::cin.tie(nullptr);std::ios::sync_with_stdio(false);solve();return 0;}