제출 #1086387

#제출 시각아이디문제언어결과실행 시간메모리
1086387avighnaPassport (JOI23_passport)C++17
48 / 100
839 ms1048576 KiB
#include <bits/stdc++.h> typedef long long ll; const ll inf = 1e15; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); ll n; std::cin >> n; std::vector<ll> l(n + 1), r(n + 1); for (ll i = 1; i <= n; ++i) { std::cin >> l[i] >> r[i]; } std::vector min_l(n + 1, std::vector<ll>(n + 1, inf)); std::vector max_r(n + 1, std::vector<ll>(n + 1, inf)); for (ll i = 1; i <= n; ++i) { min_l[i][i] = l[i]; max_r[i][i] = r[i]; for (ll j = i + 1; j <= n; ++j) { min_l[i][j] = std::min(min_l[i][j - 1], l[j]); max_r[i][j] = std::max(max_r[i][j - 1], r[j]); } } auto f = [n](ll i, ll j) { return (j - 1) + (i - 1) * n; }; std::vector<std::vector<std::pair<ll, ll>>> adj(n * n); auto add_edge = [&](ll u, ll v, ll w) { adj[v].push_back({u, w}); }; for (ll i = 1; i <= n; ++i) { add_edge(f(i, i), f(l[i], r[i]), 1); for (ll j = i + 1; j <= n; ++j) { add_edge(f(i, j), f(i + 1, j), 0); add_edge(f(i, j), f(i, j - 1), 0); add_edge(f(i, j), f(min_l[i][j], j), 1); add_edge(f(i, j), f(i, max_r[i][j]), 1); } } std::deque<ll> dq; dq.push_back(f(1, n)); std::vector<ll> dist(n * n, inf); std::vector<bool> vis(n * n); dist[f(1, n)] = 0; while (!dq.empty()) { ll node = dq.front(); dq.pop_front(); if (vis[node]) { continue; } vis[node] = true; for (auto &[i, w] : adj[node]) { if (dist[node] + w < dist[i]) { dist[i] = dist[node] + w; if (w == 0) { dq.push_front(i); } else { dq.push_back(i); } } } } ll q; std::cin >> q; while (q--) { ll x; std::cin >> x; std::cout << (dist[f(x, x)] == inf ? -1 : dist[f(x, x)]) << '\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...