Submission #959325

#TimeUsernameProblemLanguageResultExecution timeMemory
959325steveonalexValley (BOI19_valley)C++17
100 / 100
238 ms32776 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); // mt19937_64 rng(69); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const ll INF = 1e18 + 69; struct SegmentTree{ int n; vector<ll> a, lazy; SegmentTree(int _n){ n = _n; a.resize(n * 4 + 4); lazy.resize(n * 4 + 4); } void down(int id){ #define ligma(i) {a[i] += lazy[id]; lazy[i] += lazy[id];} ligma(id * 2); ligma(id * 2 + 1); lazy[id] = 0; } void update(int u, int v, ll val, int l, int r, int id){ if (u <= l && r <= v){ a[id] += val; lazy[id] += val; return; } if (lazy[id]) down(id); int mid = (l + r) >> 1; if (u <= mid) update(u, v, val, l, mid, id * 2); if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1); a[id] = min(a[id * 2], a[id * 2 + 1]); } void update(int u, int v, ll val){ if (u > v) return; update(u, v, val, 1, n, 1); } ll get(int u, int v, int l, int r, int id){ if (u <= l && r <= v) return a[id]; if (lazy[id]) down(id); int mid = (l + r) >> 1; if (v <= mid) return get(u, v, l, mid, id * 2); if (u > mid) return get(u, v, mid + 1, r, id * 2 + 1); return min(get(u, v, l, mid, id * 2), get(u, v, mid + 1, r, id * 2 + 1)); } ll get(int u, int v){ if (u > v) return INF; return get(u, v, 1, n, 1); } }; const int N = 1e5 + 69; int n, s, q, e; vector<pair<int, int>> graph[N]; pair<int, int> edge[N]; int h[N], l[N], r[N]; ll sum[N]; bitset<N> shop; int dfs_cnt = 0; void dfs(int u, int p){ l[u] = r[u] = ++dfs_cnt; for(pair<int, int> v: graph[u]) if (v.first != p){ h[v.first] = h[u] + 1; sum[v.first] = sum[u] + v.second; dfs(v.first, u); maximize(r[u], r[v.first]); } } vector<pair<int, int>> queries[N]; ll ans[N]; void go(int u, int p, SegmentTree &st){ for(pair<int, int> i: queries[u]){ int x = edge[i.first].first, y = edge[i.first].second; if (h[x] < h[y]) swap(x, y); if (l[x] <= l[u] && r[u] <= r[x]){ // x is ancestor of u if (l[x] <= l[e] && r[e] <= r[x]){ ans[i.second] = -2; } else if (st.get(l[x], r[x]) < INF){ ans[i.second] = st.get(l[x], r[x]); } else ans[i.second] = -1; } else{ // x is not ancestor of u if (l[x] > l[e] || r[e] > r[x]){ ans[i.second] = -2; } else if (min(st.get(1, l[x] - 1), st.get(r[x] + 1, n)) < INF){ ans[i.second] = min(st.get(1, l[x] - 1), st.get(r[x] + 1, n)); } else ans[i.second] = -1; } } for(pair<int, int> v: graph[u]) if (v.first != p){ #define bede(l, r, k) st.update(l, r, k); bede(1, n, v.second); bede(l[v.first], r[v.first], -2 * v.second); go(v.first, u, st); bede(1, n, -v.second); bede(l[v.first], r[v.first], 2 * v.second); } } int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s >> q >> e; for(int i = 1; i<n; ++i){ int u, v, w; cin >> u >> v >> w; graph[u].push_back({v, w}); graph[v].push_back({u, w}); edge[i] = {u, v}; } dfs(1, 1); for(int i = 0; i<s; ++i){ int x; cin >> x; shop[x] = true; } for(int i = 0; i<q; ++i){ int j, r; cin >> j >> r; queries[r].push_back({j, i}); } SegmentTree st(n); for(int i = 1; i<=n; ++i){ st.update(l[i], l[i], sum[i]); if (!shop[i]) st.update(l[i], l[i], INF); } go(1, 1, st); for(int i = 0; i<q; ++i){ if (ans[i] == -2) cout << "escaped\n"; else if (ans[i] == -1) cout << "oo\n"; else cout << ans[i] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...