Submission #923884

#TimeUsernameProblemLanguageResultExecution timeMemory
923884awuPassport (JOI23_passport)C++14
48 / 100
1129 ms386588 KiB
#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;
  vector<int> l(n), r(n);
  for(int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    l[i]--;
  }
  if(n > 2500) {
    vector<int> pref(n + 1);
    for(int i = 0; i < n; i++) {
      pref[i + 1] = max(pref[i], r[i]);
    }
    int ans = 1;
    int cur = 1;
    while(true) {
      if(cur == n) {
        cout << ans << endl;
        return 0;
      }
      int nxt = pref[cur];
      if(nxt <= cur) {
        cout << -1 << endl;
        return 0;
      }
      cur = nxt;
      ans++;
    }
  }
  vector<vector<vector<pii>>> adj(n, vector<vector<pii>>(n + 1));
  for(int i = 0; i < n; i++) {
    for(int j = i + 1; j <= n; j++) {
      if(i + 1 < j) {
        adj[i + 1][j].push_back({i, j});
        adj[i][j - 1].push_back({i, j});
      }
      adj[min(i, l[i])][max(j, r[i])].push_back({i, j});
      adj[min(i, l[j - 1])][max(j, r[j - 1])].push_back({i, j});
    }
  }
  vector<vector<int>> dist(n, vector<int>(n + 1, inf));
  deque<pair<int, pii>> pq;
  pq.push_back({0, {0, n}});
  while(pq.size()) {
    auto cur = pq.front(); pq.pop_front();
    int d = cur.f, i = cur.s.f, j = cur.s.s;
    if(dist[i][j] <= d) continue;
    dist[i][j] = d;
    for(auto nxt : adj[i][j]) {
      if(nxt.s - nxt.f < j - i) {
        pq.push_back({d + 1, nxt});
      } else {
        pq.push_front({d, nxt});
      }
    }
  }
  int q; cin >> q;
  while(q--) {
    int x; cin >> x; x--;
    int ans = dist[x][x + 1];
    if(ans == inf) ans = -1;
    cout << ans << endl;
  }
}
#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...