Submission #736118

#TimeUsernameProblemLanguageResultExecution timeMemory
736118nguyentunglamPassport (JOI23_passport)C++17
100 / 100
862 ms85324 KiB
#include<bits/stdc++.h> #define fi first #define se second #define endl "\n" #define ii pair<int, int> using namespace std; const int N = 8e5 + 10; int f[N], g[N], h[N], p[N]; bool mark[N]; vector<ii> adj[N]; priority_queue<ii, vector<ii>, greater<ii> > q; void calc(int start, int d[]) { d[start] = 0; q.push({0, start}); while (!q.empty()) { int cost, u; tie(cost, u) = q.top(); q.pop(); if (d[u] != cost) continue; for(auto &[v, w] : adj[u]) if (d[v] > d[u] + w) d[v] = d[u] + w, q.push({d[v], v}); } } void init(int s, int l, int r) { if (l == r) { mark[s] = 1; p[l] = s; return; } adj[s << 1].emplace_back(s, 0); adj[s << 1 | 1].emplace_back(s, 0); int mid = l + r >> 1; init(s << 1, l, mid); init(s << 1 | 1, mid + 1, r); } void add(int s, int l, int r, int from, int to, int idx) { if (l > to || r < from) return; if (from <= l && r <= to) { adj[s].emplace_back(p[idx], 1); return; } int mid = l + r >> 1; add(s << 1, l, mid, from, to, idx); add(s << 1 | 1, mid + 1, r, from, to, idx); } int main() { #define task "" cin.tie(0) -> sync_with_stdio(0); if (fopen ("task.inp", "r")) { freopen ("task.inp", "r", stdin); freopen ("task.out", "w", stdout); } if (fopen (task".inp", "r")) { freopen (task".inp", "r", stdin); freopen (task".out", "w", stdout); } int n; cin >> n; init(1, 1, n); for(int i = 1; i <= n; i++) { int l, r; cin >> l >> r; add(1, 1, n, l, r, i); } for(int i = 1; i <= 4 * n; i++) f[i] = g[i] = 1e9; calc(p[1], f); calc(p[n], g); for(int i = 1; i <= 4 * n; i++) { if (i == p[1] || i == p[n]) h[i] = f[i] + g[i]; else h[i] = f[i] + g[i] - mark[i]; q.push({h[i], i}); } while (!q.empty()) { int cost, u; tie(cost, u) = q.top(); q.pop(); if (cost != h[u]) continue; for(auto &[v, w] : adj[u]) if (h[v] > h[u] + w) { h[v] = h[u] + w; q.push({h[v], v}); } } for(int i = 1; i <= 4 * n; i++) if (h[i] > 1e8) h[i] = -1; int q; cin >> q; while (q--) { int x; cin >> x; cout << h[p[x]] << endl; } }

Compilation message (stderr)

passport.cpp: In function 'void init(int, int, int)':
passport.cpp:30:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   30 |     int mid = l + r >> 1;
      |               ~~^~~
passport.cpp: In function 'void add(int, int, int, int, int, int)':
passport.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   41 |     int mid = l + r >> 1;
      |               ~~^~~
passport.cpp: In function 'int main()':
passport.cpp:50:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:51:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:54:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
passport.cpp:55:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...