Submission #1110048

#TimeUsernameProblemLanguageResultExecution timeMemory
1110048IcelastValley (BOI19_valley)C++17
100 / 100
355 ms42028 KiB
#include <iostream> #include <bits/stdc++.h> #define ll long long using namespace std; const ll maxn = 2*1e5+5, INF = 1e18+9; template<class Info, class Tag> struct LazySegmentTree { int n; vector<Info> info; vector<Tag> tag; LazySegmentTree() : n(0) {} LazySegmentTree(int n_, Info v_ = Info()) { init(n_, v_); } template<class T> LazySegmentTree(vector<T> init_) { init(init_); } void init(int n_, Info v_ = Info()) { init(vector<Info>(n_, v_)); } template<class T> void init(vector<T> init_) { n = init_.size(); info.assign(4 << __lg(n), Info()); tag.assign(4 << __lg(n), Tag()); function<void(int, int, int)> build = [&](int p, int l, int r) { if (r - l == 1) { info[p] = init_[l]; return; } int m = (l + r) / 2; build(2 * p, l, m); build(2 * p + 1, m, r); pull(p); }; build(1, 0, n); } void pull(int p) { info[p] = info[2 * p] + info[2 * p + 1]; } void apply(int p, const Tag &v) { info[p].apply(v); tag[p].apply(v); } void push(int p) { apply(2 * p, tag[p]); apply(2 * p + 1, tag[p]); tag[p] = Tag(); } void modify(int p, int l, int r, int x, const Info &v) { if (r - l == 1) { info[p] = v; return; } int m = (l + r) / 2; push(p); if (x < m) { modify(2 * p, l, m, x, v); } else { modify(2 * p + 1, m, r, x, v); } pull(p); } void modify(int p, const Info &v) { modify(1, 0, n, p, v); } Info rangeQuery(int p, int l, int r, int x, int y) { if (l >= y || r <= x) { return Info(); } if (l >= x && r <= y) { return info[p]; } int m = (l + r) / 2; push(p); return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y); } Info rangeQuery(int l, int r) { return rangeQuery(1, 0, n, l, r); } void rangeApply(int p, int l, int r, int x, int y, const Tag &v) { if (l >= y || r <= x) { return; } if (l >= x && r <= y) { apply(p, v); return; } int m = (l + r) / 2; push(p); rangeApply(2 * p, l, m, x, y, v); rangeApply(2 * p + 1, m, r, x, y, v); pull(p); } void rangeApply(int l, int r, const Tag &v) { return rangeApply(1, 0, n, l, r, v); } template<class F> int findFirst(int p, int l, int r, int x, int y, F &&pred) { if (l >= y || r <= x) { return -1; } if (l >= x && r <= y && !pred(info[p])) { return -1; } if (r - l == 1) { return l; } int m = (l + r) / 2; push(p); int res = findFirst(2 * p, l, m, x, y, pred); if (res == -1) { res = findFirst(2 * p + 1, m, r, x, y, pred); } return res; } template<class F> int findFirst(int l, int r, F &&pred) { return findFirst(1, 0, n, l, r, pred); } template<class F> int findLast(int p, int l, int r, int x, int y, F &&pred) { if (l >= y || r <= x) { return -1; } if (l >= x && r <= y && !pred(info[p])) { return -1; } if (r - l == 1) { return l; } int m = (l + r) / 2; push(p); int res = findLast(2 * p + 1, m, r, x, y, pred); if (res == -1) { res = findLast(2 * p, l, m, x, y, pred); } return res; } template<class F> int findLast(int l, int r, F &&pred) { return findLast(1, 0, n, l, r, pred); } }; struct Tag { ll add = 0; void apply(const Tag &t) & { add += t.add; } }; struct Info { ll mn = INF; void apply(const Tag &t) & { mn += t.add; } Info operator+(const Info &b) { return {min(mn, b.mn)}; } }; struct edge{ ll u, v, w; }; struct que{ int I, R, id; }; void solve(){ int n, S, Q, H; cin >> n >> S >> Q >> H; vector<vector<pair<ll, ll>>> adj(n+1); vector<edge> e(n); for(int i = 1; i < n; i++){ ll u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w}); e[i] = {u, v, w}; } vector<bool> is_special(n+1, false); for(int i = 1; i <= S; i++){ int x; cin >> x; is_special[x] = true; } vector<que> q(Q+1); for(int i = 1; i <= Q; i++){ cin >> q[i].I >> q[i].R; q[i].id = i; } int timer = 0; vector<int> rt(n+1), lt(n+1); vector<ll> f(n+1, INF); vector<int> depth(n+1, -1); auto root_tree = [&](auto root_tree, int u, int p, ll len) -> void{ timer ++; lt[u] = timer; depth[u] = depth[p]+1; if(is_special[u]){ f[u] = len; }else{ f[u] = INF; } for(auto it : adj[u]){ int v = it.first; ll w = it.second; if(v == p) continue; root_tree(root_tree, v, u, len+w); } rt[u] = timer; }; root_tree(root_tree, H, 0, 0); for(int i = 1; i < n; i++){ if(depth[e[i].u] > depth[e[i].v]){ swap(e[i].u, e[i].v); } } vector<vector<que>> b(n+1); for(int i = 1; i <= Q; i++){ b[q[i].R].push_back(q[i]); } LazySegmentTree<Info, Tag> T(n+1); for(int i = 1; i <= n; i++){ T.modify(lt[i], {f[i]}); } vector<ll> ans(Q+1, -3); auto is_anc = [&](int u, int v) -> bool{ return lt[u] <= lt[v] && lt[v] <= rt[u]; }; for(int i = 1; i <= Q; i++){ int v = e[q[i].I].v; if(!is_anc(v, q[i].R)){ ans[i] = -2; } } auto dfs = [&](auto dfs, int u, int p) -> void{ for(auto it : b[u]){ ll res = INF; int id = it.id; if(ans[id] == -2) continue; int v = e[it.I].v; if(is_anc(v, u)){ res = T.rangeQuery(lt[v], rt[v]+1).mn; }else{ res = min(T.rangeQuery(1, lt[v]).mn, T.rangeQuery(rt[v]+1, n+1).mn); } if(res >= 1e15){ ans[id] = -1; }else{ ans[id] = res; } } for(auto it : adj[u]){ int v = it.first; ll w = it.second; if(v == p) continue; ll prv_fu, prv_fv, fu, fv; T.rangeApply(1, lt[v], {w}); T.rangeApply(rt[v]+1, n+1, {w}); T.rangeApply(lt[v], rt[v]+1, {-w}); dfs(dfs, v, u); T.rangeApply(1, lt[v], {-w}); T.rangeApply(rt[v]+1, n+1, {-w}); T.rangeApply(lt[v], rt[v]+1, {w}); } }; dfs(dfs, H, 0); for(int i = 1; i <= Q; i++){ if(ans[i] == -2){ cout << "escaped"; } if(ans[i] == -1){ cout << "oo"; } if(ans[i] >= 0){ cout << ans[i]; } cout << "\n"; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }

Compilation message (stderr)

valley.cpp: In instantiation of 'solve()::<lambda(auto:24, int, int)> [with auto:24 = solve()::<lambda(auto:24, int, int)>]':
valley.cpp:270:18:   required from here
valley.cpp:258:16: warning: unused variable 'prv_fu' [-Wunused-variable]
  258 |             ll prv_fu, prv_fv, fu, fv;
      |                ^~~~~~
valley.cpp:258:24: warning: unused variable 'prv_fv' [-Wunused-variable]
  258 |             ll prv_fu, prv_fv, fu, fv;
      |                        ^~~~~~
valley.cpp:258:32: warning: unused variable 'fu' [-Wunused-variable]
  258 |             ll prv_fu, prv_fv, fu, fv;
      |                                ^~
valley.cpp:258:36: warning: unused variable 'fv' [-Wunused-variable]
  258 |             ll prv_fu, prv_fv, fu, fv;
      |                                    ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...