#include <bits/stdc++.h>
#define pii pair <int, int>
#define st first
#define nd second
#define e second
#define s first
#define rep(i, n, m) for (int i = (n); i <= (m); i ++)
#define rrep(i, n, m) for (int i = (n); i >= (m); i --)
using namespace std;
const long long N = 2e5 + 5;
int n, q, sp[20][N], nxt[20][N];
pii a[N];
vector <int> cmp;
int mini(int i, int j) {
if (i == 0) return j;
if (j == 0) return i;
if (a[i].st < a[j].st) return i;
return j;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
rep(i, 1, n) {
cin >> a[i].st >> a[i].nd;
cmp.push_back(a[i].st);
cmp.push_back(a[i].nd);
}
sort(cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
auto find = [&] (int x) {
return lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin() + 1;
};
rep(i, 1, n) {
a[i].st = find(a[i].st);
a[i].nd = find(a[i].nd);
if (sp[0][a[i].nd] == 0)
sp[0][a[i].nd] = i;
else
sp[0][a[i].nd] = mini(sp[0][a[i].nd], i);
}
rep(i, 1, 19) rep(j, 1, 2 * n)
if (j + (1 << (i - 1)) <= 2 * n)
sp[i][j] = mini(sp[i - 1][j], sp[i - 1][j + (1 << (i - 1))]);
auto get = [&] (int l, int r) {
int k = log2(r - l + 1);
return mini(sp[k][l], sp[k][r - (1 << k) + 1]);
};
rep(i, 1, n) {
int u = get(a[i].st, a[i].nd);
nxt[0][i] = u;
// cout << i << ' ' << u << '\n';
}
rep(i, 1, 19) rep(j, 1, n)
nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
while (q --) {
int u, v;
cin >> u >> v;
if (u == v) {
cout << 0 << '\n';
continue;
}
if (a[u].e > a[v].e) {
cout << "impossible\n";
continue;
}
int ans = 0;
rrep(i, 19, 0) {
int x = nxt[i][v];
if (a[u].e < a[x].s) {
ans += 1 << i;
v = x;
}
}
if (a[v].s <= a[u].e && a[u].e <= a[v].e) ans ++;
else v = nxt[0][v], ans += 2;
if (a[v].s <= a[u].e && a[u].e <= a[v].e)
cout << ans << '\n';
else
cout << "impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
97 ms |
27060 KB |
Output is correct |
3 |
Correct |
108 ms |
27076 KB |
Output is correct |
4 |
Correct |
140 ms |
27076 KB |
Output is correct |
5 |
Correct |
117 ms |
27152 KB |
Output is correct |
6 |
Correct |
118 ms |
27340 KB |
Output is correct |
7 |
Correct |
117 ms |
27328 KB |
Output is correct |
8 |
Correct |
129 ms |
27972 KB |
Output is correct |
9 |
Correct |
126 ms |
28008 KB |
Output is correct |
10 |
Correct |
113 ms |
27472 KB |
Output is correct |
11 |
Correct |
118 ms |
27664 KB |
Output is correct |
12 |
Correct |
66 ms |
27004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
31 ms |
2992 KB |
Output is correct |
20 |
Correct |
32 ms |
3068 KB |
Output is correct |
21 |
Correct |
31 ms |
3216 KB |
Output is correct |
22 |
Correct |
26 ms |
3284 KB |
Output is correct |
23 |
Correct |
27 ms |
3204 KB |
Output is correct |
24 |
Correct |
24 ms |
3116 KB |
Output is correct |
25 |
Correct |
27 ms |
2812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
70 ms |
25344 KB |
Output is correct |
20 |
Correct |
70 ms |
24556 KB |
Output is correct |
21 |
Correct |
71 ms |
25252 KB |
Output is correct |
22 |
Correct |
76 ms |
25516 KB |
Output is correct |
23 |
Correct |
74 ms |
25284 KB |
Output is correct |
24 |
Correct |
75 ms |
25720 KB |
Output is correct |
25 |
Correct |
33 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
27076 KB |
Output is correct |
2 |
Correct |
108 ms |
27092 KB |
Output is correct |
3 |
Correct |
135 ms |
27028 KB |
Output is correct |
4 |
Correct |
125 ms |
28104 KB |
Output is correct |
5 |
Correct |
116 ms |
27484 KB |
Output is correct |
6 |
Correct |
121 ms |
27664 KB |
Output is correct |
7 |
Correct |
117 ms |
27588 KB |
Output is correct |
8 |
Correct |
111 ms |
27764 KB |
Output is correct |
9 |
Correct |
74 ms |
25252 KB |
Output is correct |
10 |
Correct |
110 ms |
27224 KB |
Output is correct |
11 |
Correct |
108 ms |
26936 KB |
Output is correct |
12 |
Correct |
112 ms |
27220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
97 ms |
27060 KB |
Output is correct |
3 |
Correct |
108 ms |
27076 KB |
Output is correct |
4 |
Correct |
140 ms |
27076 KB |
Output is correct |
5 |
Correct |
117 ms |
27152 KB |
Output is correct |
6 |
Correct |
118 ms |
27340 KB |
Output is correct |
7 |
Correct |
117 ms |
27328 KB |
Output is correct |
8 |
Correct |
129 ms |
27972 KB |
Output is correct |
9 |
Correct |
126 ms |
28008 KB |
Output is correct |
10 |
Correct |
113 ms |
27472 KB |
Output is correct |
11 |
Correct |
118 ms |
27664 KB |
Output is correct |
12 |
Correct |
66 ms |
27004 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
0 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
31 ms |
2992 KB |
Output is correct |
32 |
Correct |
32 ms |
3068 KB |
Output is correct |
33 |
Correct |
31 ms |
3216 KB |
Output is correct |
34 |
Correct |
26 ms |
3284 KB |
Output is correct |
35 |
Correct |
27 ms |
3204 KB |
Output is correct |
36 |
Correct |
24 ms |
3116 KB |
Output is correct |
37 |
Correct |
27 ms |
2812 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
70 ms |
25344 KB |
Output is correct |
48 |
Correct |
70 ms |
24556 KB |
Output is correct |
49 |
Correct |
71 ms |
25252 KB |
Output is correct |
50 |
Correct |
76 ms |
25516 KB |
Output is correct |
51 |
Correct |
74 ms |
25284 KB |
Output is correct |
52 |
Correct |
75 ms |
25720 KB |
Output is correct |
53 |
Correct |
33 ms |
24276 KB |
Output is correct |
54 |
Correct |
95 ms |
27076 KB |
Output is correct |
55 |
Correct |
108 ms |
27092 KB |
Output is correct |
56 |
Correct |
135 ms |
27028 KB |
Output is correct |
57 |
Correct |
125 ms |
28104 KB |
Output is correct |
58 |
Correct |
116 ms |
27484 KB |
Output is correct |
59 |
Correct |
121 ms |
27664 KB |
Output is correct |
60 |
Correct |
117 ms |
27588 KB |
Output is correct |
61 |
Correct |
111 ms |
27764 KB |
Output is correct |
62 |
Correct |
74 ms |
25252 KB |
Output is correct |
63 |
Correct |
110 ms |
27224 KB |
Output is correct |
64 |
Correct |
108 ms |
26936 KB |
Output is correct |
65 |
Correct |
112 ms |
27220 KB |
Output is correct |
66 |
Correct |
0 ms |
456 KB |
Output is correct |
67 |
Correct |
95 ms |
27056 KB |
Output is correct |
68 |
Correct |
110 ms |
27112 KB |
Output is correct |
69 |
Correct |
135 ms |
27060 KB |
Output is correct |
70 |
Correct |
116 ms |
27204 KB |
Output is correct |
71 |
Correct |
111 ms |
27208 KB |
Output is correct |
72 |
Correct |
116 ms |
27328 KB |
Output is correct |
73 |
Correct |
123 ms |
28008 KB |
Output is correct |
74 |
Correct |
131 ms |
28012 KB |
Output is correct |
75 |
Correct |
115 ms |
27420 KB |
Output is correct |
76 |
Correct |
126 ms |
27608 KB |
Output is correct |
77 |
Correct |
65 ms |
26884 KB |
Output is correct |
78 |
Correct |
1 ms |
468 KB |
Output is correct |
79 |
Correct |
2 ms |
596 KB |
Output is correct |
80 |
Correct |
1 ms |
596 KB |
Output is correct |
81 |
Correct |
1 ms |
596 KB |
Output is correct |
82 |
Correct |
1 ms |
596 KB |
Output is correct |
83 |
Correct |
1 ms |
596 KB |
Output is correct |
84 |
Correct |
1 ms |
724 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
31 ms |
3024 KB |
Output is correct |
87 |
Correct |
30 ms |
3008 KB |
Output is correct |
88 |
Correct |
31 ms |
3244 KB |
Output is correct |
89 |
Correct |
26 ms |
3348 KB |
Output is correct |
90 |
Correct |
25 ms |
3148 KB |
Output is correct |
91 |
Correct |
25 ms |
3160 KB |
Output is correct |
92 |
Correct |
27 ms |
2892 KB |
Output is correct |
93 |
Correct |
71 ms |
25276 KB |
Output is correct |
94 |
Correct |
73 ms |
24512 KB |
Output is correct |
95 |
Correct |
73 ms |
25220 KB |
Output is correct |
96 |
Correct |
78 ms |
25472 KB |
Output is correct |
97 |
Correct |
75 ms |
25244 KB |
Output is correct |
98 |
Correct |
74 ms |
25708 KB |
Output is correct |
99 |
Correct |
33 ms |
24252 KB |
Output is correct |
100 |
Correct |
135 ms |
27620 KB |
Output is correct |
101 |
Correct |
117 ms |
27572 KB |
Output is correct |
102 |
Correct |
108 ms |
27720 KB |
Output is correct |
103 |
Correct |
109 ms |
27284 KB |
Output is correct |
104 |
Correct |
118 ms |
27044 KB |
Output is correct |
105 |
Correct |
110 ms |
27244 KB |
Output is correct |
106 |
Correct |
111 ms |
26440 KB |
Output is correct |
107 |
Correct |
122 ms |
26476 KB |
Output is correct |
108 |
Correct |
108 ms |
27288 KB |
Output is correct |
109 |
Correct |
106 ms |
27148 KB |
Output is correct |
110 |
Correct |
111 ms |
27464 KB |
Output is correct |
111 |
Correct |
105 ms |
27492 KB |
Output is correct |