#include <bits/stdc++.h>
using namespace std;
const int N = 101010;
int n, q, l[N], r[N], w[N], go[20][N], ans[N];
vector<array<int, 2>> query[N];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> l[i] >> r[i];
iota(w + 1, w + 1 + n, 1);
sort(w + 1, w + 1 + n, [&](auto i, auto j) {
if (r[i] == r[j])
return l[i] < l[j];
return r[i] < r[j];
});
for (int i = 0; i < q; i++) {
int s, e; cin >> s >> e;
if (s != e) {
if (r[s] <= r[e])
query[e].push_back({s, i});
else
ans[i] = 1e9;
}
}
vector<int> v;
for (int i = 1; i <= n; i++) {
while (!v.empty() && l[v.back()] >= l[w[i]])
v.pop_back();
v.push_back(w[i]);
int L = 0, R = (int)v.size() - 1;
while (L < R) {
int M = (L + R) / 2;
if (r[v[M]] < l[w[i]])
L = M + 1;
else
R = M;
}
go[0][w[i]] = v[L];
for (int j = 1; j < 20; j++)
go[j][w[i]] = go[j - 1][go[j - 1][w[i]]];
for (auto [s, t] : query[w[i]]) {
int x = w[i];
for (int j = 19; j >= 0; j--) {
if (r[s] < l[go[j][x]]) {
x = go[j][x];
ans[t] += (1 << j);
}
}
ans[t] += (r[s] < l[x]) ? 2 : 1;
}
}
for (int i = 0; i < q; i++) {
if (ans[i] > n)
cout << "impossible\n";
else
cout << ans[i] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
88 ms |
14540 KB |
Output is correct |
3 |
Correct |
91 ms |
14440 KB |
Output is correct |
4 |
Correct |
121 ms |
14976 KB |
Output is correct |
5 |
Correct |
90 ms |
14484 KB |
Output is correct |
6 |
Correct |
101 ms |
14456 KB |
Output is correct |
7 |
Correct |
92 ms |
14496 KB |
Output is correct |
8 |
Correct |
76 ms |
15012 KB |
Output is correct |
9 |
Correct |
75 ms |
17936 KB |
Output is correct |
10 |
Correct |
112 ms |
17772 KB |
Output is correct |
11 |
Correct |
105 ms |
17696 KB |
Output is correct |
12 |
Correct |
49 ms |
16776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2700 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2844 KB |
Output is correct |
4 |
Correct |
2 ms |
2868 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2700 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2844 KB |
Output is correct |
4 |
Correct |
2 ms |
2868 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2900 KB |
Output is correct |
14 |
Correct |
2 ms |
2900 KB |
Output is correct |
15 |
Correct |
3 ms |
2900 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2844 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
33 ms |
6256 KB |
Output is correct |
20 |
Correct |
29 ms |
6252 KB |
Output is correct |
21 |
Correct |
31 ms |
6088 KB |
Output is correct |
22 |
Correct |
25 ms |
6040 KB |
Output is correct |
23 |
Correct |
26 ms |
5972 KB |
Output is correct |
24 |
Correct |
27 ms |
6036 KB |
Output is correct |
25 |
Correct |
28 ms |
6040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2700 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2844 KB |
Output is correct |
4 |
Correct |
2 ms |
2868 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2900 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
1 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2900 KB |
Output is correct |
14 |
Correct |
2 ms |
2900 KB |
Output is correct |
15 |
Correct |
2 ms |
2900 KB |
Output is correct |
16 |
Correct |
2 ms |
2864 KB |
Output is correct |
17 |
Correct |
3 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
66 ms |
14364 KB |
Output is correct |
20 |
Correct |
65 ms |
13472 KB |
Output is correct |
21 |
Correct |
49 ms |
13516 KB |
Output is correct |
22 |
Correct |
65 ms |
14056 KB |
Output is correct |
23 |
Correct |
53 ms |
14248 KB |
Output is correct |
24 |
Correct |
64 ms |
14360 KB |
Output is correct |
25 |
Correct |
26 ms |
12940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
14496 KB |
Output is correct |
2 |
Correct |
94 ms |
14424 KB |
Output is correct |
3 |
Correct |
110 ms |
14864 KB |
Output is correct |
4 |
Correct |
74 ms |
15000 KB |
Output is correct |
5 |
Correct |
104 ms |
14920 KB |
Output is correct |
6 |
Correct |
102 ms |
14656 KB |
Output is correct |
7 |
Correct |
107 ms |
14632 KB |
Output is correct |
8 |
Correct |
89 ms |
14720 KB |
Output is correct |
9 |
Correct |
55 ms |
12568 KB |
Output is correct |
10 |
Correct |
83 ms |
14276 KB |
Output is correct |
11 |
Correct |
78 ms |
14112 KB |
Output is correct |
12 |
Correct |
84 ms |
14240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
88 ms |
14540 KB |
Output is correct |
3 |
Correct |
91 ms |
14440 KB |
Output is correct |
4 |
Correct |
121 ms |
14976 KB |
Output is correct |
5 |
Correct |
90 ms |
14484 KB |
Output is correct |
6 |
Correct |
101 ms |
14456 KB |
Output is correct |
7 |
Correct |
92 ms |
14496 KB |
Output is correct |
8 |
Correct |
76 ms |
15012 KB |
Output is correct |
9 |
Correct |
75 ms |
17936 KB |
Output is correct |
10 |
Correct |
112 ms |
17772 KB |
Output is correct |
11 |
Correct |
105 ms |
17696 KB |
Output is correct |
12 |
Correct |
49 ms |
16776 KB |
Output is correct |
13 |
Correct |
2 ms |
2700 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2844 KB |
Output is correct |
16 |
Correct |
2 ms |
2868 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
2 ms |
2900 KB |
Output is correct |
22 |
Correct |
1 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2900 KB |
Output is correct |
25 |
Correct |
2 ms |
2900 KB |
Output is correct |
26 |
Correct |
2 ms |
2900 KB |
Output is correct |
27 |
Correct |
3 ms |
2900 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2844 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
31 |
Correct |
33 ms |
6256 KB |
Output is correct |
32 |
Correct |
29 ms |
6252 KB |
Output is correct |
33 |
Correct |
31 ms |
6088 KB |
Output is correct |
34 |
Correct |
25 ms |
6040 KB |
Output is correct |
35 |
Correct |
26 ms |
5972 KB |
Output is correct |
36 |
Correct |
27 ms |
6036 KB |
Output is correct |
37 |
Correct |
28 ms |
6040 KB |
Output is correct |
38 |
Correct |
2 ms |
2688 KB |
Output is correct |
39 |
Correct |
1 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2900 KB |
Output is correct |
41 |
Correct |
2 ms |
2900 KB |
Output is correct |
42 |
Correct |
2 ms |
2900 KB |
Output is correct |
43 |
Correct |
2 ms |
2900 KB |
Output is correct |
44 |
Correct |
2 ms |
2864 KB |
Output is correct |
45 |
Correct |
3 ms |
2900 KB |
Output is correct |
46 |
Correct |
2 ms |
2900 KB |
Output is correct |
47 |
Correct |
66 ms |
14364 KB |
Output is correct |
48 |
Correct |
65 ms |
13472 KB |
Output is correct |
49 |
Correct |
49 ms |
13516 KB |
Output is correct |
50 |
Correct |
65 ms |
14056 KB |
Output is correct |
51 |
Correct |
53 ms |
14248 KB |
Output is correct |
52 |
Correct |
64 ms |
14360 KB |
Output is correct |
53 |
Correct |
26 ms |
12940 KB |
Output is correct |
54 |
Correct |
92 ms |
14496 KB |
Output is correct |
55 |
Correct |
94 ms |
14424 KB |
Output is correct |
56 |
Correct |
110 ms |
14864 KB |
Output is correct |
57 |
Correct |
74 ms |
15000 KB |
Output is correct |
58 |
Correct |
104 ms |
14920 KB |
Output is correct |
59 |
Correct |
102 ms |
14656 KB |
Output is correct |
60 |
Correct |
107 ms |
14632 KB |
Output is correct |
61 |
Correct |
89 ms |
14720 KB |
Output is correct |
62 |
Correct |
55 ms |
12568 KB |
Output is correct |
63 |
Correct |
83 ms |
14276 KB |
Output is correct |
64 |
Correct |
78 ms |
14112 KB |
Output is correct |
65 |
Correct |
84 ms |
14240 KB |
Output is correct |
66 |
Correct |
1 ms |
2772 KB |
Output is correct |
67 |
Correct |
90 ms |
17556 KB |
Output is correct |
68 |
Correct |
92 ms |
17476 KB |
Output is correct |
69 |
Correct |
137 ms |
17872 KB |
Output is correct |
70 |
Correct |
90 ms |
17524 KB |
Output is correct |
71 |
Correct |
88 ms |
17484 KB |
Output is correct |
72 |
Correct |
90 ms |
17472 KB |
Output is correct |
73 |
Correct |
73 ms |
17940 KB |
Output is correct |
74 |
Correct |
80 ms |
17972 KB |
Output is correct |
75 |
Correct |
111 ms |
17856 KB |
Output is correct |
76 |
Correct |
98 ms |
17700 KB |
Output is correct |
77 |
Correct |
49 ms |
16840 KB |
Output is correct |
78 |
Correct |
2 ms |
2772 KB |
Output is correct |
79 |
Correct |
2 ms |
2900 KB |
Output is correct |
80 |
Correct |
2 ms |
2848 KB |
Output is correct |
81 |
Correct |
2 ms |
2840 KB |
Output is correct |
82 |
Correct |
2 ms |
2844 KB |
Output is correct |
83 |
Correct |
2 ms |
2900 KB |
Output is correct |
84 |
Correct |
2 ms |
2840 KB |
Output is correct |
85 |
Correct |
2 ms |
2900 KB |
Output is correct |
86 |
Correct |
30 ms |
6152 KB |
Output is correct |
87 |
Correct |
29 ms |
6292 KB |
Output is correct |
88 |
Correct |
31 ms |
6016 KB |
Output is correct |
89 |
Correct |
24 ms |
6016 KB |
Output is correct |
90 |
Correct |
26 ms |
5868 KB |
Output is correct |
91 |
Correct |
25 ms |
5972 KB |
Output is correct |
92 |
Correct |
26 ms |
6080 KB |
Output is correct |
93 |
Correct |
64 ms |
14312 KB |
Output is correct |
94 |
Correct |
59 ms |
13508 KB |
Output is correct |
95 |
Correct |
47 ms |
13516 KB |
Output is correct |
96 |
Correct |
63 ms |
14016 KB |
Output is correct |
97 |
Correct |
55 ms |
14304 KB |
Output is correct |
98 |
Correct |
59 ms |
14308 KB |
Output is correct |
99 |
Correct |
26 ms |
13048 KB |
Output is correct |
100 |
Correct |
104 ms |
17624 KB |
Output is correct |
101 |
Correct |
100 ms |
17596 KB |
Output is correct |
102 |
Correct |
92 ms |
17776 KB |
Output is correct |
103 |
Correct |
84 ms |
17280 KB |
Output is correct |
104 |
Correct |
75 ms |
17128 KB |
Output is correct |
105 |
Correct |
88 ms |
17224 KB |
Output is correct |
106 |
Correct |
115 ms |
16816 KB |
Output is correct |
107 |
Correct |
132 ms |
16884 KB |
Output is correct |
108 |
Correct |
85 ms |
17100 KB |
Output is correct |
109 |
Correct |
91 ms |
17064 KB |
Output is correct |
110 |
Correct |
85 ms |
17028 KB |
Output is correct |
111 |
Correct |
84 ms |
17012 KB |
Output is correct |