Submission #1038764

# Submission time Handle Problem Language Result Execution time Memory
1038764 2024-07-30T07:20:02 Z juicy Passport (JOI23_passport) C++17
100 / 100
492 ms 81920 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5, inf = 1e9 + 7;

int n, q;
int res[4 * N], pos[N], d[4 * N], vis[4 * N];
vector<array<int, 2>> g[4 * N];

void bfs(int src) {
  memset(d, 0x3f, sizeof(d));
  deque<int> q;
  d[src] = 0;
  q.push_back(src);
  while (q.size()) {
    int u = q.front(); q.pop_front();
    if (vis[u] == src) {
      continue;
    }
    vis[u] = src;
    for (auto [v, w] : g[u]) {
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        if (w) {
          q.push_back(v);
        } else {
          q.push_front(v);
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (res[pos[i]] != inf && d[pos[i]] < inf) {
      res[pos[i]] += d[pos[i]];
    } else {
      res[pos[i]] = inf;
    }
  }
}

void add(int u, int v, int w) {
  g[v].push_back({u, w});
}

void bld(int id = 1, int l = 1, int r = n) {
  if (l == r) {
    pos[l] = id;
    return;
  }
  res[id] = inf;
  int md = (l + r) / 2;
  bld(id * 2, l, md);
  bld(id * 2 + 1, md + 1, r);
  add(id, id * 2, 0);
  add(id, id * 2 + 1, 0);
}

void upd(int u, int v, int x, int id = 1, int l = 1, int r = n) {
  if (u <= l && r <= v) {
    add(x, id, 1);
    return;
  }
  int md = (l + r) / 2;
  if (u <= md) {
    upd(u, v, x, id * 2, l, md);
  }
  if (md < v) {
    upd(u, v, x, id * 2 + 1, md + 1, r);
  }
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n;
  bld();
  for (int i = 1; i <= n; ++i) {
    int l, r; cin >> l >> r;
    upd(l, r, pos[i]);
  }
  bfs(pos[1]);
  bfs(pos[n]);
  using T = array<int, 2>;
  priority_queue<T, vector<T>, greater<T>> pq;
  for (int i = 1; i <= n; ++i) {
    if (res[pos[i]] != inf) {
      if (i != 1 && i != n) {
        res[pos[i]] -= 1;
      }
      pq.push({res[pos[i]], pos[i]});
    }
  }
  while (pq.size()) {
    auto [c, u] = pq.top(); pq.pop();
    if (c != res[u]) {
      continue;
    }
    for (auto [v, w] : g[u]) {
      if (res[v] > res[u] + w) {
        pq.push({res[v] = res[u] + w, v});
      }
    }
  }
  cin >> q;
  while (q--) {
    int u; cin >> u;
    cout << (res[pos[u]] == inf ? -1 : res[pos[u]]) << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 22360 KB Output is correct
2 Correct 9 ms 22360 KB Output is correct
3 Correct 12 ms 22364 KB Output is correct
4 Correct 492 ms 72700 KB Output is correct
5 Correct 251 ms 52704 KB Output is correct
6 Correct 189 ms 47560 KB Output is correct
7 Correct 136 ms 45764 KB Output is correct
8 Correct 98 ms 50696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22364 KB Output is correct
2 Correct 13 ms 22364 KB Output is correct
3 Correct 12 ms 22364 KB Output is correct
4 Correct 9 ms 22364 KB Output is correct
5 Correct 14 ms 22400 KB Output is correct
6 Correct 11 ms 22364 KB Output is correct
7 Correct 13 ms 22360 KB Output is correct
8 Correct 11 ms 22364 KB Output is correct
9 Correct 9 ms 22364 KB Output is correct
10 Correct 13 ms 22364 KB Output is correct
11 Correct 10 ms 22364 KB Output is correct
12 Correct 9 ms 22436 KB Output is correct
13 Correct 10 ms 22364 KB Output is correct
14 Correct 9 ms 22460 KB Output is correct
15 Correct 12 ms 22364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22364 KB Output is correct
2 Correct 13 ms 22364 KB Output is correct
3 Correct 12 ms 22364 KB Output is correct
4 Correct 9 ms 22364 KB Output is correct
5 Correct 14 ms 22400 KB Output is correct
6 Correct 11 ms 22364 KB Output is correct
7 Correct 13 ms 22360 KB Output is correct
8 Correct 11 ms 22364 KB Output is correct
9 Correct 9 ms 22364 KB Output is correct
10 Correct 13 ms 22364 KB Output is correct
11 Correct 10 ms 22364 KB Output is correct
12 Correct 9 ms 22436 KB Output is correct
13 Correct 10 ms 22364 KB Output is correct
14 Correct 9 ms 22460 KB Output is correct
15 Correct 12 ms 22364 KB Output is correct
16 Correct 12 ms 22848 KB Output is correct
17 Correct 11 ms 22620 KB Output is correct
18 Correct 13 ms 22848 KB Output is correct
19 Correct 12 ms 22864 KB Output is correct
20 Correct 10 ms 22620 KB Output is correct
21 Correct 11 ms 22736 KB Output is correct
22 Correct 10 ms 22620 KB Output is correct
23 Correct 11 ms 22848 KB Output is correct
24 Correct 11 ms 23128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 22364 KB Output is correct
2 Correct 13 ms 22364 KB Output is correct
3 Correct 12 ms 22364 KB Output is correct
4 Correct 9 ms 22364 KB Output is correct
5 Correct 14 ms 22400 KB Output is correct
6 Correct 11 ms 22364 KB Output is correct
7 Correct 13 ms 22360 KB Output is correct
8 Correct 11 ms 22364 KB Output is correct
9 Correct 9 ms 22364 KB Output is correct
10 Correct 13 ms 22364 KB Output is correct
11 Correct 10 ms 22364 KB Output is correct
12 Correct 9 ms 22436 KB Output is correct
13 Correct 10 ms 22364 KB Output is correct
14 Correct 9 ms 22460 KB Output is correct
15 Correct 12 ms 22364 KB Output is correct
16 Correct 12 ms 22848 KB Output is correct
17 Correct 11 ms 22620 KB Output is correct
18 Correct 13 ms 22848 KB Output is correct
19 Correct 12 ms 22864 KB Output is correct
20 Correct 10 ms 22620 KB Output is correct
21 Correct 11 ms 22736 KB Output is correct
22 Correct 10 ms 22620 KB Output is correct
23 Correct 11 ms 22848 KB Output is correct
24 Correct 11 ms 23128 KB Output is correct
25 Correct 10 ms 22364 KB Output is correct
26 Correct 10 ms 22364 KB Output is correct
27 Correct 13 ms 22884 KB Output is correct
28 Correct 13 ms 22832 KB Output is correct
29 Correct 12 ms 22616 KB Output is correct
30 Correct 11 ms 22620 KB Output is correct
31 Correct 11 ms 22616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 22360 KB Output is correct
2 Correct 9 ms 22360 KB Output is correct
3 Correct 12 ms 22364 KB Output is correct
4 Correct 492 ms 72700 KB Output is correct
5 Correct 251 ms 52704 KB Output is correct
6 Correct 189 ms 47560 KB Output is correct
7 Correct 136 ms 45764 KB Output is correct
8 Correct 98 ms 50696 KB Output is correct
9 Correct 10 ms 22364 KB Output is correct
10 Correct 13 ms 22364 KB Output is correct
11 Correct 12 ms 22364 KB Output is correct
12 Correct 9 ms 22364 KB Output is correct
13 Correct 14 ms 22400 KB Output is correct
14 Correct 11 ms 22364 KB Output is correct
15 Correct 13 ms 22360 KB Output is correct
16 Correct 11 ms 22364 KB Output is correct
17 Correct 9 ms 22364 KB Output is correct
18 Correct 13 ms 22364 KB Output is correct
19 Correct 10 ms 22364 KB Output is correct
20 Correct 9 ms 22436 KB Output is correct
21 Correct 10 ms 22364 KB Output is correct
22 Correct 9 ms 22460 KB Output is correct
23 Correct 12 ms 22364 KB Output is correct
24 Correct 12 ms 22848 KB Output is correct
25 Correct 11 ms 22620 KB Output is correct
26 Correct 13 ms 22848 KB Output is correct
27 Correct 12 ms 22864 KB Output is correct
28 Correct 10 ms 22620 KB Output is correct
29 Correct 11 ms 22736 KB Output is correct
30 Correct 10 ms 22620 KB Output is correct
31 Correct 11 ms 22848 KB Output is correct
32 Correct 11 ms 23128 KB Output is correct
33 Correct 10 ms 22364 KB Output is correct
34 Correct 10 ms 22364 KB Output is correct
35 Correct 13 ms 22884 KB Output is correct
36 Correct 13 ms 22832 KB Output is correct
37 Correct 12 ms 22616 KB Output is correct
38 Correct 11 ms 22620 KB Output is correct
39 Correct 11 ms 22616 KB Output is correct
40 Correct 465 ms 75148 KB Output is correct
41 Correct 238 ms 54832 KB Output is correct
42 Correct 303 ms 81920 KB Output is correct
43 Correct 295 ms 81432 KB Output is correct
44 Correct 146 ms 49600 KB Output is correct
45 Correct 173 ms 56120 KB Output is correct
46 Correct 73 ms 34572 KB Output is correct
47 Correct 216 ms 57532 KB Output is correct
48 Correct 171 ms 62904 KB Output is correct