#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld double
#define ll long long
mt19937 rnd(51);
const int N = 2e5 + 10, INF = 1e9;
int po[20][N];
pair<int,int> t[4 * N];
void update(int v, int l, int r, int pos, pair<int,int> val) {
if (l == r) {
t[v] = min(t[v], val);
return;
}
int m = (l + r) / 2;
if (pos <= m) {
update(2 * v, l, m, pos, val);
} else {
update(2 * v + 1, m + 1, r, pos, val);
}
t[v] = min(t[v * 2], t[v * 2 + 1]);
}
pair<int,int> get(int v, int tl, int tr, int l, int r) {
if (l > r) return {INF, INF};
if (tl == l && tr == r) {
return t[v];
}
int tm = (tl + tr) / 2;
return min(get(2 * v, tl, tm, l, min(r, tm)), get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i < 4 * N; i++) {
t[i] = {INF, INF};
}
int n, q;
cin >> n >> q;
vector<int> s(n), e(n), u;
for (int i = 0; i < n; i++) {
cin >> s[i] >> e[i];
u.pb(s[i]);
u.pb(e[i]);
}
sort(u.begin(), u.end());
u.erase(unique(u.begin(), u.end()), u.end());
int sz = u.size();
for (int i = 0; i < n; i++) {
s[i] = lower_bound(u.begin(), u.end(), s[i]) - u.begin();
e[i] = lower_bound(u.begin(), u.end(), e[i]) - u.begin();
update(1, 0, sz - 1, e[i], {s[i], i});
}
for (int i = 0; i < n; i++) {
po[0][i] = get(1, 0, sz - 1, s[i], e[i]).second;
}
for (int i = 1; i < 20; i++) {
for (int j = 0; j < n; j++) {
po[i][j] = po[i - 1][po[i - 1][j]];
}
}
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a--, b--;
if (a == b) {
cout << 0 << endl;
} else if (e[a] > e[b]) {
cout << "impossible" << endl;
} else {
if (s[b] <= e[a] && e[a] <= e[b]) {
cout << 1 << endl;
continue;
}
int cnt = 2;
for (int j = 19; j >= 0; j--) {
if (e[a] < s[po[j][b]]) {
b = po[j][b];
cnt += (1 << j);
}
}
b = po[0][b];
if (e[a] < s[b]) {
cout << "impossible" << endl;
} else {
cout << cnt << endl;
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
278 ms |
19900 KB |
Output is correct |
3 |
Correct |
307 ms |
19872 KB |
Output is correct |
4 |
Correct |
320 ms |
19920 KB |
Output is correct |
5 |
Correct |
297 ms |
19924 KB |
Output is correct |
6 |
Correct |
313 ms |
19944 KB |
Output is correct |
7 |
Correct |
288 ms |
19900 KB |
Output is correct |
8 |
Correct |
321 ms |
20304 KB |
Output is correct |
9 |
Correct |
292 ms |
20352 KB |
Output is correct |
10 |
Correct |
299 ms |
20260 KB |
Output is correct |
11 |
Correct |
309 ms |
20276 KB |
Output is correct |
12 |
Correct |
250 ms |
19608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6596 KB |
Output is correct |
2 |
Correct |
4 ms |
6584 KB |
Output is correct |
3 |
Correct |
4 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6756 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6740 KB |
Output is correct |
8 |
Correct |
5 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6596 KB |
Output is correct |
2 |
Correct |
4 ms |
6584 KB |
Output is correct |
3 |
Correct |
4 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6756 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6740 KB |
Output is correct |
8 |
Correct |
5 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6740 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
6740 KB |
Output is correct |
14 |
Correct |
6 ms |
6740 KB |
Output is correct |
15 |
Correct |
7 ms |
6740 KB |
Output is correct |
16 |
Correct |
5 ms |
6740 KB |
Output is correct |
17 |
Correct |
5 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6732 KB |
Output is correct |
19 |
Correct |
172 ms |
8696 KB |
Output is correct |
20 |
Correct |
158 ms |
8708 KB |
Output is correct |
21 |
Correct |
167 ms |
8916 KB |
Output is correct |
22 |
Correct |
191 ms |
9028 KB |
Output is correct |
23 |
Correct |
174 ms |
8900 KB |
Output is correct |
24 |
Correct |
158 ms |
8900 KB |
Output is correct |
25 |
Correct |
150 ms |
8484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6596 KB |
Output is correct |
2 |
Correct |
4 ms |
6584 KB |
Output is correct |
3 |
Correct |
4 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6756 KB |
Output is correct |
5 |
Correct |
4 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
5 ms |
6740 KB |
Output is correct |
8 |
Correct |
5 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6740 KB |
Output is correct |
10 |
Correct |
3 ms |
6612 KB |
Output is correct |
11 |
Correct |
4 ms |
6592 KB |
Output is correct |
12 |
Correct |
5 ms |
6728 KB |
Output is correct |
13 |
Correct |
4 ms |
6740 KB |
Output is correct |
14 |
Correct |
4 ms |
6728 KB |
Output is correct |
15 |
Correct |
5 ms |
6740 KB |
Output is correct |
16 |
Correct |
5 ms |
6728 KB |
Output is correct |
17 |
Correct |
4 ms |
6732 KB |
Output is correct |
18 |
Correct |
4 ms |
6740 KB |
Output is correct |
19 |
Correct |
108 ms |
18116 KB |
Output is correct |
20 |
Correct |
106 ms |
17244 KB |
Output is correct |
21 |
Correct |
115 ms |
18020 KB |
Output is correct |
22 |
Correct |
134 ms |
18052 KB |
Output is correct |
23 |
Correct |
130 ms |
18068 KB |
Output is correct |
24 |
Correct |
137 ms |
18044 KB |
Output is correct |
25 |
Correct |
31 ms |
17472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
19932 KB |
Output is correct |
2 |
Correct |
267 ms |
19796 KB |
Output is correct |
3 |
Correct |
304 ms |
19700 KB |
Output is correct |
4 |
Correct |
307 ms |
20060 KB |
Output is correct |
5 |
Correct |
316 ms |
19980 KB |
Output is correct |
6 |
Correct |
300 ms |
19988 KB |
Output is correct |
7 |
Correct |
355 ms |
19784 KB |
Output is correct |
8 |
Correct |
299 ms |
20080 KB |
Output is correct |
9 |
Correct |
132 ms |
17968 KB |
Output is correct |
10 |
Correct |
334 ms |
19532 KB |
Output is correct |
11 |
Correct |
300 ms |
19380 KB |
Output is correct |
12 |
Correct |
329 ms |
19624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
278 ms |
19900 KB |
Output is correct |
3 |
Correct |
307 ms |
19872 KB |
Output is correct |
4 |
Correct |
320 ms |
19920 KB |
Output is correct |
5 |
Correct |
297 ms |
19924 KB |
Output is correct |
6 |
Correct |
313 ms |
19944 KB |
Output is correct |
7 |
Correct |
288 ms |
19900 KB |
Output is correct |
8 |
Correct |
321 ms |
20304 KB |
Output is correct |
9 |
Correct |
292 ms |
20352 KB |
Output is correct |
10 |
Correct |
299 ms |
20260 KB |
Output is correct |
11 |
Correct |
309 ms |
20276 KB |
Output is correct |
12 |
Correct |
250 ms |
19608 KB |
Output is correct |
13 |
Correct |
4 ms |
6596 KB |
Output is correct |
14 |
Correct |
4 ms |
6584 KB |
Output is correct |
15 |
Correct |
4 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6756 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
4 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
5 ms |
6740 KB |
Output is correct |
21 |
Correct |
4 ms |
6740 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
23 |
Correct |
3 ms |
6612 KB |
Output is correct |
24 |
Correct |
4 ms |
6740 KB |
Output is correct |
25 |
Correct |
6 ms |
6740 KB |
Output is correct |
26 |
Correct |
6 ms |
6740 KB |
Output is correct |
27 |
Correct |
7 ms |
6740 KB |
Output is correct |
28 |
Correct |
5 ms |
6740 KB |
Output is correct |
29 |
Correct |
5 ms |
6740 KB |
Output is correct |
30 |
Correct |
4 ms |
6732 KB |
Output is correct |
31 |
Correct |
172 ms |
8696 KB |
Output is correct |
32 |
Correct |
158 ms |
8708 KB |
Output is correct |
33 |
Correct |
167 ms |
8916 KB |
Output is correct |
34 |
Correct |
191 ms |
9028 KB |
Output is correct |
35 |
Correct |
174 ms |
8900 KB |
Output is correct |
36 |
Correct |
158 ms |
8900 KB |
Output is correct |
37 |
Correct |
150 ms |
8484 KB |
Output is correct |
38 |
Correct |
3 ms |
6612 KB |
Output is correct |
39 |
Correct |
4 ms |
6592 KB |
Output is correct |
40 |
Correct |
5 ms |
6728 KB |
Output is correct |
41 |
Correct |
4 ms |
6740 KB |
Output is correct |
42 |
Correct |
4 ms |
6728 KB |
Output is correct |
43 |
Correct |
5 ms |
6740 KB |
Output is correct |
44 |
Correct |
5 ms |
6728 KB |
Output is correct |
45 |
Correct |
4 ms |
6732 KB |
Output is correct |
46 |
Correct |
4 ms |
6740 KB |
Output is correct |
47 |
Correct |
108 ms |
18116 KB |
Output is correct |
48 |
Correct |
106 ms |
17244 KB |
Output is correct |
49 |
Correct |
115 ms |
18020 KB |
Output is correct |
50 |
Correct |
134 ms |
18052 KB |
Output is correct |
51 |
Correct |
130 ms |
18068 KB |
Output is correct |
52 |
Correct |
137 ms |
18044 KB |
Output is correct |
53 |
Correct |
31 ms |
17472 KB |
Output is correct |
54 |
Correct |
259 ms |
19932 KB |
Output is correct |
55 |
Correct |
267 ms |
19796 KB |
Output is correct |
56 |
Correct |
304 ms |
19700 KB |
Output is correct |
57 |
Correct |
307 ms |
20060 KB |
Output is correct |
58 |
Correct |
316 ms |
19980 KB |
Output is correct |
59 |
Correct |
300 ms |
19988 KB |
Output is correct |
60 |
Correct |
355 ms |
19784 KB |
Output is correct |
61 |
Correct |
299 ms |
20080 KB |
Output is correct |
62 |
Correct |
132 ms |
17968 KB |
Output is correct |
63 |
Correct |
334 ms |
19532 KB |
Output is correct |
64 |
Correct |
300 ms |
19380 KB |
Output is correct |
65 |
Correct |
329 ms |
19624 KB |
Output is correct |
66 |
Correct |
5 ms |
6592 KB |
Output is correct |
67 |
Correct |
300 ms |
19884 KB |
Output is correct |
68 |
Correct |
334 ms |
19840 KB |
Output is correct |
69 |
Correct |
338 ms |
19848 KB |
Output is correct |
70 |
Correct |
356 ms |
19960 KB |
Output is correct |
71 |
Correct |
319 ms |
19936 KB |
Output is correct |
72 |
Correct |
324 ms |
19932 KB |
Output is correct |
73 |
Correct |
337 ms |
20304 KB |
Output is correct |
74 |
Correct |
328 ms |
20308 KB |
Output is correct |
75 |
Correct |
345 ms |
20184 KB |
Output is correct |
76 |
Correct |
339 ms |
20260 KB |
Output is correct |
77 |
Correct |
251 ms |
19604 KB |
Output is correct |
78 |
Correct |
4 ms |
6612 KB |
Output is correct |
79 |
Correct |
4 ms |
6740 KB |
Output is correct |
80 |
Correct |
6 ms |
6740 KB |
Output is correct |
81 |
Correct |
4 ms |
6724 KB |
Output is correct |
82 |
Correct |
6 ms |
6824 KB |
Output is correct |
83 |
Correct |
5 ms |
6728 KB |
Output is correct |
84 |
Correct |
4 ms |
6744 KB |
Output is correct |
85 |
Correct |
4 ms |
6740 KB |
Output is correct |
86 |
Correct |
180 ms |
8668 KB |
Output is correct |
87 |
Correct |
164 ms |
8756 KB |
Output is correct |
88 |
Correct |
168 ms |
8908 KB |
Output is correct |
89 |
Correct |
171 ms |
8972 KB |
Output is correct |
90 |
Correct |
157 ms |
8832 KB |
Output is correct |
91 |
Correct |
157 ms |
8812 KB |
Output is correct |
92 |
Correct |
190 ms |
8512 KB |
Output is correct |
93 |
Correct |
120 ms |
18080 KB |
Output is correct |
94 |
Correct |
143 ms |
17320 KB |
Output is correct |
95 |
Correct |
156 ms |
17972 KB |
Output is correct |
96 |
Correct |
136 ms |
18048 KB |
Output is correct |
97 |
Correct |
157 ms |
18032 KB |
Output is correct |
98 |
Correct |
145 ms |
18092 KB |
Output is correct |
99 |
Correct |
33 ms |
17440 KB |
Output is correct |
100 |
Correct |
326 ms |
20048 KB |
Output is correct |
101 |
Correct |
331 ms |
20020 KB |
Output is correct |
102 |
Correct |
321 ms |
20092 KB |
Output is correct |
103 |
Correct |
298 ms |
19584 KB |
Output is correct |
104 |
Correct |
351 ms |
19560 KB |
Output is correct |
105 |
Correct |
322 ms |
19580 KB |
Output is correct |
106 |
Correct |
311 ms |
19312 KB |
Output is correct |
107 |
Correct |
310 ms |
19288 KB |
Output is correct |
108 |
Correct |
343 ms |
19928 KB |
Output is correct |
109 |
Correct |
345 ms |
20012 KB |
Output is correct |
110 |
Correct |
317 ms |
19920 KB |
Output is correct |
111 |
Correct |
316 ms |
19924 KB |
Output is correct |