Submission #857833

# Submission time Handle Problem Language Result Execution time Memory
857833 2023-10-07T04:27:12 Z NeroZein Event Hopping (BOI22_events) C++17
100 / 100
116 ms 16488 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int LOG = 18;
const int N = 1e5 + 5;

int l[N], r[N];
int nx[LOG][N]; 

template<typename T, class F = function<T(const T&, const T&)>>
struct SparseTable {
  int n;
  F func;
  vector<vector<T>> mat; 
  SparseTable(const vector<int>& a, F f) : func(f) {
    n = (int) a.size();
    int max_log = 32 - __builtin_clz(n); 
    mat.resize(max_log); 
    mat[0] = a; 
    for (int j = 1; j < max_log; ++j) {
      mat[j].resize(n - (1 << j) + 1);
      for (int i = 0; i <= n - (1 << j); ++i) {
        mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]); 
      }
    }
  }
  T query(int from, int to) {
    assert(0 <= from && from <= to && to <= n - 1); 
    int lg = 31 - __builtin_clz(to - from + 1);
    return func(mat[lg][from], mat[lg][to - (1 << lg) + 1]); 
  }
};

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> l[i] >> r[i];
  }
  vector<int> id(n);
  iota(id.begin(), id.end(), 1); 
  sort(id.begin(), id.end(), [&](int i, int j) {
    return r[i] < r[j]; 
  });
  SparseTable<int> spa(id, [&](int i, int j) {
    return l[i] < l[j] ? i : j;
  }); 
  for (int i = 1; i <= n; ++i) {
    int lo = 0, hi = n;
    while (lo < hi) {
      int mid = (lo + hi) / 2;
      if (r[id[mid]] >= l[i]) {
        hi = mid;
      } else {
        lo = mid + 1; 
      }
    }
    int lo2 = -1, hi2 = n - 1;
    while (lo2 < hi2) {
      int mid = (lo2 + hi2 + 1) / 2;
      if (r[id[mid]] <= r[i]) {
        lo2 = mid;
      } else {
        hi2 = mid - 1; 
      }
    }
    if (lo2 == -1 || lo == n) {
      continue;
    }
    int tnx = spa.query(lo, lo2); 
    nx[0][i] = (tnx == i ? 0 : tnx); 
  }
  for (int j = 1; j < LOG; ++j) {
    for (int i = 1; i <= n; ++i) {
      nx[j][i] = nx[j - 1][nx[j - 1][i]]; 
    }
  }
  while (q--) {
    int st, en;
    cin >> st >> en; 
    if (st == en) {
      cout << 0 << '\n';
      continue;
    }
    if (r[en] >= r[st] && l[en] <= r[st]) {
      cout << 1 << '\n';
      continue;
    } 
    int ans = 0;
    for (int j = LOG - 1; j >= 0; --j) {
      if (l[nx[j][en]] > r[st]) {
        en = nx[j][en];
        ans += 1 << j; 
      }
    }
    if (l[en] > r[st] && nx[0][en]) {
      cout << ans + 2 << '\n';
    } else {
      cout << "impossible" << '\n';
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 85 ms 15188 KB Output is correct
3 Correct 92 ms 15388 KB Output is correct
4 Correct 107 ms 15444 KB Output is correct
5 Correct 100 ms 15396 KB Output is correct
6 Correct 94 ms 15516 KB Output is correct
7 Correct 95 ms 15528 KB Output is correct
8 Correct 82 ms 15700 KB Output is correct
9 Correct 84 ms 15956 KB Output is correct
10 Correct 99 ms 15552 KB Output is correct
11 Correct 93 ms 15700 KB Output is correct
12 Correct 51 ms 15696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 2 ms 6644 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 2 ms 6492 KB Output is correct
16 Correct 2 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 26 ms 7508 KB Output is correct
20 Correct 23 ms 7368 KB Output is correct
21 Correct 28 ms 7504 KB Output is correct
22 Correct 22 ms 7744 KB Output is correct
23 Correct 21 ms 7516 KB Output is correct
24 Correct 20 ms 7452 KB Output is correct
25 Correct 22 ms 7164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6488 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6744 KB Output is correct
16 Correct 1 ms 6648 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 69 ms 16488 KB Output is correct
20 Correct 61 ms 14672 KB Output is correct
21 Correct 65 ms 14672 KB Output is correct
22 Correct 68 ms 14748 KB Output is correct
23 Correct 54 ms 14772 KB Output is correct
24 Correct 72 ms 14680 KB Output is correct
25 Correct 25 ms 14676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 15392 KB Output is correct
2 Correct 92 ms 15392 KB Output is correct
3 Correct 99 ms 15188 KB Output is correct
4 Correct 83 ms 15716 KB Output is correct
5 Correct 116 ms 15748 KB Output is correct
6 Correct 105 ms 15468 KB Output is correct
7 Correct 97 ms 15444 KB Output is correct
8 Correct 94 ms 15644 KB Output is correct
9 Correct 55 ms 14712 KB Output is correct
10 Correct 91 ms 15184 KB Output is correct
11 Correct 85 ms 14928 KB Output is correct
12 Correct 92 ms 15140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 85 ms 15188 KB Output is correct
3 Correct 92 ms 15388 KB Output is correct
4 Correct 107 ms 15444 KB Output is correct
5 Correct 100 ms 15396 KB Output is correct
6 Correct 94 ms 15516 KB Output is correct
7 Correct 95 ms 15528 KB Output is correct
8 Correct 82 ms 15700 KB Output is correct
9 Correct 84 ms 15956 KB Output is correct
10 Correct 99 ms 15552 KB Output is correct
11 Correct 93 ms 15700 KB Output is correct
12 Correct 51 ms 15696 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6488 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 2 ms 6644 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 2 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 26 ms 7508 KB Output is correct
32 Correct 23 ms 7368 KB Output is correct
33 Correct 28 ms 7504 KB Output is correct
34 Correct 22 ms 7744 KB Output is correct
35 Correct 21 ms 7516 KB Output is correct
36 Correct 20 ms 7452 KB Output is correct
37 Correct 22 ms 7164 KB Output is correct
38 Correct 1 ms 6488 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6744 KB Output is correct
44 Correct 1 ms 6648 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 69 ms 16488 KB Output is correct
48 Correct 61 ms 14672 KB Output is correct
49 Correct 65 ms 14672 KB Output is correct
50 Correct 68 ms 14748 KB Output is correct
51 Correct 54 ms 14772 KB Output is correct
52 Correct 72 ms 14680 KB Output is correct
53 Correct 25 ms 14676 KB Output is correct
54 Correct 87 ms 15392 KB Output is correct
55 Correct 92 ms 15392 KB Output is correct
56 Correct 99 ms 15188 KB Output is correct
57 Correct 83 ms 15716 KB Output is correct
58 Correct 116 ms 15748 KB Output is correct
59 Correct 105 ms 15468 KB Output is correct
60 Correct 97 ms 15444 KB Output is correct
61 Correct 94 ms 15644 KB Output is correct
62 Correct 55 ms 14712 KB Output is correct
63 Correct 91 ms 15184 KB Output is correct
64 Correct 85 ms 14928 KB Output is correct
65 Correct 92 ms 15140 KB Output is correct
66 Correct 1 ms 6744 KB Output is correct
67 Correct 88 ms 15328 KB Output is correct
68 Correct 94 ms 15396 KB Output is correct
69 Correct 115 ms 15208 KB Output is correct
70 Correct 96 ms 15420 KB Output is correct
71 Correct 93 ms 15444 KB Output is correct
72 Correct 99 ms 15532 KB Output is correct
73 Correct 85 ms 16164 KB Output is correct
74 Correct 86 ms 15700 KB Output is correct
75 Correct 100 ms 15700 KB Output is correct
76 Correct 92 ms 15840 KB Output is correct
77 Correct 51 ms 15696 KB Output is correct
78 Correct 1 ms 6488 KB Output is correct
79 Correct 1 ms 6492 KB Output is correct
80 Correct 1 ms 6492 KB Output is correct
81 Correct 1 ms 6492 KB Output is correct
82 Correct 1 ms 6492 KB Output is correct
83 Correct 1 ms 6492 KB Output is correct
84 Correct 1 ms 6492 KB Output is correct
85 Correct 1 ms 6492 KB Output is correct
86 Correct 26 ms 7260 KB Output is correct
87 Correct 23 ms 7416 KB Output is correct
88 Correct 28 ms 7516 KB Output is correct
89 Correct 28 ms 7616 KB Output is correct
90 Correct 21 ms 7448 KB Output is correct
91 Correct 20 ms 7512 KB Output is correct
92 Correct 22 ms 7164 KB Output is correct
93 Correct 64 ms 14716 KB Output is correct
94 Correct 62 ms 14676 KB Output is correct
95 Correct 69 ms 14628 KB Output is correct
96 Correct 71 ms 14672 KB Output is correct
97 Correct 54 ms 14768 KB Output is correct
98 Correct 78 ms 14600 KB Output is correct
99 Correct 25 ms 14684 KB Output is correct
100 Correct 104 ms 15392 KB Output is correct
101 Correct 96 ms 15444 KB Output is correct
102 Correct 100 ms 15632 KB Output is correct
103 Correct 92 ms 15184 KB Output is correct
104 Correct 87 ms 14932 KB Output is correct
105 Correct 95 ms 15032 KB Output is correct
106 Correct 87 ms 15440 KB Output is correct
107 Correct 99 ms 15440 KB Output is correct
108 Correct 95 ms 15612 KB Output is correct
109 Correct 93 ms 15444 KB Output is correct
110 Correct 85 ms 15252 KB Output is correct
111 Correct 83 ms 15444 KB Output is correct