#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 25;
const int B = 18;
int dp[B][MAXN];
int n, q;
array <int, 3> a[MAXN];
int jump (int a, int b) {
for (int i = B - 1; i >= 0; i--) {
if (b & (1 << i)) {
a = dp[i][a];
}
}
return a;
}
int idx[MAXN];
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i][0] >> a[i][1]; a[i][2] = i;
}
sort(a + 1, a + n + 1, [] (array <int, 3>& x, array <int, 3>& y) {
return x[1] == y[1] ? x[0] < y[0] : x[1] < y[1];
});
for (int i = 1; i <= n; i++) {
idx[a[i][2]] = i;
}
vector <array <int, 3>> cur;
for (int i = 1; i <= n; i++) {
array <int, 3> v = {a[i][1], a[i][0], i};
while (!cur.empty() && cur.back()[1] >= v[1]) {
cur.pop_back();
}
cur.push_back(v);
int l = 0, r = (int)cur.size() - 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (cur[mid][0] >= a[i][0]) {
r = mid - 1; ans = mid;
} else {
l = mid + 1;
}
}
dp[0][a[i][2]] = a[cur[ans][2]][2];
}
for (int i = 1; i < B; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
}
while (q--) {
int x, y; cin >> x >> y;
//x = idx[x], y = idx[y];
if (x == y) {
cout << "0\n";
continue;
}
int ans = 0;
if (a[idx[y]][0] <= a[idx[x]][1]) {
ans = -1;
} else {
auto l = y;
for (int i = B - 1; i >= 0; i--) {
auto g = dp[i][l];
if (!g) continue;
if (a[idx[g]][0] > a[idx[x]][1]) {
ans ^= 1 << i;
l = g;
}
}
}
y = jump(y, ans + 1);
x = idx[x], y = idx[y];
if (a[x][1] <= a[y][1] && a[x][1] >= a[y][0]) {
cout << ans + 2 << '\n';
} else {
cout << "impossible\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
50 ms |
11924 KB |
Output is correct |
3 |
Correct |
60 ms |
14020 KB |
Output is correct |
4 |
Correct |
69 ms |
13880 KB |
Output is correct |
5 |
Correct |
63 ms |
13964 KB |
Output is correct |
6 |
Correct |
61 ms |
13740 KB |
Output is correct |
7 |
Correct |
62 ms |
13764 KB |
Output is correct |
8 |
Correct |
65 ms |
14524 KB |
Output is correct |
9 |
Correct |
64 ms |
14512 KB |
Output is correct |
10 |
Correct |
61 ms |
14276 KB |
Output is correct |
11 |
Correct |
60 ms |
13800 KB |
Output is correct |
12 |
Correct |
37 ms |
13004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8660 KB |
Output is correct |
13 |
Correct |
2 ms |
8656 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
30 ms |
9020 KB |
Output is correct |
20 |
Correct |
26 ms |
9308 KB |
Output is correct |
21 |
Correct |
40 ms |
9808 KB |
Output is correct |
22 |
Correct |
23 ms |
9304 KB |
Output is correct |
23 |
Correct |
21 ms |
9296 KB |
Output is correct |
24 |
Correct |
20 ms |
9304 KB |
Output is correct |
25 |
Correct |
25 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
2 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8792 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
28 ms |
10192 KB |
Output is correct |
20 |
Correct |
27 ms |
11304 KB |
Output is correct |
21 |
Correct |
29 ms |
10760 KB |
Output is correct |
22 |
Correct |
30 ms |
11808 KB |
Output is correct |
23 |
Correct |
34 ms |
12228 KB |
Output is correct |
24 |
Correct |
31 ms |
12192 KB |
Output is correct |
25 |
Correct |
23 ms |
10300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
11716 KB |
Output is correct |
2 |
Correct |
60 ms |
13884 KB |
Output is correct |
3 |
Correct |
101 ms |
14024 KB |
Output is correct |
4 |
Correct |
65 ms |
14508 KB |
Output is correct |
5 |
Correct |
61 ms |
14240 KB |
Output is correct |
6 |
Correct |
67 ms |
14044 KB |
Output is correct |
7 |
Correct |
65 ms |
14004 KB |
Output is correct |
8 |
Correct |
63 ms |
14276 KB |
Output is correct |
9 |
Correct |
29 ms |
12236 KB |
Output is correct |
10 |
Correct |
61 ms |
13584 KB |
Output is correct |
11 |
Correct |
58 ms |
13512 KB |
Output is correct |
12 |
Correct |
58 ms |
13768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
50 ms |
11924 KB |
Output is correct |
3 |
Correct |
60 ms |
14020 KB |
Output is correct |
4 |
Correct |
69 ms |
13880 KB |
Output is correct |
5 |
Correct |
63 ms |
13964 KB |
Output is correct |
6 |
Correct |
61 ms |
13740 KB |
Output is correct |
7 |
Correct |
62 ms |
13764 KB |
Output is correct |
8 |
Correct |
65 ms |
14524 KB |
Output is correct |
9 |
Correct |
64 ms |
14512 KB |
Output is correct |
10 |
Correct |
61 ms |
14276 KB |
Output is correct |
11 |
Correct |
60 ms |
13800 KB |
Output is correct |
12 |
Correct |
37 ms |
13004 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
1 ms |
8540 KB |
Output is correct |
19 |
Correct |
2 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
8540 KB |
Output is correct |
24 |
Correct |
1 ms |
8660 KB |
Output is correct |
25 |
Correct |
2 ms |
8656 KB |
Output is correct |
26 |
Correct |
1 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8540 KB |
Output is correct |
28 |
Correct |
1 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
31 |
Correct |
30 ms |
9020 KB |
Output is correct |
32 |
Correct |
26 ms |
9308 KB |
Output is correct |
33 |
Correct |
40 ms |
9808 KB |
Output is correct |
34 |
Correct |
23 ms |
9304 KB |
Output is correct |
35 |
Correct |
21 ms |
9296 KB |
Output is correct |
36 |
Correct |
20 ms |
9304 KB |
Output is correct |
37 |
Correct |
25 ms |
9048 KB |
Output is correct |
38 |
Correct |
1 ms |
8536 KB |
Output is correct |
39 |
Correct |
2 ms |
8540 KB |
Output is correct |
40 |
Correct |
1 ms |
8540 KB |
Output is correct |
41 |
Correct |
2 ms |
8540 KB |
Output is correct |
42 |
Correct |
2 ms |
8540 KB |
Output is correct |
43 |
Correct |
2 ms |
8540 KB |
Output is correct |
44 |
Correct |
2 ms |
8540 KB |
Output is correct |
45 |
Correct |
2 ms |
8792 KB |
Output is correct |
46 |
Correct |
1 ms |
8540 KB |
Output is correct |
47 |
Correct |
28 ms |
10192 KB |
Output is correct |
48 |
Correct |
27 ms |
11304 KB |
Output is correct |
49 |
Correct |
29 ms |
10760 KB |
Output is correct |
50 |
Correct |
30 ms |
11808 KB |
Output is correct |
51 |
Correct |
34 ms |
12228 KB |
Output is correct |
52 |
Correct |
31 ms |
12192 KB |
Output is correct |
53 |
Correct |
23 ms |
10300 KB |
Output is correct |
54 |
Correct |
50 ms |
11716 KB |
Output is correct |
55 |
Correct |
60 ms |
13884 KB |
Output is correct |
56 |
Correct |
101 ms |
14024 KB |
Output is correct |
57 |
Correct |
65 ms |
14508 KB |
Output is correct |
58 |
Correct |
61 ms |
14240 KB |
Output is correct |
59 |
Correct |
67 ms |
14044 KB |
Output is correct |
60 |
Correct |
65 ms |
14004 KB |
Output is correct |
61 |
Correct |
63 ms |
14276 KB |
Output is correct |
62 |
Correct |
29 ms |
12236 KB |
Output is correct |
63 |
Correct |
61 ms |
13584 KB |
Output is correct |
64 |
Correct |
58 ms |
13512 KB |
Output is correct |
65 |
Correct |
58 ms |
13768 KB |
Output is correct |
66 |
Correct |
2 ms |
8540 KB |
Output is correct |
67 |
Correct |
55 ms |
13864 KB |
Output is correct |
68 |
Correct |
59 ms |
14016 KB |
Output is correct |
69 |
Correct |
74 ms |
13852 KB |
Output is correct |
70 |
Correct |
65 ms |
14024 KB |
Output is correct |
71 |
Correct |
61 ms |
13764 KB |
Output is correct |
72 |
Correct |
65 ms |
14020 KB |
Output is correct |
73 |
Correct |
65 ms |
14488 KB |
Output is correct |
74 |
Correct |
70 ms |
14528 KB |
Output is correct |
75 |
Correct |
64 ms |
14272 KB |
Output is correct |
76 |
Correct |
65 ms |
13824 KB |
Output is correct |
77 |
Correct |
42 ms |
13204 KB |
Output is correct |
78 |
Correct |
1 ms |
8540 KB |
Output is correct |
79 |
Correct |
2 ms |
8540 KB |
Output is correct |
80 |
Correct |
2 ms |
8540 KB |
Output is correct |
81 |
Correct |
2 ms |
8540 KB |
Output is correct |
82 |
Correct |
2 ms |
8676 KB |
Output is correct |
83 |
Correct |
2 ms |
8540 KB |
Output is correct |
84 |
Correct |
2 ms |
8536 KB |
Output is correct |
85 |
Correct |
1 ms |
8648 KB |
Output is correct |
86 |
Correct |
30 ms |
10068 KB |
Output is correct |
87 |
Correct |
26 ms |
10320 KB |
Output is correct |
88 |
Correct |
32 ms |
10580 KB |
Output is correct |
89 |
Correct |
23 ms |
10376 KB |
Output is correct |
90 |
Correct |
21 ms |
10328 KB |
Output is correct |
91 |
Correct |
20 ms |
10332 KB |
Output is correct |
92 |
Correct |
25 ms |
10136 KB |
Output is correct |
93 |
Correct |
27 ms |
12240 KB |
Output is correct |
94 |
Correct |
24 ms |
11436 KB |
Output is correct |
95 |
Correct |
27 ms |
10760 KB |
Output is correct |
96 |
Correct |
27 ms |
11724 KB |
Output is correct |
97 |
Correct |
27 ms |
12228 KB |
Output is correct |
98 |
Correct |
28 ms |
12204 KB |
Output is correct |
99 |
Correct |
17 ms |
10332 KB |
Output is correct |
100 |
Correct |
66 ms |
14020 KB |
Output is correct |
101 |
Correct |
63 ms |
14020 KB |
Output is correct |
102 |
Correct |
65 ms |
14232 KB |
Output is correct |
103 |
Correct |
59 ms |
13592 KB |
Output is correct |
104 |
Correct |
56 ms |
13632 KB |
Output is correct |
105 |
Correct |
59 ms |
13760 KB |
Output is correct |
106 |
Correct |
62 ms |
13024 KB |
Output is correct |
107 |
Correct |
70 ms |
13504 KB |
Output is correct |
108 |
Correct |
55 ms |
12624 KB |
Output is correct |
109 |
Correct |
54 ms |
12880 KB |
Output is correct |
110 |
Correct |
49 ms |
12624 KB |
Output is correct |
111 |
Correct |
49 ms |
12920 KB |
Output is correct |