#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
vector<int> l, r, si, ids;
vector<vector<int> > parent;
int n, q;
const int psize = 17;
struct Segment_Tree{
vector<int> t;
void build(int v, int l, int r){
if(l == r){
t[v] = si[l-1];
return;
}
int m = (l + r) >> 1;
build(v<<1, l, m);
build(v<<1|1, m+1, r);
t[v] = ::l[t[v<<1]] <= ::l[t[v<<1|1]] ? t[v<<1] : t[v<<1|1];
}
int get(int v, int l, int r, int L, int R){
if(L == l && R == r) return t[v];
int m = (l + r) >> 1;
if(m < L) return get(v<<1|1, m+1, r, L, R);
if(m >= R) return get(v<<1, l, m, L, R);
int i = get(v<<1|1, m+1, r, m+1, R);
int j = get(v<<1, l, m, L, m);
return ::l[j] <= ::l[i] ? j : i;
}
int get(int l, int r){
return ids[get(1, 1, n, l+1, r+1)];
}
void build(){
t.resize(n<<2);
build(1, 1, n);
}
};
int main(){
cin >> n >> q;
l.resize(n);
r.resize(n);
si.resize(n);
ids.resize(n);
parent.resize(n, vector<int>(psize, -1));
for(int i = 0; i < n; i ++){
cin >> l[i] >> r[i];
// r[i] --;
si[i] = i;
}
sort(si.begin(), si.end(), [&](int r, int l){
return ::r[r] != ::r[l] ? ::r[r] < ::r[l] : ::l[r] < ::l[l];
});
for(int i = 0; i < n; i ++) ids[si[i]] = i;
// cout << ids[2] << ' ' << ids[4] << endl;
Segment_Tree st;
st.build();
for(int i = 0, l, r, m; i < n; i ++){
l = 0;
r = i;
while(l < r){
m = (l + r) >> 1;
if(::r[si[m]] >= ::l[si[i]]) r = m;
else l = m + 1;
}
l = st.get(l, i);
if(::l[si[l]] != ::l[si[i]]){
parent[i][0] = l;
for(int j = 1; j < psize; j ++){
if(parent[parent[i][j-1]][j-1] != -1)
parent[i][j] = parent[parent[i][j-1]][j-1];
else break;
}
}
}
for(int i = 0, u, v, cnt = 0; i < q; i ++){
cin >> u >> v;
// cout << u << ' ' << v << ' ';
u --;
v --;
if(u == v){
cout << "0\n";
continue;
}
if(l[v] <= r[u] && r[u] <= r[v]){
cout << 1 << '\n';
continue;
}
u = ids[u];
v = ids[v];
if(u > v){
cout << "impossible\n";
continue;
}
cnt = 0;
for(int i = psize - 1; i >= 0; i --){
if(parent[v][i] != -1 && l[si[parent[v][i]]] > r[si[u]]){
cnt += 1<<i;
v = parent[v][i];
}
}
if(parent[v][0] == -1) cout << "impossible\n";
else cout << cnt + 2 << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
315 ms |
14532 KB |
Output is correct |
3 |
Correct |
322 ms |
14608 KB |
Output is correct |
4 |
Correct |
344 ms |
14568 KB |
Output is correct |
5 |
Correct |
326 ms |
14656 KB |
Output is correct |
6 |
Correct |
338 ms |
14924 KB |
Output is correct |
7 |
Correct |
333 ms |
14988 KB |
Output is correct |
8 |
Correct |
309 ms |
15256 KB |
Output is correct |
9 |
Correct |
271 ms |
17776 KB |
Output is correct |
10 |
Correct |
349 ms |
17556 KB |
Output is correct |
11 |
Correct |
331 ms |
17660 KB |
Output is correct |
12 |
Correct |
245 ms |
16972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
416 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
184 ms |
1392 KB |
Output is correct |
20 |
Correct |
180 ms |
1360 KB |
Output is correct |
21 |
Correct |
184 ms |
1668 KB |
Output is correct |
22 |
Correct |
178 ms |
1664 KB |
Output is correct |
23 |
Correct |
183 ms |
1664 KB |
Output is correct |
24 |
Correct |
175 ms |
1476 KB |
Output is correct |
25 |
Correct |
179 ms |
1224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 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 |
2 ms |
412 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
124 ms |
13696 KB |
Output is correct |
20 |
Correct |
99 ms |
13572 KB |
Output is correct |
21 |
Correct |
108 ms |
13580 KB |
Output is correct |
22 |
Correct |
120 ms |
14624 KB |
Output is correct |
23 |
Correct |
101 ms |
13516 KB |
Output is correct |
24 |
Correct |
117 ms |
13568 KB |
Output is correct |
25 |
Correct |
71 ms |
13580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
14224 KB |
Output is correct |
2 |
Correct |
314 ms |
14208 KB |
Output is correct |
3 |
Correct |
343 ms |
14092 KB |
Output is correct |
4 |
Correct |
275 ms |
14596 KB |
Output is correct |
5 |
Correct |
328 ms |
14660 KB |
Output is correct |
6 |
Correct |
357 ms |
14756 KB |
Output is correct |
7 |
Correct |
354 ms |
14896 KB |
Output is correct |
8 |
Correct |
321 ms |
15260 KB |
Output is correct |
9 |
Correct |
100 ms |
14244 KB |
Output is correct |
10 |
Correct |
300 ms |
14624 KB |
Output is correct |
11 |
Correct |
302 ms |
14416 KB |
Output is correct |
12 |
Correct |
303 ms |
14656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
315 ms |
14532 KB |
Output is correct |
3 |
Correct |
322 ms |
14608 KB |
Output is correct |
4 |
Correct |
344 ms |
14568 KB |
Output is correct |
5 |
Correct |
326 ms |
14656 KB |
Output is correct |
6 |
Correct |
338 ms |
14924 KB |
Output is correct |
7 |
Correct |
333 ms |
14988 KB |
Output is correct |
8 |
Correct |
309 ms |
15256 KB |
Output is correct |
9 |
Correct |
271 ms |
17776 KB |
Output is correct |
10 |
Correct |
349 ms |
17556 KB |
Output is correct |
11 |
Correct |
331 ms |
17660 KB |
Output is correct |
12 |
Correct |
245 ms |
16972 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
416 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
184 ms |
1392 KB |
Output is correct |
32 |
Correct |
180 ms |
1360 KB |
Output is correct |
33 |
Correct |
184 ms |
1668 KB |
Output is correct |
34 |
Correct |
178 ms |
1664 KB |
Output is correct |
35 |
Correct |
183 ms |
1664 KB |
Output is correct |
36 |
Correct |
175 ms |
1476 KB |
Output is correct |
37 |
Correct |
179 ms |
1224 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
412 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
124 ms |
13696 KB |
Output is correct |
48 |
Correct |
99 ms |
13572 KB |
Output is correct |
49 |
Correct |
108 ms |
13580 KB |
Output is correct |
50 |
Correct |
120 ms |
14624 KB |
Output is correct |
51 |
Correct |
101 ms |
13516 KB |
Output is correct |
52 |
Correct |
117 ms |
13568 KB |
Output is correct |
53 |
Correct |
71 ms |
13580 KB |
Output is correct |
54 |
Correct |
300 ms |
14224 KB |
Output is correct |
55 |
Correct |
314 ms |
14208 KB |
Output is correct |
56 |
Correct |
343 ms |
14092 KB |
Output is correct |
57 |
Correct |
275 ms |
14596 KB |
Output is correct |
58 |
Correct |
328 ms |
14660 KB |
Output is correct |
59 |
Correct |
357 ms |
14756 KB |
Output is correct |
60 |
Correct |
354 ms |
14896 KB |
Output is correct |
61 |
Correct |
321 ms |
15260 KB |
Output is correct |
62 |
Correct |
100 ms |
14244 KB |
Output is correct |
63 |
Correct |
300 ms |
14624 KB |
Output is correct |
64 |
Correct |
302 ms |
14416 KB |
Output is correct |
65 |
Correct |
303 ms |
14656 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
361 ms |
17324 KB |
Output is correct |
68 |
Correct |
320 ms |
17236 KB |
Output is correct |
69 |
Correct |
399 ms |
17300 KB |
Output is correct |
70 |
Correct |
333 ms |
17280 KB |
Output is correct |
71 |
Correct |
316 ms |
17268 KB |
Output is correct |
72 |
Correct |
460 ms |
17404 KB |
Output is correct |
73 |
Correct |
271 ms |
17720 KB |
Output is correct |
74 |
Correct |
276 ms |
17740 KB |
Output is correct |
75 |
Correct |
370 ms |
17652 KB |
Output is correct |
76 |
Correct |
362 ms |
17660 KB |
Output is correct |
77 |
Correct |
253 ms |
17044 KB |
Output is correct |
78 |
Correct |
1 ms |
296 KB |
Output is correct |
79 |
Correct |
2 ms |
432 KB |
Output is correct |
80 |
Correct |
2 ms |
436 KB |
Output is correct |
81 |
Correct |
1 ms |
340 KB |
Output is correct |
82 |
Correct |
3 ms |
340 KB |
Output is correct |
83 |
Correct |
2 ms |
340 KB |
Output is correct |
84 |
Correct |
2 ms |
340 KB |
Output is correct |
85 |
Correct |
2 ms |
340 KB |
Output is correct |
86 |
Correct |
205 ms |
2412 KB |
Output is correct |
87 |
Correct |
249 ms |
2408 KB |
Output is correct |
88 |
Correct |
221 ms |
2616 KB |
Output is correct |
89 |
Correct |
185 ms |
2764 KB |
Output is correct |
90 |
Correct |
193 ms |
2512 KB |
Output is correct |
91 |
Correct |
192 ms |
2596 KB |
Output is correct |
92 |
Correct |
181 ms |
2252 KB |
Output is correct |
93 |
Correct |
128 ms |
15552 KB |
Output is correct |
94 |
Correct |
121 ms |
14704 KB |
Output is correct |
95 |
Correct |
111 ms |
15348 KB |
Output is correct |
96 |
Correct |
114 ms |
15480 KB |
Output is correct |
97 |
Correct |
101 ms |
15508 KB |
Output is correct |
98 |
Correct |
125 ms |
15436 KB |
Output is correct |
99 |
Correct |
75 ms |
14796 KB |
Output is correct |
100 |
Correct |
344 ms |
17332 KB |
Output is correct |
101 |
Correct |
373 ms |
17384 KB |
Output is correct |
102 |
Correct |
311 ms |
17560 KB |
Output is correct |
103 |
Correct |
305 ms |
17076 KB |
Output is correct |
104 |
Correct |
301 ms |
16876 KB |
Output is correct |
105 |
Correct |
310 ms |
17140 KB |
Output is correct |
106 |
Correct |
331 ms |
16896 KB |
Output is correct |
107 |
Correct |
370 ms |
16632 KB |
Output is correct |
108 |
Correct |
300 ms |
17356 KB |
Output is correct |
109 |
Correct |
299 ms |
17400 KB |
Output is correct |
110 |
Correct |
318 ms |
17332 KB |
Output is correct |
111 |
Correct |
308 ms |
17324 KB |
Output is correct |