#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll INFLL = numeric_limits<ll>::max() / 2;
template<class T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i : a) {
is >> i;
}
return is;
}
struct SegTree {
vector<pair<int, int>> tree;
int n;
SegTree(int n) : n(n) {
tree.resize(4 * n, {INF, -1});
}
void update(int v, int l, int r, int i, pair<int, int> x) {
if (l + 1 == r) {
tree[v] = min(tree[v], x);
return;
}
int m = (l + r) / 2;
if (i < m) {
update(2 * v + 1, l, m, i, x);
} else {
update(2 * v + 2, m, r, i, x);
}
tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
}
void update(int i, pair<int, int> x) {
update(0, 0, n, i, x);
}
pair<int, int> get(int v, int l, int r, int li, int ri) {
if (li >= r || l >= ri) {
return {INF, -1};
}
if (li <= l && r <= ri) {
return tree[v];
}
int m = (l + r) / 2;
return min(get(2 * v + 1, l, m, li, ri), get(2 * v + 2, m, r, li, ri));
}
int get(int li, int ri) {
return get(0, 0, n, li, ri).second;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int n, q;
cin >> n >> q;
vector<array<int, 3>> lr(n);
vector<int> coords;
for (int i = 0; i < n; ++i) {
cin >> lr[i][0] >> lr[i][1];
lr[i][2] = i;
coords.push_back(lr[i][0]);
coords.push_back(lr[i][1]);
}
sort(coords.begin(), coords.end());
coords.resize(unique(coords.begin(), coords.end()) - coords.begin());
sort(lr.begin(), lr.end());
vector<int> p(n);
for (int i = 0; i < n; ++i) {
p[lr[i][2]] = i;
}
for (int i = 0; i < n; ++i) {
auto &[l, r, ii] = lr[i];
l = lower_bound(coords.begin(), coords.end(), l) - coords.begin();
r = lower_bound(coords.begin(), coords.end(), r) - coords.begin();
}
SegTree t(coords.size());
auto can = [&](int i, int j) {// i -> j
auto [li, ri, ii] = lr[i];
auto [lj, rj, jj] = lr[j];
return lj <= ri && ri <= rj;
};
vector<int> prv(n);
for (int i = 0; i < n; ++i) {
// int ind = -1;
// for (int j = 0; j < i; ++j) {
// if (can(j, i)) {
// ind = j;
// break;
// }
// }
// prv[i] = ind;
auto [l, r, _] = lr[i];
prv[i] = t.get(l, r + 1);
t.update(r, {l, i});
}
constexpr int k = 20;
vector<vector<int>> up(k, vector<int>(n, -1));
up[0] = prv;
for (int i = 1; i < k; ++i) {
for (int j = 0; j < n; ++j) {
if (up[i - 1][j] != -1) {
up[i][j] = up[i - 1][up[i - 1][j]];
}
}
}
while (q--) {
int a, b;
cin >> a >> b;
--a;
--b;
a = p[a];
b = p[b];
int ans = 0;
for (int i = k - 1; i >= 0; --i) {
if (up[i][b] != -1 && !can(a, up[i][b]) && lr[up[i][b]][0] > lr[a][1]) {
b = up[i][b];
ans += (1 << i);
}
}
if (!can(a, b)) {
b = up[0][b];
++ans;
}
if (b == -1 || !can(a, b)) {
ans = -1;
} else if (a != b) {
++ans;
}
if (ans == -1) {
cout << "impossible\n";
} else {
cout << ans << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
113 ms |
14960 KB |
Output is correct |
3 |
Correct |
110 ms |
18004 KB |
Output is correct |
4 |
Correct |
113 ms |
18140 KB |
Output is correct |
5 |
Correct |
117 ms |
18368 KB |
Output is correct |
6 |
Correct |
130 ms |
18704 KB |
Output is correct |
7 |
Correct |
118 ms |
18888 KB |
Output is correct |
8 |
Correct |
106 ms |
21716 KB |
Output is correct |
9 |
Correct |
108 ms |
21732 KB |
Output is correct |
10 |
Correct |
151 ms |
18380 KB |
Output is correct |
11 |
Correct |
160 ms |
19700 KB |
Output is correct |
12 |
Correct |
83 ms |
17932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
38 ms |
1484 KB |
Output is correct |
20 |
Correct |
29 ms |
1484 KB |
Output is correct |
21 |
Correct |
41 ms |
1760 KB |
Output is correct |
22 |
Correct |
32 ms |
1824 KB |
Output is correct |
23 |
Correct |
32 ms |
1772 KB |
Output is correct |
24 |
Correct |
26 ms |
1832 KB |
Output is correct |
25 |
Correct |
27 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
376 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
80 ms |
14284 KB |
Output is correct |
20 |
Correct |
76 ms |
14388 KB |
Output is correct |
21 |
Correct |
118 ms |
14308 KB |
Output is correct |
22 |
Correct |
92 ms |
15540 KB |
Output is correct |
23 |
Correct |
100 ms |
17512 KB |
Output is correct |
24 |
Correct |
98 ms |
17516 KB |
Output is correct |
25 |
Correct |
35 ms |
11224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
14976 KB |
Output is correct |
2 |
Correct |
108 ms |
18104 KB |
Output is correct |
3 |
Correct |
117 ms |
18004 KB |
Output is correct |
4 |
Correct |
105 ms |
21740 KB |
Output is correct |
5 |
Correct |
152 ms |
18392 KB |
Output is correct |
6 |
Correct |
148 ms |
21416 KB |
Output is correct |
7 |
Correct |
180 ms |
21384 KB |
Output is correct |
8 |
Correct |
171 ms |
21432 KB |
Output is correct |
9 |
Correct |
92 ms |
19448 KB |
Output is correct |
10 |
Correct |
124 ms |
20956 KB |
Output is correct |
11 |
Correct |
123 ms |
20820 KB |
Output is correct |
12 |
Correct |
130 ms |
20912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
113 ms |
14960 KB |
Output is correct |
3 |
Correct |
110 ms |
18004 KB |
Output is correct |
4 |
Correct |
113 ms |
18140 KB |
Output is correct |
5 |
Correct |
117 ms |
18368 KB |
Output is correct |
6 |
Correct |
130 ms |
18704 KB |
Output is correct |
7 |
Correct |
118 ms |
18888 KB |
Output is correct |
8 |
Correct |
106 ms |
21716 KB |
Output is correct |
9 |
Correct |
108 ms |
21732 KB |
Output is correct |
10 |
Correct |
151 ms |
18380 KB |
Output is correct |
11 |
Correct |
160 ms |
19700 KB |
Output is correct |
12 |
Correct |
83 ms |
17932 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
376 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
38 ms |
1484 KB |
Output is correct |
32 |
Correct |
29 ms |
1484 KB |
Output is correct |
33 |
Correct |
41 ms |
1760 KB |
Output is correct |
34 |
Correct |
32 ms |
1824 KB |
Output is correct |
35 |
Correct |
32 ms |
1772 KB |
Output is correct |
36 |
Correct |
26 ms |
1832 KB |
Output is correct |
37 |
Correct |
27 ms |
1364 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
80 ms |
14284 KB |
Output is correct |
48 |
Correct |
76 ms |
14388 KB |
Output is correct |
49 |
Correct |
118 ms |
14308 KB |
Output is correct |
50 |
Correct |
92 ms |
15540 KB |
Output is correct |
51 |
Correct |
100 ms |
17512 KB |
Output is correct |
52 |
Correct |
98 ms |
17516 KB |
Output is correct |
53 |
Correct |
35 ms |
11224 KB |
Output is correct |
54 |
Correct |
131 ms |
14976 KB |
Output is correct |
55 |
Correct |
108 ms |
18104 KB |
Output is correct |
56 |
Correct |
117 ms |
18004 KB |
Output is correct |
57 |
Correct |
105 ms |
21740 KB |
Output is correct |
58 |
Correct |
152 ms |
18392 KB |
Output is correct |
59 |
Correct |
148 ms |
21416 KB |
Output is correct |
60 |
Correct |
180 ms |
21384 KB |
Output is correct |
61 |
Correct |
171 ms |
21432 KB |
Output is correct |
62 |
Correct |
92 ms |
19448 KB |
Output is correct |
63 |
Correct |
124 ms |
20956 KB |
Output is correct |
64 |
Correct |
123 ms |
20820 KB |
Output is correct |
65 |
Correct |
130 ms |
20912 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
127 ms |
18068 KB |
Output is correct |
68 |
Correct |
110 ms |
18032 KB |
Output is correct |
69 |
Correct |
120 ms |
17984 KB |
Output is correct |
70 |
Correct |
120 ms |
18460 KB |
Output is correct |
71 |
Correct |
130 ms |
18800 KB |
Output is correct |
72 |
Correct |
124 ms |
18900 KB |
Output is correct |
73 |
Correct |
122 ms |
21720 KB |
Output is correct |
74 |
Correct |
119 ms |
21720 KB |
Output is correct |
75 |
Correct |
163 ms |
18468 KB |
Output is correct |
76 |
Correct |
176 ms |
19744 KB |
Output is correct |
77 |
Correct |
112 ms |
17836 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
332 KB |
Output is correct |
86 |
Correct |
38 ms |
2448 KB |
Output is correct |
87 |
Correct |
43 ms |
2508 KB |
Output is correct |
88 |
Correct |
40 ms |
2668 KB |
Output is correct |
89 |
Correct |
35 ms |
2864 KB |
Output is correct |
90 |
Correct |
39 ms |
2756 KB |
Output is correct |
91 |
Correct |
31 ms |
2852 KB |
Output is correct |
92 |
Correct |
34 ms |
2480 KB |
Output is correct |
93 |
Correct |
88 ms |
16344 KB |
Output is correct |
94 |
Correct |
106 ms |
15536 KB |
Output is correct |
95 |
Correct |
115 ms |
16168 KB |
Output is correct |
96 |
Correct |
90 ms |
17496 KB |
Output is correct |
97 |
Correct |
96 ms |
19452 KB |
Output is correct |
98 |
Correct |
99 ms |
19504 KB |
Output is correct |
99 |
Correct |
43 ms |
12436 KB |
Output is correct |
100 |
Correct |
156 ms |
21336 KB |
Output is correct |
101 |
Correct |
162 ms |
21448 KB |
Output is correct |
102 |
Correct |
169 ms |
21508 KB |
Output is correct |
103 |
Correct |
117 ms |
20976 KB |
Output is correct |
104 |
Correct |
145 ms |
20820 KB |
Output is correct |
105 |
Correct |
121 ms |
20948 KB |
Output is correct |
106 |
Correct |
142 ms |
16764 KB |
Output is correct |
107 |
Correct |
172 ms |
17428 KB |
Output is correct |
108 |
Correct |
157 ms |
18204 KB |
Output is correct |
109 |
Correct |
147 ms |
18132 KB |
Output is correct |
110 |
Correct |
168 ms |
21212 KB |
Output is correct |
111 |
Correct |
191 ms |
21264 KB |
Output is correct |