Submission #883869

#TimeUsernameProblemLanguageResultExecution timeMemory
883869fanwenPassport (JOI23_passport)C++17
48 / 100
25 ms34652 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define file(name) \ if(fopen(name".inp", "r")) \ freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout); const int MAX = 1e5 + 5; const int INF = 1e9; int n, q, dp[3][MAX << 2], p[MAX]; vector <pair <int, int>> adj[MAX << 2]; void build(int idx, int l, int r) { if(l == r) { p[l] = idx; return; } else { int mid = l + r >> 1; build(idx << 1, l, mid); build(idx << 1 | 1, mid + 1, r); adj[idx << 1].emplace_back(idx, 0); adj[idx << 1 | 1].emplace_back(idx, 0); } } void add_edge(int idx, int l, int r, int u, int v, int i) { if(l > v || r < u) return; if(l >= u && r <= v) { adj[idx].emplace_back(p[i], 1); return; } int mid = l + r >> 1; add_edge(idx << 1, l, mid, u, v, i); add_edge(idx << 1 | 1, mid + 1, r, u, v, i); } void calc(int dp[]) { priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q; for (int i = 1; i <= n; ++i) { if(dp[p[i]] < INF) { q.emplace(dp[p[i]], p[i]); } } while(!q.empty()) { auto [du, u] = q.top(); q.pop(); if(dp[u] != du) continue; for (auto [v, w] : adj[u]) if(dp[v] > dp[u] + w) { dp[v] = dp[u] + w; q.emplace(dp[v], v); } } } void you_make_it(void) { cin >> n; build(1, 1, n); for (int i = 1; i <= n; ++i) { int l, r; cin >> l >> r; add_edge(1, 1, n, l, r, i); } memset(dp, 0x3f, sizeof dp); dp[0][p[1]] = 0; calc(dp[0]); dp[1][p[n]] = 0; calc(dp[1]); for (int i = 1; i <= n; ++i) { dp[2][p[i]] = dp[0][p[i]] + dp[1][p[i]] - (1 < i && i < n); } calc(dp[2]); cin >> q; while(q--) { int x; cin >> x; cout << (dp[2][p[x]] >= INF ? -1 : dp[2][p[x]]) << '\n'; } } signed main() { #ifdef LOCAL freopen("TASK.inp", "r", stdin); freopen("TASK.out", "w", stdout); #endif file("passport"); auto start_time = chrono::steady_clock::now(); cin.tie(0), cout.tie(0) -> sync_with_stdio(0); you_make_it(); auto end_time = chrono::steady_clock::now(); cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl; return (0 ^ 0); } // Dream it. Wish it. Do it.

Compilation message (stderr)

passport.cpp: In function 'void build(int, int, int)':
passport.cpp:23:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |         int mid = l + r >> 1;
      |                   ~~^~~
passport.cpp: In function 'void add_edge(int, int, int, int, int, int)':
passport.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   38 |     int mid = l + r >> 1;
      |               ~~^~~
passport.cpp: In function 'int main()':
passport.cpp:10:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:87:5: note: in expansion of macro 'file'
   87 |     file("passport");
      |     ^~~~
passport.cpp:10:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
      |                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:87:5: note: in expansion of macro 'file'
   87 |     file("passport");
      |     ^~~~
#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...