Submission #946697

#TimeUsernameProblemLanguageResultExecution timeMemory
946697vladiliusPassport (JOI23_passport)C++17
6 / 100
693 ms77040 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int inf = 1e9; struct ST{ vector<vector<pii>> t; vector<int> idx; int n; void build(int v, int tl, int tr){ if (tl == tr){ idx[tl] = v; return; } int tm = (tl + tr) / 2, vv = 2 * v; build(vv, tl, tm); build(vv + 1, tm + 1, tr); t[vv].push_back({v, 0}); t[vv + 1].push_back({v, 0}); } ST(int ns){ n = ns; t.resize(4 * n); idx.resize(n + 1); build(1, 1, n); } void add(int v, int tl, int tr, int& l, int& r, int& i){ if (l > tr || r < tl) return; if (l <= tl && tr <= r){ if (i != v){ t[v].push_back({i, 1}); } return; } int tm = (tl + tr) / 2, vv = 2 * v; add(vv, tl, tm, l, r, i); add(vv + 1, tm + 1, tr, l, r, i); } void add(int p, int l, int r){ add(1, 1, n, l, r, idx[p]); } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; ST T(n); for (int i = 1; i <= n; i++){ int l, r; cin>>l>>r; T.add(i, l, r); } vector<int> out(4 * n); auto dijkstra = [&](int v){ priority_queue<pii, vector<pii>, greater<pii>> pq; pq.push({0, v}); vector<int> dist(4 * n, inf); dist[v] = 0; while (!pq.empty()){ auto [d, k] = pq.top(); pq.pop(); for (auto [i, w]: T.t[k]){ if (dist[i] > dist[k] + w){ dist[i] = dist[k] + w; pq.push({dist[i], i}); } } } for (int i = 1; i < 4 * n; i++){ out[i] += dist[i]; } }; dijkstra(T.idx[1]); dijkstra(T.idx[n]); for (int i = 1; i < 4 * n; i++){ out[i] = min(out[i], inf); } priority_queue<pii, vector<pii>, greater<pii>> pq; for (int i = 1; i < 4 * n; i++){ if (out[i] != inf){ pq.push({out[i], i}); } } while (!pq.empty()){ auto [d, v] = pq.top(); pq.pop(); for (auto [i, w]: T.t[v]){ if (out[i] > out[v] + w){ out[i] = out[v] + w; pq.push({out[i], i}); } } } int q; cin>>q; while (q--){ int x; cin>>x; int ans = out[T.idx[x]]; if (ans == inf){ cout<<-1<<"\n"; continue; } cout<<ans<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...