Submission #899655

# Submission time Handle Problem Language Result Execution time Memory
899655 2024-01-06T19:32:03 Z MilosMilutinovic Railway Trip 2 (JOI22_ho_t4) C++14
100 / 100
1639 ms 523848 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  int m;
  cin >> m;
  vector<vector<vector<int>>> qs(n, vector<vector<int>>(2));
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a; --b;
    if (a < b) {
      qs[a][0].push_back(b);
      qs[min(b, a + k - 1)][0].push_back(~b);
    } else {
      qs[max(b, a - k + 1)][1].push_back(b);
      qs[a][1].push_back(~b);
    }
  }
  const int LOG = 20;
  vector<vector<int>> R(n, vector<int>(LOG));
  vector<vector<int>> L(n, vector<int>(LOG));
  vector<multiset<int>> st(2);
  for (int i = 0; i < n; i++) {
    for (int t = 0; t < 2; t++) {
      for (int x : qs[i][t]) {
        if (x >= 0) {
          st[t].insert(x);
        }
      }
    }
    R[i][0] = (st[0].empty() ? i : *prev(st[0].end()));
    L[i][0] = (st[1].empty() ? i : *st[1].begin());
    for (int t = 0; t < 2; t++) {
      for (int x : qs[i][t]) {
        if (x < 0) {
          x = ~x;
          st[t].erase(st[t].find(x));
        }
      }
    }
  }
  for (int j = 1; j < LOG; j++) {
    vector<vector<int>> mn(n, vector<int>(LOG));
    vector<vector<int>> mx(n, vector<int>(LOG));
    vector<int> logs(n + 1);
    for (int i = 0; i < n; i++) {
      mn[i][0] = L[i][j - 1];
      mx[i][0] = R[i][j - 1];
    }
    for (int l = 1; l < LOG; l++) {
      for (int i = 0; i + (1 << l) <= n; i++) {
        mn[i][l] = min(mn[i][l - 1], mn[i + (1 << (l - 1))][l - 1]);
        mx[i][l] = max(mx[i][l - 1], mx[i + (1 << (l - 1))][l - 1]);
      }
    }
    for (int i = 2; i <= n; i++) {
      logs[i] = logs[i >> 1] + 1;
    }
    auto QueryMin = [&](int l, int r) {
      int k = logs[r - l + 1];
      return min(mn[l][k], mn[r - (1 << k) + 1][k]);
    };
    auto QueryMax = [&](int l, int r) {
      int k = logs[r - l + 1];
      return max(mx[l][k], mx[r - (1 << k) + 1][k]);
    };
    for (int i = 0; i < n; i++) {
      L[i][j] = QueryMin(L[i][j - 1], R[i][j - 1]);
      R[i][j] = QueryMax(L[i][j - 1], R[i][j - 1]);
    }
  }
  vector<vector<vector<int>>> mn(LOG, vector<vector<int>>(n, vector<int>(LOG)));
  vector<vector<vector<int>>> mx(LOG, vector<vector<int>>(n, vector<int>(LOG)));
  vector<int> logs(n + 1);
  for (int i = 2; i <= n; i++) {
    logs[i] = logs[i >> 1] + 1;
  }
  for (int l = 0; l < LOG; l++) {
    for (int i = 0; i < n; i++) {
      mn[l][i][0] = L[i][l];
      mx[l][i][0] = R[i][l];
    }
    for (int j = 1; j < LOG; j++) {
      for (int i = 0; i + (1 << j) <= n; i++) {
        mn[l][i][j] = min(mn[l][i][j - 1], mn[l][i + (1 << (j - 1))][j - 1]);
        mx[l][i][j] = max(mx[l][i][j - 1], mx[l][i + (1 << (j - 1))][j - 1]);
      }
    }
  }
  auto QueryMin = [&](int i, int l, int r) {
    int k = logs[r - l + 1];
    return min(mn[i][l][k], mn[i][r - (1 << k) + 1][k]);
  };
  auto QueryMax = [&](int i, int l, int r) {
    int k = logs[r - l + 1];
    return max(mx[i][l][k], mx[i][r - (1 << k) + 1][k]);
  }; 
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    --a; --b;
    if (L[a][LOG - 1] > b || R[a][LOG - 1] < b) {
      cout << -1 << '\n';
      continue;
    }
    int cl = a, cr = a;
    int res = 1;
    for (int i = LOG - 1; i >= 0; i--) {
      if (QueryMin(i, cl, cr) <= b && QueryMax(i, cl, cr) >= b) {
        continue;
      }
      res += (1 << i);
      int new_cl = QueryMin(i, cl, cr);
      int new_cr = QueryMax(i, cl, cr);
      cl = new_cl;
      cr = new_cr;
    }
    cout << res << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1880 KB Output is correct
