#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int maxn = 1e5 + 5;
struct SegTree {
int n;
vector<pii> tree;
SegTree(int _n) : n(_n), tree(4*_n+5, { 1e9, 0 }) {}
void update(int u, int tl, int tr, int p, int L, int id) {
if(tl == tr) {
tree[u] = min(tree[u], { L, id });
} else {
int tm = (tl + tr) / 2;
if(p <= tm) update(2*u, tl, tm, p, L, id);
else update(2*u+1, tm+1, tr, p, L, id);
tree[u] = min(tree[2*u], tree[2*u+1]);
}
}
pii query(int u, int tl, int tr, int l, int r) {
if(l > tr || tl > r) return { 1e9, 0 };
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return min(query(2*u, tl, tm, l, r), query(2*u+1, tm+1, tr, l, r));
}
};
vector<int> graph[maxn], L(maxn), R(maxn);
int up[maxn][20], n, q;
int main() {
cin >> n >> q;
vector<array<int, 3> > v(n+1); set<int> s;
for(int i=1; i<=n; i++) {
cin >> v[i][1] >> v[i][0]; v[i][2] = i;
s.insert(v[i][1]); s.insert(v[i][0]);
L[i] = v[i][1]; R[i] = v[i][0];
}
vector<int> comp(s.begin(), s.end());
for(auto &[r, l, id] : v) {
if(l == 0 && r == 0) continue;
l = lower_bound(comp.begin(), comp.end(), l) - comp.begin();
r = lower_bound(comp.begin(), comp.end(), r) - comp.begin();
}
SegTree tree(2*n);
sort(v.begin(), v.end());
for(auto &[r, l, id] : v) {
auto [x, y] = tree.query(1, 0, 2*n-1, l, r);
if(y) up[id][0] = y;
tree.update(1, 0, 2*n-1, r, l, id);
}
for(int j=1; j<20; j++)
for(int i=1; i<=n; i++) up[i][j] = up[up[i][j-1]][j-1];
while(q--) {
int a, b;
cin >> a >> b;
if(R[a] > R[b]) {
cout << "impossible\n";
continue;
}
int ans = 0;
for(int j=19; j>=0; j--)
if(up[b][j] != 0 && L[up[b][j]] > R[a]) b = up[b][j], ans |= (1 << j);
if(a == b) cout << ans << '\n';
else if(L[b] <= R[a]) cout << ans + 1 << '\n';
else if(up[b][0] && L[up[b][0]] <= R[a]) cout << ans + 2 << '\n';
else cout << "impossible\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
251 ms |
24520 KB |
Output is correct |
3 |
Correct |
254 ms |
24396 KB |
Output is correct |
4 |
Correct |
242 ms |
24404 KB |
Output is correct |
5 |
Correct |
247 ms |
24884 KB |
Output is correct |
6 |
Correct |
238 ms |
25424 KB |
Output is correct |
7 |
Correct |
245 ms |
25684 KB |
Output is correct |
8 |
Correct |
251 ms |
30036 KB |
Output is correct |
9 |
Correct |
249 ms |
30068 KB |
Output is correct |
10 |
Correct |
238 ms |
24660 KB |
Output is correct |
11 |
Correct |
236 ms |
26704 KB |
Output is correct |
12 |
Correct |
203 ms |
24912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5048 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5280 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5048 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5280 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
124 ms |
8440 KB |
Output is correct |
20 |
Correct |
122 ms |
8200 KB |
Output is correct |
21 |
Correct |
126 ms |
8532 KB |
Output is correct |
22 |
Correct |
127 ms |
8528 KB |
Output is correct |
23 |
Correct |
122 ms |
8784 KB |
Output is correct |
24 |
Correct |
122 ms |
8592 KB |
Output is correct |
25 |
Correct |
128 ms |
8276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5048 KB |
Output is correct |
4 |
Correct |
2 ms |
5212 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5280 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
1 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5036 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
112 ms |
23868 KB |
Output is correct |
20 |
Correct |
95 ms |
23856 KB |
Output is correct |
21 |
Correct |
115 ms |
23892 KB |
Output is correct |
22 |
Correct |
112 ms |
25684 KB |
Output is correct |
23 |
Correct |
133 ms |
28756 KB |
Output is correct |
24 |
Correct |
140 ms |
29004 KB |
Output is correct |
25 |
Correct |
58 ms |
18768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
290 ms |
24376 KB |
Output is correct |
2 |
Correct |
233 ms |
24404 KB |
Output is correct |
3 |
Correct |
264 ms |
24416 KB |
Output is correct |
4 |
Correct |
257 ms |
30032 KB |
Output is correct |
5 |
Correct |
238 ms |
24852 KB |
Output is correct |
6 |
Correct |
269 ms |
29604 KB |
Output is correct |
7 |
Correct |
263 ms |
29520 KB |
Output is correct |
8 |
Correct |
273 ms |
29780 KB |
Output is correct |
9 |
Correct |
139 ms |
29004 KB |
Output is correct |
10 |
Correct |
269 ms |
29264 KB |
Output is correct |
11 |
Correct |
281 ms |
29008 KB |
Output is correct |
12 |
Correct |
308 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
251 ms |
24520 KB |
Output is correct |
3 |
Correct |
254 ms |
24396 KB |
Output is correct |
4 |
Correct |
242 ms |
24404 KB |
Output is correct |
5 |
Correct |
247 ms |
24884 KB |
Output is correct |
6 |
Correct |
238 ms |
25424 KB |
Output is correct |
7 |
Correct |
245 ms |
25684 KB |
Output is correct |
8 |
Correct |
251 ms |
30036 KB |
Output is correct |
9 |
Correct |
249 ms |
30068 KB |
Output is correct |
10 |
Correct |
238 ms |
24660 KB |
Output is correct |
11 |
Correct |
236 ms |
26704 KB |
Output is correct |
12 |
Correct |
203 ms |
24912 KB |
Output is correct |
13 |
Correct |
1 ms |
4952 KB |
Output is correct |
14 |
Correct |
1 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
5048 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5212 KB |
Output is correct |
20 |
Correct |
2 ms |
5280 KB |
Output is correct |
21 |
Correct |
2 ms |
5464 KB |
Output is correct |
22 |
Correct |
1 ms |
4956 KB |
Output is correct |
23 |
Correct |
1 ms |
4956 KB |
Output is correct |
24 |
Correct |
2 ms |
5212 KB |
Output is correct |
25 |
Correct |
2 ms |
5212 KB |
Output is correct |
26 |
Correct |
2 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
2 ms |
5212 KB |
Output is correct |
31 |
Correct |
124 ms |
8440 KB |
Output is correct |
32 |
Correct |
122 ms |
8200 KB |
Output is correct |
33 |
Correct |
126 ms |
8532 KB |
Output is correct |
34 |
Correct |
127 ms |
8528 KB |
Output is correct |
35 |
Correct |
122 ms |
8784 KB |
Output is correct |
36 |
Correct |
122 ms |
8592 KB |
Output is correct |
37 |
Correct |
128 ms |
8276 KB |
Output is correct |
38 |
Correct |
1 ms |
4956 KB |
Output is correct |
39 |
Correct |
1 ms |
4956 KB |
Output is correct |
40 |
Correct |
2 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 ms |
5212 KB |
Output is correct |
42 |
Correct |
2 ms |
5036 KB |
Output is correct |
43 |
Correct |
2 ms |
5212 KB |
Output is correct |
44 |
Correct |
3 ms |
5212 KB |
Output is correct |
45 |
Correct |
3 ms |
5212 KB |
Output is correct |
46 |
Correct |
2 ms |
5212 KB |
Output is correct |
47 |
Correct |
112 ms |
23868 KB |
Output is correct |
48 |
Correct |
95 ms |
23856 KB |
Output is correct |
49 |
Correct |
115 ms |
23892 KB |
Output is correct |
50 |
Correct |
112 ms |
25684 KB |
Output is correct |
51 |
Correct |
133 ms |
28756 KB |
Output is correct |
52 |
Correct |
140 ms |
29004 KB |
Output is correct |
53 |
Correct |
58 ms |
18768 KB |
Output is correct |
54 |
Correct |
290 ms |
24376 KB |
Output is correct |
55 |
Correct |
233 ms |
24404 KB |
Output is correct |
56 |
Correct |
264 ms |
24416 KB |
Output is correct |
57 |
Correct |
257 ms |
30032 KB |
Output is correct |
58 |
Correct |
238 ms |
24852 KB |
Output is correct |
59 |
Correct |
269 ms |
29604 KB |
Output is correct |
60 |
Correct |
263 ms |
29520 KB |
Output is correct |
61 |
Correct |
273 ms |
29780 KB |
Output is correct |
62 |
Correct |
139 ms |
29004 KB |
Output is correct |
63 |
Correct |
269 ms |
29264 KB |
Output is correct |
64 |
Correct |
281 ms |
29008 KB |
Output is correct |
65 |
Correct |
308 ms |
29264 KB |
Output is correct |
66 |
Correct |
1 ms |
4956 KB |
Output is correct |
67 |
Correct |
243 ms |
24384 KB |
Output is correct |
68 |
Correct |
236 ms |
24380 KB |
Output is correct |
69 |
Correct |
261 ms |
24400 KB |
Output is correct |
70 |
Correct |
246 ms |
24916 KB |
Output is correct |
71 |
Correct |
239 ms |
25428 KB |
Output is correct |
72 |
Correct |
239 ms |
25684 KB |
Output is correct |
73 |
Correct |
272 ms |
30032 KB |
Output is correct |
74 |
Correct |
293 ms |
30036 KB |
Output is correct |
75 |
Correct |
241 ms |
24656 KB |
Output is correct |
76 |
Correct |
239 ms |
26704 KB |
Output is correct |
77 |
Correct |
190 ms |
24892 KB |
Output is correct |
78 |
Correct |
1 ms |
4956 KB |
Output is correct |
79 |
Correct |
2 ms |
5212 KB |
Output is correct |
80 |
Correct |
3 ms |
5212 KB |
Output is correct |
81 |
Correct |
2 ms |
5212 KB |
Output is correct |
82 |
Correct |
2 ms |
5212 KB |
Output is correct |
83 |
Correct |
2 ms |
5300 KB |
Output is correct |
84 |
Correct |
2 ms |
5208 KB |
Output is correct |
85 |
Correct |
2 ms |
5212 KB |
Output is correct |
86 |
Correct |
123 ms |
8460 KB |
Output is correct |
87 |
Correct |
121 ms |
8264 KB |
Output is correct |
88 |
Correct |
124 ms |
8532 KB |
Output is correct |
89 |
Correct |
122 ms |
8552 KB |
Output is correct |
90 |
Correct |
121 ms |
8788 KB |
Output is correct |
91 |
Correct |
119 ms |
8732 KB |
Output is correct |
92 |
Correct |
145 ms |
8224 KB |
Output is correct |
93 |
Correct |
115 ms |
23892 KB |
Output is correct |
94 |
Correct |
92 ms |
23732 KB |
Output is correct |
95 |
Correct |
113 ms |
23892 KB |
Output is correct |
96 |
Correct |
116 ms |
25564 KB |
Output is correct |
97 |
Correct |
129 ms |
29004 KB |
Output is correct |
98 |
Correct |
136 ms |
28752 KB |
Output is correct |
99 |
Correct |
50 ms |
18772 KB |
Output is correct |
100 |
Correct |
277 ms |
29520 KB |
Output is correct |
101 |
Correct |
270 ms |
29668 KB |
Output is correct |
102 |
Correct |
266 ms |
29644 KB |
Output is correct |
103 |
Correct |
268 ms |
29268 KB |
Output is correct |
104 |
Correct |
265 ms |
29012 KB |
Output is correct |
105 |
Correct |
273 ms |
29496 KB |
Output is correct |
106 |
Correct |
224 ms |
23380 KB |
Output is correct |
107 |
Correct |
237 ms |
24656 KB |
Output is correct |
108 |
Correct |
241 ms |
24640 KB |
Output is correct |
109 |
Correct |
231 ms |
24660 KB |
Output is correct |
110 |
Correct |
284 ms |
29524 KB |
Output is correct |
111 |
Correct |
275 ms |
29520 KB |
Output is correct |