#include <bits/stdc++.h>
using namespace std;
int s[200005], e[200005];
vector<int> comp;
pair<int, int> tree[4 * 200005];
void update(int x, int l, int r, int loc, pair<int, int> stuff) {
if (l == r) {
tree[x] = min(tree[x], stuff);
return;
}
int mid = (l + r) / 2;
if (loc <= mid) update(2 * x, l, mid, loc, stuff);
else update(2 * x + 1, mid + 1, r, loc, stuff);
tree[x] = min(tree[2 * x], tree[2 * x + 1]);
}
int convert(int x) {
return lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1;
}
int prevjmp[100005][20];
pair<int, int> get(int x, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return tree[x];
if (l > qr || r < ql) return {1e9 + 10, 1e9 + 10};
int mid = (l + r) / 2;
return min(get(2 * x, l, mid, ql, qr), get(2 * x + 1, mid + 1, r, ql, qr));
}
int main() {
int n, q; cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> s[i] >> e[i];
comp.push_back(s[i]);
comp.push_back(e[i]);
}
fill(&tree[0], &tree[0] + sizeof(tree) / sizeof(tree[0]), make_pair(1e9 + 10, 1e9 + 10));
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
int sz = comp.size();
for (int i = 1; i <= n; i++) {
// find furtherst cano back
s[i] = convert(s[i]);
e[i] = convert(e[i]);
update(1, 1, sz, e[i], {s[i], i});
}
for (int i = 1; i <= n; i++) {
prevjmp[i][0] = get(1, 1, sz, s[i], e[i]).second;
}
for (int log = 1; log < 20; log++) {
for (int i = 1; i <= n; i++) {
prevjmp[i][log] = prevjmp[prevjmp[i][log-1]][log-1];
}
}
for (int i = 0; i < q; i++) {
long long ans = 0;
int a, b; cin >> a >> b;
if (a == b) {
cout << "0\n";
continue;
}
if (e[a] > e[b]) {
cout << "impossible\n";
continue;
}
if (s[b] <= e[a] && e[a] <= e[b]) {
cout << "1\n";
continue;
}
for (int log = 19; log >= 0; log--) {
if (s[prevjmp[b][log]] > e[a]) {
b = prevjmp[b][log];
ans += (long long) (1 << log);
}
}
b = prevjmp[b][0];
if (s[b] > e[a]) {
cout << "impossible\n";
continue;
} else {
cout << ans + 2 << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
338 ms |
16784 KB |
Output is correct |
3 |
Correct |
345 ms |
16804 KB |
Output is correct |
4 |
Correct |
395 ms |
16688 KB |
Output is correct |
5 |
Correct |
360 ms |
16712 KB |
Output is correct |
6 |
Correct |
359 ms |
16696 KB |
Output is correct |
7 |
Correct |
368 ms |
16748 KB |
Output is correct |
8 |
Correct |
335 ms |
18160 KB |
Output is correct |
9 |
Correct |
333 ms |
20228 KB |
Output is correct |
10 |
Correct |
354 ms |
20108 KB |
Output is correct |
11 |
Correct |
353 ms |
20076 KB |
Output is correct |
12 |
Correct |
281 ms |
19448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
5 ms |
6656 KB |
Output is correct |
4 |
Correct |
5 ms |
6612 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
5 ms |
6612 KB |
Output is correct |
7 |
Correct |
5 ms |
6596 KB |
Output is correct |
8 |
Correct |
5 ms |
6612 KB |
Output is correct |
9 |
Correct |
5 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
5 ms |
6656 KB |
Output is correct |
4 |
Correct |
5 ms |
6612 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
5 ms |
6612 KB |
Output is correct |
7 |
Correct |
5 ms |
6596 KB |
Output is correct |
8 |
Correct |
5 ms |
6612 KB |
Output is correct |
9 |
Correct |
5 ms |
6612 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6504 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
6 ms |
6632 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
5 ms |
6612 KB |
Output is correct |
16 |
Correct |
5 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6596 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
209 ms |
7640 KB |
Output is correct |
20 |
Correct |
190 ms |
7600 KB |
Output is correct |
21 |
Correct |
181 ms |
7772 KB |
Output is correct |
22 |
Correct |
172 ms |
7836 KB |
Output is correct |
23 |
Correct |
174 ms |
7700 KB |
Output is correct |
24 |
Correct |
177 ms |
7688 KB |
Output is correct |
25 |
Correct |
187 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
5 ms |
6656 KB |
Output is correct |
4 |
Correct |
5 ms |
6612 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
5 ms |
6612 KB |
Output is correct |
7 |
Correct |
5 ms |
6596 KB |
Output is correct |
8 |
Correct |
5 ms |
6612 KB |
Output is correct |
9 |
Correct |
5 ms |
6612 KB |
Output is correct |
10 |
Correct |
3 ms |
6588 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
4 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
5 ms |
6612 KB |
Output is correct |
16 |
Correct |
6 ms |
6612 KB |
Output is correct |
17 |
Correct |
5 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
161 ms |
15992 KB |
Output is correct |
20 |
Correct |
139 ms |
16048 KB |
Output is correct |
21 |
Correct |
166 ms |
16076 KB |
Output is correct |
22 |
Correct |
160 ms |
16056 KB |
Output is correct |
23 |
Correct |
200 ms |
17980 KB |
Output is correct |
24 |
Correct |
177 ms |
17996 KB |
Output is correct |
25 |
Correct |
70 ms |
17292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
341 ms |
16660 KB |
Output is correct |
2 |
Correct |
367 ms |
16572 KB |
Output is correct |
3 |
Correct |
365 ms |
16664 KB |
Output is correct |
4 |
Correct |
364 ms |
18184 KB |
Output is correct |
5 |
Correct |
366 ms |
20108 KB |
Output is correct |
6 |
Correct |
379 ms |
19884 KB |
Output is correct |
7 |
Correct |
405 ms |
19904 KB |
Output is correct |
8 |
Correct |
360 ms |
20140 KB |
Output is correct |
9 |
Correct |
211 ms |
17952 KB |
Output is correct |
10 |
Correct |
380 ms |
19656 KB |
Output is correct |
11 |
Correct |
372 ms |
19264 KB |
Output is correct |
12 |
Correct |
356 ms |
19604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
338 ms |
16784 KB |
Output is correct |
3 |
Correct |
345 ms |
16804 KB |
Output is correct |
4 |
Correct |
395 ms |
16688 KB |
Output is correct |
5 |
Correct |
360 ms |
16712 KB |
Output is correct |
6 |
Correct |
359 ms |
16696 KB |
Output is correct |
7 |
Correct |
368 ms |
16748 KB |
Output is correct |
8 |
Correct |
335 ms |
18160 KB |
Output is correct |
9 |
Correct |
333 ms |
20228 KB |
Output is correct |
10 |
Correct |
354 ms |
20108 KB |
Output is correct |
11 |
Correct |
353 ms |
20076 KB |
Output is correct |
12 |
Correct |
281 ms |
19448 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6484 KB |
Output is correct |
15 |
Correct |
5 ms |
6656 KB |
Output is correct |
16 |
Correct |
5 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
5 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6596 KB |
Output is correct |
20 |
Correct |
5 ms |
6612 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
3 ms |
6484 KB |
Output is correct |
23 |
Correct |
3 ms |
6504 KB |
Output is correct |
24 |
Correct |
4 ms |
6612 KB |
Output is correct |
25 |
Correct |
6 ms |
6632 KB |
Output is correct |
26 |
Correct |
4 ms |
6612 KB |
Output is correct |
27 |
Correct |
5 ms |
6612 KB |
Output is correct |
28 |
Correct |
5 ms |
6612 KB |
Output is correct |
29 |
Correct |
4 ms |
6596 KB |
Output is correct |
30 |
Correct |
4 ms |
6612 KB |
Output is correct |
31 |
Correct |
209 ms |
7640 KB |
Output is correct |
32 |
Correct |
190 ms |
7600 KB |
Output is correct |
33 |
Correct |
181 ms |
7772 KB |
Output is correct |
34 |
Correct |
172 ms |
7836 KB |
Output is correct |
35 |
Correct |
174 ms |
7700 KB |
Output is correct |
36 |
Correct |
177 ms |
7688 KB |
Output is correct |
37 |
Correct |
187 ms |
7380 KB |
Output is correct |
38 |
Correct |
3 ms |
6588 KB |
Output is correct |
39 |
Correct |
3 ms |
6484 KB |
Output is correct |
40 |
Correct |
4 ms |
6612 KB |
Output is correct |
41 |
Correct |
4 ms |
6612 KB |
Output is correct |
42 |
Correct |
4 ms |
6612 KB |
Output is correct |
43 |
Correct |
5 ms |
6612 KB |
Output is correct |
44 |
Correct |
6 ms |
6612 KB |
Output is correct |
45 |
Correct |
5 ms |
6612 KB |
Output is correct |
46 |
Correct |
4 ms |
6612 KB |
Output is correct |
47 |
Correct |
161 ms |
15992 KB |
Output is correct |
48 |
Correct |
139 ms |
16048 KB |
Output is correct |
49 |
Correct |
166 ms |
16076 KB |
Output is correct |
50 |
Correct |
160 ms |
16056 KB |
Output is correct |
51 |
Correct |
200 ms |
17980 KB |
Output is correct |
52 |
Correct |
177 ms |
17996 KB |
Output is correct |
53 |
Correct |
70 ms |
17292 KB |
Output is correct |
54 |
Correct |
341 ms |
16660 KB |
Output is correct |
55 |
Correct |
367 ms |
16572 KB |
Output is correct |
56 |
Correct |
365 ms |
16664 KB |
Output is correct |
57 |
Correct |
364 ms |
18184 KB |
Output is correct |
58 |
Correct |
366 ms |
20108 KB |
Output is correct |
59 |
Correct |
379 ms |
19884 KB |
Output is correct |
60 |
Correct |
405 ms |
19904 KB |
Output is correct |
61 |
Correct |
360 ms |
20140 KB |
Output is correct |
62 |
Correct |
211 ms |
17952 KB |
Output is correct |
63 |
Correct |
380 ms |
19656 KB |
Output is correct |
64 |
Correct |
372 ms |
19264 KB |
Output is correct |
65 |
Correct |
356 ms |
19604 KB |
Output is correct |
66 |
Correct |
3 ms |
6484 KB |
Output is correct |
67 |
Correct |
357 ms |
19752 KB |
Output is correct |
68 |
Correct |
349 ms |
19764 KB |
Output is correct |
69 |
Correct |
385 ms |
19768 KB |
Output is correct |
70 |
Correct |
356 ms |
19904 KB |
Output is correct |
71 |
Correct |
364 ms |
19848 KB |
Output is correct |
72 |
Correct |
361 ms |
19800 KB |
Output is correct |
73 |
Correct |
336 ms |
20244 KB |
Output is correct |
74 |
Correct |
349 ms |
20248 KB |
Output is correct |
75 |
Correct |
351 ms |
20144 KB |
Output is correct |
76 |
Correct |
350 ms |
20184 KB |
Output is correct |
77 |
Correct |
322 ms |
19476 KB |
Output is correct |
78 |
Correct |
3 ms |
6484 KB |
Output is correct |
79 |
Correct |
5 ms |
6612 KB |
Output is correct |
80 |
Correct |
5 ms |
6612 KB |
Output is correct |
81 |
Correct |
4 ms |
6612 KB |
Output is correct |
82 |
Correct |
5 ms |
6604 KB |
Output is correct |
83 |
Correct |
5 ms |
6624 KB |
Output is correct |
84 |
Correct |
7 ms |
6608 KB |
Output is correct |
85 |
Correct |
4 ms |
6612 KB |
Output is correct |
86 |
Correct |
192 ms |
8508 KB |
Output is correct |
87 |
Correct |
180 ms |
8596 KB |
Output is correct |
88 |
Correct |
183 ms |
8800 KB |
Output is correct |
89 |
Correct |
182 ms |
8908 KB |
Output is correct |
90 |
Correct |
172 ms |
8776 KB |
Output is correct |
91 |
Correct |
208 ms |
8712 KB |
Output is correct |
92 |
Correct |
175 ms |
8316 KB |
Output is correct |
93 |
Correct |
160 ms |
17980 KB |
Output is correct |
94 |
Correct |
146 ms |
17148 KB |
Output is correct |
95 |
Correct |
173 ms |
17888 KB |
Output is correct |
96 |
Correct |
162 ms |
18024 KB |
Output is correct |
97 |
Correct |
193 ms |
18012 KB |
Output is correct |
98 |
Correct |
185 ms |
17904 KB |
Output is correct |
99 |
Correct |
71 ms |
17396 KB |
Output is correct |
100 |
Correct |
382 ms |
19916 KB |
Output is correct |
101 |
Correct |
405 ms |
19892 KB |
Output is correct |
102 |
Correct |
359 ms |
19984 KB |
Output is correct |
103 |
Correct |
395 ms |
19460 KB |
Output is correct |
104 |
Correct |
374 ms |
19300 KB |
Output is correct |
105 |
Correct |
371 ms |
19516 KB |
Output is correct |
106 |
Correct |
338 ms |
19188 KB |
Output is correct |
107 |
Correct |
378 ms |
19244 KB |
Output is correct |
108 |
Correct |
351 ms |
19860 KB |
Output is correct |
109 |
Correct |
366 ms |
19816 KB |
Output is correct |
110 |
Correct |
379 ms |
19796 KB |
Output is correct |
111 |
Correct |
377 ms |
19808 KB |
Output is correct |