Submission #925059

# Submission time Handle Problem Language Result Execution time Memory
925059 2024-02-10T15:55:26 Z awu Passport (JOI23_passport) C++14
6 / 100
1877 ms 71364 KB
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

// #define int long long
#define ll long long
// #define double long double
#define all(x) x.begin(), x.end()
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
#define f first
#define s second
// #define endl '\n'

using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int inf = 1 << 29;
// const ll inf = 1ll << 50;

const int MOD = 1e9 + 7;

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n; cin >> n;
  int s = 1;
  while(s < n) s *= 2;
  vector<vector<int>> adj(s * 2); // reversed edges
  vector<int> s1, sn;
  for(int i = 0; i < n; i++) {
    int l, r; cin >> l >> r; l--;
    if(l == 0) s1.push_back(i);
    if(r == n) sn.push_back(i);
    auto add = [&](int a, int b) {
      adj[a].push_back(b);
    };
    for(l += s, r += s; l < r; l /= 2, r /= 2) {
      if(l & 1) add(l++, s + i);
      if(r & 1) add(--r, s + i);
    }
  }
  std::priority_queue<pii, vector<pii>, greater<pii>> pq;
  auto dijk = [&](vector<int>& d) {
    while(pq.size()) {
      auto cur = pq.top(); pq.pop();
      if(d[cur.s] <= cur.f) continue;
      d[cur.s] = cur.f;
      for(auto i : adj[cur.s]) {
        pq.push({d[cur.s] + 1, i});
      }
      if(cur.s > 1) pq.push({d[cur.s], cur.s / 2});
    }
  };
  for(auto i : s1) {
    pq.push({0, s + i});
  }
  vector<int> d1(s * 2, inf);
  dijk(d1);
  for(auto i : sn) {
    pq.push({0, s + i});
  }
  vector<int> dn(s * 2, inf);
  dijk(dn);
  vector<int> d(s * 2, inf);
  for(int i = 0; i < n; i++) {
    pq.push({d1[s + i] + dn[s + i], s + i});
  }
  dijk(d);
  int q; cin >> q;
  while(q--) {
    int x; cin >> x; x--;
    cout << (d[s + x] == inf ? -1 : d[s + x] + (x == 0 || x == n)) << endl;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1877 ms 71364 KB Output is correct
5 Correct 376 ms 33748 KB Output is correct
6 Correct 180 ms 35016 KB Output is correct
7 Correct 985 ms 60064 KB Output is correct
8 Correct 356 ms 46244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1877 ms 71364 KB Output is correct
5 Correct 376 ms 33748 KB Output is correct
6 Correct 180 ms 35016 KB Output is correct
7 Correct 985 ms 60064 KB Output is correct
8 Correct 356 ms 46244 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -