#include <bits/stdc++.h>
using ll = long long;
using std::vector;
using std::array;
using std::tuple;
using std::pair;
template <class F> struct fixed : private F {
explicit fixed(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <class T>
constexpr T infty = std::numeric_limits<T>::max() / 2;
constexpr pair<int, int> unit = {infty<int>, infty<int>};
class segtree {
int size;
vector<pair<int, int>> data;
public:
explicit segtree(const int n) : size(n), data(2 * n, unit) {}
void assign(int i, const pair<int, int>& x) {
i += size;
data[i] = x;
while (i > 1) {
i >>= 1;
data[i] = std::min(data[2 * i], data[2 * i + 1]);
}
}
pair<int, int> fold(int l, int r) {
l += size;
r += size;
auto ret = unit;
while (l < r) {
if (l & 1) ret = std::min(ret, data[l++]);
if (r & 1) ret = std::min(ret, data[--r]);
l >>= 1;
r >>= 1;
}
return ret;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, Q;
std::cin >> N >> Q;
vector<int> S(N), E(N);
for (int i = 0; i < N; ++i) {
std::cin >> S[i] >> E[i];
}
vector<pair<int, int>> vec(N);
for (int i = 0; i < N; ++i) {
vec[i] = {E[i], i};
}
std::sort(vec.begin(), vec.end());
segtree seg(N);
for (int i = 0; i < N; ++i) {
const int k = vec[i].second;
seg.assign(i, pair(S[k], k));
}
constexpr int LOG = 18;
array<vector<int>, LOG> parent = {};
parent[0].resize(N);
for (int i = 0; i < N; ++i) {
const int l = std::lower_bound(vec.begin(), vec.end(), pair(S[i], 0)) - vec.begin();
const int r = std::lower_bound(vec.begin(), vec.end(), pair(E[i] + 1, 0)) - vec.begin();
parent[0][i] = seg.fold(l, r).second;
}
for (int i = 0; i < LOG - 1; ++i) {
parent[i + 1].resize(N);
for (int j = 0; j < N; ++j) {
parent[i + 1][j] = parent[i][parent[i][j]];
}
}
while (Q--) {
int s, e;
std::cin >> s >> e;
s -= 1, e -= 1;
const int root = parent[LOG - 1][e];
if (s == e) {
std::cout << 0 << '\n';
continue;
}
if (E[e] < E[s] or E[s] < S[root]) {
std::cout << "impossible\n";
continue;
}
if (S[e] <= E[s]) {
std::cout << 1 << '\n';
continue;
}
int ans = 0;
for (int k = LOG - 1; k >= 0; --k) {
if (E[s] < S[parent[k][e]]) {
e = parent[k][e];
ans |= 1 << k;
}
}
std::cout << ans + 2 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
87 ms |
11092 KB |
Output is correct |
3 |
Correct |
98 ms |
11008 KB |
Output is correct |
4 |
Correct |
163 ms |
11108 KB |
Output is correct |
5 |
Correct |
103 ms |
11132 KB |
Output is correct |
6 |
Correct |
100 ms |
11076 KB |
Output is correct |
7 |
Correct |
120 ms |
11120 KB |
Output is correct |
8 |
Correct |
82 ms |
11524 KB |
Output is correct |
9 |
Correct |
89 ms |
11616 KB |
Output is correct |
10 |
Correct |
104 ms |
11480 KB |
Output is correct |
11 |
Correct |
110 ms |
11516 KB |
Output is correct |
12 |
Correct |
55 ms |
11604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
33 ms |
1236 KB |
Output is correct |
20 |
Correct |
42 ms |
1204 KB |
Output is correct |
21 |
Correct |
33 ms |
1524 KB |
Output is correct |
22 |
Correct |
32 ms |
1644 KB |
Output is correct |
23 |
Correct |
31 ms |
1464 KB |
Output is correct |
24 |
Correct |
31 ms |
1336 KB |
Output is correct |
25 |
Correct |
30 ms |
1032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
61 ms |
10448 KB |
Output is correct |
20 |
Correct |
70 ms |
10492 KB |
Output is correct |
21 |
Correct |
70 ms |
10464 KB |
Output is correct |
22 |
Correct |
63 ms |
10508 KB |
Output is correct |
23 |
Correct |
69 ms |
10500 KB |
Output is correct |
24 |
Correct |
74 ms |
10448 KB |
Output is correct |
25 |
Correct |
43 ms |
10500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
11004 KB |
Output is correct |
2 |
Correct |
105 ms |
11096 KB |
Output is correct |
3 |
Correct |
149 ms |
11080 KB |
Output is correct |
4 |
Correct |
89 ms |
11608 KB |
Output is correct |
5 |
Correct |
124 ms |
11476 KB |
Output is correct |
6 |
Correct |
121 ms |
11252 KB |
Output is correct |
7 |
Correct |
161 ms |
11272 KB |
Output is correct |
8 |
Correct |
126 ms |
11364 KB |
Output is correct |
9 |
Correct |
58 ms |
10496 KB |
Output is correct |
10 |
Correct |
121 ms |
10808 KB |
Output is correct |
11 |
Correct |
116 ms |
10652 KB |
Output is correct |
12 |
Correct |
112 ms |
10880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
87 ms |
11092 KB |
Output is correct |
3 |
Correct |
98 ms |
11008 KB |
Output is correct |
4 |
Correct |
163 ms |
11108 KB |
Output is correct |
5 |
Correct |
103 ms |
11132 KB |
Output is correct |
6 |
Correct |
100 ms |
11076 KB |
Output is correct |
7 |
Correct |
120 ms |
11120 KB |
Output is correct |
8 |
Correct |
82 ms |
11524 KB |
Output is correct |
9 |
Correct |
89 ms |
11616 KB |
Output is correct |
10 |
Correct |
104 ms |
11480 KB |
Output is correct |
11 |
Correct |
110 ms |
11516 KB |
Output is correct |
12 |
Correct |
55 ms |
11604 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
33 ms |
1236 KB |
Output is correct |
32 |
Correct |
42 ms |
1204 KB |
Output is correct |
33 |
Correct |
33 ms |
1524 KB |
Output is correct |
34 |
Correct |
32 ms |
1644 KB |
Output is correct |
35 |
Correct |
31 ms |
1464 KB |
Output is correct |
36 |
Correct |
31 ms |
1336 KB |
Output is correct |
37 |
Correct |
30 ms |
1032 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
61 ms |
10448 KB |
Output is correct |
48 |
Correct |
70 ms |
10492 KB |
Output is correct |
49 |
Correct |
70 ms |
10464 KB |
Output is correct |
50 |
Correct |
63 ms |
10508 KB |
Output is correct |
51 |
Correct |
69 ms |
10500 KB |
Output is correct |
52 |
Correct |
74 ms |
10448 KB |
Output is correct |
53 |
Correct |
43 ms |
10500 KB |
Output is correct |
54 |
Correct |
87 ms |
11004 KB |
Output is correct |
55 |
Correct |
105 ms |
11096 KB |
Output is correct |
56 |
Correct |
149 ms |
11080 KB |
Output is correct |
57 |
Correct |
89 ms |
11608 KB |
Output is correct |
58 |
Correct |
124 ms |
11476 KB |
Output is correct |
59 |
Correct |
121 ms |
11252 KB |
Output is correct |
60 |
Correct |
161 ms |
11272 KB |
Output is correct |
61 |
Correct |
126 ms |
11364 KB |
Output is correct |
62 |
Correct |
58 ms |
10496 KB |
Output is correct |
63 |
Correct |
121 ms |
10808 KB |
Output is correct |
64 |
Correct |
116 ms |
10652 KB |
Output is correct |
65 |
Correct |
112 ms |
10880 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
85 ms |
11128 KB |
Output is correct |
68 |
Correct |
133 ms |
11080 KB |
Output is correct |
69 |
Correct |
164 ms |
11012 KB |
Output is correct |
70 |
Correct |
109 ms |
11144 KB |
Output is correct |
71 |
Correct |
109 ms |
11136 KB |
Output is correct |
72 |
Correct |
105 ms |
11124 KB |
Output is correct |
73 |
Correct |
82 ms |
11528 KB |
Output is correct |
74 |
Correct |
78 ms |
11520 KB |
Output is correct |
75 |
Correct |
109 ms |
11492 KB |
Output is correct |
76 |
Correct |
106 ms |
11516 KB |
Output is correct |
77 |
Correct |
58 ms |
11612 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |
81 |
Correct |
1 ms |
340 KB |
Output is correct |
82 |
Correct |
1 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
33 ms |
1248 KB |
Output is correct |
87 |
Correct |
32 ms |
1228 KB |
Output is correct |
88 |
Correct |
34 ms |
1524 KB |
Output is correct |
89 |
Correct |
31 ms |
1620 KB |
Output is correct |
90 |
Correct |
24 ms |
1364 KB |
Output is correct |
91 |
Correct |
22 ms |
1468 KB |
Output is correct |
92 |
Correct |
26 ms |
1088 KB |
Output is correct |
93 |
Correct |
57 ms |
10576 KB |
Output is correct |
94 |
Correct |
55 ms |
10572 KB |
Output is correct |
95 |
Correct |
77 ms |
10468 KB |
Output is correct |
96 |
Correct |
64 ms |
10444 KB |
Output is correct |
97 |
Correct |
62 ms |
10456 KB |
Output is correct |
98 |
Correct |
67 ms |
10460 KB |
Output is correct |
99 |
Correct |
37 ms |
10456 KB |
Output is correct |
100 |
Correct |
112 ms |
11280 KB |
Output is correct |
101 |
Correct |
117 ms |
11236 KB |
Output is correct |
102 |
Correct |
117 ms |
11340 KB |
Output is correct |
103 |
Correct |
115 ms |
10824 KB |
Output is correct |
104 |
Correct |
107 ms |
10708 KB |
Output is correct |
105 |
Correct |
118 ms |
10852 KB |
Output is correct |
106 |
Correct |
119 ms |
11212 KB |
Output is correct |
107 |
Correct |
119 ms |
11348 KB |
Output is correct |
108 |
Correct |
124 ms |
11340 KB |
Output is correct |
109 |
Correct |
103 ms |
11272 KB |
Output is correct |
110 |
Correct |
118 ms |
11136 KB |
Output is correct |
111 |
Correct |
130 ms |
11064 KB |
Output is correct |