2 Correct 2 ms 1884 KB Output is correct
3 Correct 2 ms 1804 KB Output is correct
4 Correct 2 ms 1880 KB Output is correct
5 Correct 2 ms 1880 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1884 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 2 ms 1884 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1880 KB Output is correct
2 Correct 2 ms 1884 KB Output is correct
3 Correct 2 ms 1804 KB Output is correct
4 Correct 2 ms 1880 KB Output is correct
5 Correct 2 ms 1880 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1884 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 2 ms 1884 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
13 Correct 16 ms 10812 KB Output is correct
14 Correct 14 ms 10768 KB Output is correct
15 Correct 15 ms 10724 KB Output is correct
16 Correct 13 ms 10844 KB Output is correct
17 Correct 14 ms 10780 KB Output is correct
18 Correct 13 ms 10588 KB Output is correct
19 Correct 14 ms 10104 KB Output is correct
20 Correct 13 ms 10848 KB Output is correct
21 Correct 14 ms 10632 KB Output is correct
22 Correct 14 ms 10844 KB Output is correct
23 Correct 15 ms 10844 KB Output is correct
24 Correct 13 ms 10848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1368 ms 517388 KB Output is correct
2 Correct 1307 ms 517432 KB Output is correct
3 Correct 1309 ms 518076 KB Output is correct
4 Correct 1367 ms 517332 KB Output is correct
5 Correct 1407 ms 521880 KB Output is correct
6 Correct 1330 ms 522772 KB Output is correct
7 Correct 1349 ms 523164 KB Output is correct
8 Correct 1351 ms 513872 KB Output is correct
9 Correct 1352 ms 519108 KB Output is correct
10 Correct 1428 ms 522144 KB Output is correct
11 Correct 1394 ms 522284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1413 ms 519808 KB Output is correct
2 Correct 1420 ms 522304 KB Output is correct
3 Correct 1504 ms 518996 KB Output is correct
4 Correct 1430 ms 523848 KB Output is correct
5 Correct 1535 ms 517160 KB Output is correct
6 Correct 1525 ms 522596 KB Output is correct
7 Correct 1639 ms 522280 KB Output is correct
8 Correct 1622 ms 522668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1497 ms 520956 KB Output is correct
2 Correct 1539 ms 519016 KB Output is correct
3 Correct 1620 ms 518476 KB Output is correct
4 Correct 1487 ms 516416 KB Output is correct
5 Correct 1396 ms 514916 KB Output is correct
6 Correct 1378 ms 514528 KB Output is correct
7 Correct 1392 ms 519176 KB Output is correct
8 Correct 2 ms 1880 KB Output is correct
9 Correct 13 ms 10588 KB Output is correct
10 Correct 1475 ms 520312 KB Output is correct
11 Correct 1488 ms 521000 KB Output is correct
12 Correct 1491 ms 515608 KB Output is correct
13 Correct 1450 ms 521028 KB Output is correct
14 Correct 2 ms 1884 KB Output is correct
15 Correct 14 ms 10588 KB Output is correct
16 Correct 1423 ms 520232 KB Output is correct
17 Correct 1553 ms 521268 KB Output is correct
18 Correct 1560 ms 521136 KB Output is correct
19 Correct 1477 ms 521064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1880 KB Output is correct
2 Correct 2 ms 1884 KB Output is correct
3 Correct 2 ms 1804 KB Output is correct
4 Correct 2 ms 1880 KB Output is correct
5 Correct 2 ms 1880 KB Output is correct
6 Correct 2 ms 1884 KB Output is correct
7 Correct 2 ms 1884 KB Output is correct
8 Correct 2 ms 1884 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 2 ms 1884 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
13 Correct 16 ms 10812 KB Output is correct
14 Correct 14 ms 10768 KB Output is correct
15 Correct 15 ms 10724 KB Output is correct
16 Correct 13 ms 10844 KB Output is correct
17 Correct 14 ms 10780 KB Output is correct
18 Correct 13 ms 10588 KB Output is correct
19 Correct 14 ms 10104 KB Output is correct
20 Correct 13 ms 10848 KB Output is correct
21 Correct 14 ms 10632 KB Output is correct
22 Correct 14 ms 10844 KB Output is correct
23 Correct 15 ms 10844 KB Output is correct
24 Correct 13 ms 10848 KB Output is correct
25 Correct 1368 ms 517388 KB Output is correct
26 Correct 1307 ms 517432 KB Output is correct
27 Correct 1309 ms 518076 KB Output is correct
28 Correct 1367 ms 517332 KB Output is correct
29 Correct 1407 ms 521880 KB Output is correct
30 Correct 1330 ms 522772 KB Output is correct
31 Correct 1349 ms 523164 KB Output is correct
32 Correct 1351 ms 513872 KB Output is correct
33 Correct 1352 ms 519108 KB Output is correct
34 Correct 1428 ms 522144 KB Output is correct
35 Correct 1394 ms 522284 KB Output is correct
36 Correct 1413 ms 519808 KB Output is correct
37 Correct 1420 ms 522304 KB Output is correct
38 Correct 1504 ms 518996 KB Output is correct
39 Correct 1430 ms 523848 KB Output is correct
40 Correct 1535 ms 517160 KB Output is correct
41 Correct 1525 ms 522596 KB Output is correct
42 Correct 1639 ms 522280 KB Output is correct
43 Correct 1622 ms 522668 KB Output is correct
44 Correct 1497 ms 520956 KB Output is correct
45 Correct 1539 ms 519016 KB Output is correct
46 Correct 1620 ms 518476 KB Output is correct
47 Correct 1487 ms 516416 KB Output is correct
48 Correct 1396 ms 514916 KB Output is correct
49 Correct 1378 ms 514528 KB Output is correct
50 Correct 1392 ms 519176 KB Output is correct
51 Correct 2 ms 1880 KB Output is correct
52 Correct 13 ms 10588 KB Output is correct
53 Correct 1475 ms 520312 KB Output is correct
54 Correct 1488 ms 521000 KB Output is correct
55 Correct 1491 ms 515608 KB Output is correct
56 Correct 1450 ms 521028 KB Output is correct
57 Correct 2 ms 1884 KB Output is correct
58 Correct 14 ms 10588 KB Output is correct
59 Correct 1423 ms 520232 KB Output is correct
60 Correct 1553 ms 521268 KB Output is correct
61 Correct 1560 ms 521136 KB Output is correct
62 Correct 1477 ms 521064 KB Output is correct
63 Correct 1510 ms 517944 KB Output is correct
64 Correct 1557 ms 518712 KB Output is correct
65 Correct 1496 ms 517788 KB Output is correct
66 Correct 1483 ms 522324 KB Output is correct
67 Correct 1583 ms 523484 KB Output is correct
68 Correct 1418 ms 516848 KB Output is correct
69 Correct 1505 ms 522388 KB Output is correct
70 Correct 1452 ms 522424 KB Output is correct
71 Correct 1599 ms 522560 KB Output is correct