#include<bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
int n, q;
int a[20][MAX], S[MAX], E[MAX], v[MAX], id[MAX], _prev[20][MAX];
int lg(int i){
return i ? __builtin_clz(1) - __builtin_clz(i) : -1;
}
void init(){
for(int i = 1; i < 20; i++){
for(int j = 1; j + (1 << i) - 1 < MAX; j++){
if(v[a[i - 1][j]] < v[a[i - 1][j + (1 << (i - 1))]]){
a[i][j] = a[i - 1][j];
}else{
a[i][j] = a[i - 1][j + (1 << (i - 1))];
}
}
}
}
int query(int l, int r){
int k = lg(r - l + 1);
if(v[a[k][l]] <= v[a[k][r - (1 << k) + 1]]) return id[a[k][l]];
return id[a[k][r - (1 << k) + 1]];
}
void solve(){
cin >> n >> q;
map<int, int> m;
for(int i = 1; i <= n; i++) cin >> S[i] >> E[i], m[S[i]], m[E[i]];
int idx = 0;
for(auto& i : m) i.second = ++idx;
for(int i = 1; i <= n; i++) S[i] = m[S[i]], E[i] = m[E[i]];
fill(v + 1, v + 1 + m.size(), MAX);
for (int i = 1; i <= n; i++)
{
if(v[E[i]] > S[i]){
v[E[i]] = S[i];
id[E[i]] = i;
}
}
for (int i = 1; i < MAX; i++) a[0][i] = i;
init();
for (int i = 1; i <= n; i++){
_prev[0][i] = query(S[i], E[i]);
if(_prev[0][i] == i) _prev[0][i] = 0;
}
for (int i = 1; i < 20; i++)
{
for(int j = 1; j <= n; j++){
_prev[i][j] = _prev[i - 1][_prev[i - 1][j]];
}
}
while (q--)
{
int s, e;
cin >> s >> e;
if(s == e){
cout << 0 << endl;
continue;
}
if(E[s] > E[e]){
cout << "impossible" << endl;
continue;
}
if(S[e] <= E[s] && E[s] <= E[e]){
cout << 1 << endl;
continue;
}
int ans = 0;
for(int i = 19; i >= 0; i--){
if(S[_prev[i][e]] > E[s]){
e = _prev[i][e];
ans += (1 << i);
}
}
e = _prev[0][e];
if(e == 0) cout << "impossible" << endl;
else cout << ans + 2 << endl;
}
}
int main(){
int t = 1;
//cin>>t;
for(int i = 1; i <= t; i++){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
20708 KB |
Output is correct |
2 |
Correct |
399 ms |
35340 KB |
Output is correct |
3 |
Correct |
511 ms |
35372 KB |
Output is correct |
4 |
Correct |
457 ms |
35372 KB |
Output is correct |
5 |
Correct |
461 ms |
35864 KB |
Output is correct |
6 |
Correct |
472 ms |
36500 KB |
Output is correct |
7 |
Correct |
558 ms |
36648 KB |
Output is correct |
8 |
Correct |
497 ms |
41320 KB |
Output is correct |
9 |
Correct |
488 ms |
41324 KB |
Output is correct |
10 |
Correct |
481 ms |
35704 KB |
Output is correct |
11 |
Correct |
558 ms |
37888 KB |
Output is correct |
12 |
Correct |
339 ms |
35884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
20644 KB |
Output is correct |
2 |
Correct |
43 ms |
20692 KB |
Output is correct |
3 |
Correct |
16 ms |
20820 KB |
Output is correct |
4 |
Correct |
16 ms |
20800 KB |
Output is correct |
5 |
Correct |
19 ms |
20772 KB |
Output is correct |
6 |
Correct |
17 ms |
20948 KB |
Output is correct |
7 |
Correct |
19 ms |
20900 KB |
Output is correct |
8 |
Correct |
16 ms |
20948 KB |
Output is correct |
9 |
Correct |
16 ms |
20820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
20644 KB |
Output is correct |
2 |
Correct |
43 ms |
20692 KB |
Output is correct |
3 |
Correct |
16 ms |
20820 KB |
Output is correct |
4 |
Correct |
16 ms |
20800 KB |
Output is correct |
5 |
Correct |
19 ms |
20772 KB |
Output is correct |
6 |
Correct |
17 ms |
20948 KB |
Output is correct |
7 |
Correct |
19 ms |
20900 KB |
Output is correct |
8 |
Correct |
16 ms |
20948 KB |
Output is correct |
9 |
Correct |
16 ms |
20820 KB |
Output is correct |
10 |
Correct |
23 ms |
20644 KB |
Output is correct |
11 |
Correct |
23 ms |
20716 KB |
Output is correct |
12 |
Correct |
17 ms |
20820 KB |
Output is correct |
13 |
Correct |
18 ms |
20820 KB |
Output is correct |
14 |
Correct |
20 ms |
20820 KB |
Output is correct |
15 |
Correct |
17 ms |
20828 KB |
Output is correct |
16 |
Correct |
19 ms |
20868 KB |
Output is correct |
17 |
Correct |
24 ms |
20876 KB |
Output is correct |
18 |
Correct |
15 ms |
20844 KB |
Output is correct |
19 |
Correct |
255 ms |
21976 KB |
Output is correct |
20 |
Correct |
217 ms |
21836 KB |
Output is correct |
21 |
Correct |
236 ms |
22176 KB |
Output is correct |
22 |
Correct |
203 ms |
22216 KB |
Output is correct |
23 |
Correct |
219 ms |
22336 KB |
Output is correct |
24 |
Correct |
251 ms |
22284 KB |
Output is correct |
25 |
Correct |
206 ms |
21996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
20644 KB |
Output is correct |
2 |
Correct |
43 ms |
20692 KB |
Output is correct |
3 |
Correct |
16 ms |
20820 KB |
Output is correct |
4 |
Correct |
16 ms |
20800 KB |
Output is correct |
5 |
Correct |
19 ms |
20772 KB |
Output is correct |
6 |
Correct |
17 ms |
20948 KB |
Output is correct |
7 |
Correct |
19 ms |
20900 KB |
Output is correct |
8 |
Correct |
16 ms |
20948 KB |
Output is correct |
9 |
Correct |
16 ms |
20820 KB |
Output is correct |
10 |
Correct |
14 ms |
20692 KB |
Output is correct |
11 |
Correct |
14 ms |
20748 KB |
Output is correct |
12 |
Correct |
16 ms |
20836 KB |
Output is correct |
13 |
Correct |
16 ms |
20892 KB |
Output is correct |
14 |
Correct |
17 ms |
20812 KB |
Output is correct |
15 |
Correct |
18 ms |
20888 KB |
Output is correct |
16 |
Correct |
18 ms |
20868 KB |
Output is correct |
17 |
Correct |
15 ms |
20948 KB |
Output is correct |
18 |
Correct |
18 ms |
20800 KB |
Output is correct |
19 |
Correct |
192 ms |
34776 KB |
Output is correct |
20 |
Correct |
179 ms |
34732 KB |
Output is correct |
21 |
Correct |
180 ms |
34776 KB |
Output is correct |
22 |
Correct |
225 ms |
36748 KB |
Output is correct |
23 |
Correct |
317 ms |
39864 KB |
Output is correct |
24 |
Correct |
272 ms |
40196 KB |
Output is correct |
25 |
Correct |
101 ms |
29212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
400 ms |
35396 KB |
Output is correct |
2 |
Correct |
557 ms |
35356 KB |
Output is correct |
3 |
Correct |
505 ms |
35356 KB |
Output is correct |
4 |
Correct |
407 ms |
41348 KB |
Output is correct |
5 |
Correct |
482 ms |
35676 KB |
Output is correct |
6 |
Correct |
468 ms |
40980 KB |
Output is correct |
7 |
Correct |
434 ms |
40888 KB |
Output is correct |
8 |
Correct |
498 ms |
41044 KB |
Output is correct |
9 |
Correct |
230 ms |
39788 KB |
Output is correct |
10 |
Correct |
478 ms |
40668 KB |
Output is correct |
11 |
Correct |
470 ms |
40452 KB |
Output is correct |
12 |
Correct |
417 ms |
40720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
20708 KB |
Output is correct |
2 |
Correct |
399 ms |
35340 KB |
Output is correct |
3 |
Correct |
511 ms |
35372 KB |
Output is correct |
4 |
Correct |
457 ms |
35372 KB |
Output is correct |
5 |
Correct |
461 ms |
35864 KB |
Output is correct |
6 |
Correct |
472 ms |
36500 KB |
Output is correct |
7 |
Correct |
558 ms |
36648 KB |
Output is correct |
8 |
Correct |
497 ms |
41320 KB |
Output is correct |
9 |
Correct |
488 ms |
41324 KB |
Output is correct |
10 |
Correct |
481 ms |
35704 KB |
Output is correct |
11 |
Correct |
558 ms |
37888 KB |
Output is correct |
12 |
Correct |
339 ms |
35884 KB |
Output is correct |
13 |
Correct |
25 ms |
20644 KB |
Output is correct |
14 |
Correct |
43 ms |
20692 KB |
Output is correct |
15 |
Correct |
16 ms |
20820 KB |
Output is correct |
16 |
Correct |
16 ms |
20800 KB |
Output is correct |
17 |
Correct |
19 ms |
20772 KB |
Output is correct |
18 |
Correct |
17 ms |
20948 KB |
Output is correct |
19 |
Correct |
19 ms |
20900 KB |
Output is correct |
20 |
Correct |
16 ms |
20948 KB |
Output is correct |
21 |
Correct |
16 ms |
20820 KB |
Output is correct |
22 |
Correct |
23 ms |
20644 KB |
Output is correct |
23 |
Correct |
23 ms |
20716 KB |
Output is correct |
24 |
Correct |
17 ms |
20820 KB |
Output is correct |
25 |
Correct |
18 ms |
20820 KB |
Output is correct |
26 |
Correct |
20 ms |
20820 KB |
Output is correct |
27 |
Correct |
17 ms |
20828 KB |
Output is correct |
28 |
Correct |
19 ms |
20868 KB |
Output is correct |
29 |
Correct |
24 ms |
20876 KB |
Output is correct |
30 |
Correct |
15 ms |
20844 KB |
Output is correct |
31 |
Correct |
255 ms |
21976 KB |
Output is correct |
32 |
Correct |
217 ms |
21836 KB |
Output is correct |
33 |
Correct |
236 ms |
22176 KB |
Output is correct |
34 |
Correct |
203 ms |
22216 KB |
Output is correct |
35 |
Correct |
219 ms |
22336 KB |
Output is correct |
36 |
Correct |
251 ms |
22284 KB |
Output is correct |
37 |
Correct |
206 ms |
21996 KB |
Output is correct |
38 |
Correct |
14 ms |
20692 KB |
Output is correct |
39 |
Correct |
14 ms |
20748 KB |
Output is correct |
40 |
Correct |
16 ms |
20836 KB |
Output is correct |
41 |
Correct |
16 ms |
20892 KB |
Output is correct |
42 |
Correct |
17 ms |
20812 KB |
Output is correct |
43 |
Correct |
18 ms |
20888 KB |
Output is correct |
44 |
Correct |
18 ms |
20868 KB |
Output is correct |
45 |
Correct |
15 ms |
20948 KB |
Output is correct |
46 |
Correct |
18 ms |
20800 KB |
Output is correct |
47 |
Correct |
192 ms |
34776 KB |
Output is correct |
48 |
Correct |
179 ms |
34732 KB |
Output is correct |
49 |
Correct |
180 ms |
34776 KB |
Output is correct |
50 |
Correct |
225 ms |
36748 KB |
Output is correct |
51 |
Correct |
317 ms |
39864 KB |
Output is correct |
52 |
Correct |
272 ms |
40196 KB |
Output is correct |
53 |
Correct |
101 ms |
29212 KB |
Output is correct |
54 |
Correct |
400 ms |
35396 KB |
Output is correct |
55 |
Correct |
557 ms |
35356 KB |
Output is correct |
56 |
Correct |
505 ms |
35356 KB |
Output is correct |
57 |
Correct |
407 ms |
41348 KB |
Output is correct |
58 |
Correct |
482 ms |
35676 KB |
Output is correct |
59 |
Correct |
468 ms |
40980 KB |
Output is correct |
60 |
Correct |
434 ms |
40888 KB |
Output is correct |
61 |
Correct |
498 ms |
41044 KB |
Output is correct |
62 |
Correct |
230 ms |
39788 KB |
Output is correct |
63 |
Correct |
478 ms |
40668 KB |
Output is correct |
64 |
Correct |
470 ms |
40452 KB |
Output is correct |
65 |
Correct |
417 ms |
40720 KB |
Output is correct |
66 |
Correct |
14 ms |
20692 KB |
Output is correct |
67 |
Correct |
351 ms |
35472 KB |
Output is correct |
68 |
Correct |
387 ms |
35368 KB |
Output is correct |
69 |
Correct |
523 ms |
35348 KB |
Output is correct |
70 |
Correct |
391 ms |
35936 KB |
Output is correct |
71 |
Correct |
466 ms |
36652 KB |
Output is correct |
72 |
Correct |
390 ms |
36652 KB |
Output is correct |
73 |
Correct |
542 ms |
41260 KB |
Output is correct |
74 |
Correct |
465 ms |
41276 KB |
Output is correct |
75 |
Correct |
477 ms |
35704 KB |
Output is correct |
76 |
Correct |
554 ms |
37884 KB |
Output is correct |
77 |
Correct |
330 ms |
35832 KB |
Output is correct |
78 |
Correct |
22 ms |
20692 KB |
Output is correct |
79 |
Correct |
16 ms |
20820 KB |
Output is correct |
80 |
Correct |
15 ms |
20840 KB |
Output is correct |
81 |
Correct |
16 ms |
20824 KB |
Output is correct |
82 |
Correct |
17 ms |
20820 KB |
Output is correct |
83 |
Correct |
22 ms |
20948 KB |
Output is correct |
84 |
Correct |
16 ms |
20820 KB |
Output is correct |
85 |
Correct |
17 ms |
20756 KB |
Output is correct |
86 |
Correct |
245 ms |
21976 KB |
Output is correct |
87 |
Correct |
208 ms |
22028 KB |
Output is correct |
88 |
Correct |
252 ms |
22200 KB |
Output is correct |
89 |
Correct |
200 ms |
22252 KB |
Output is correct |
90 |
Correct |
197 ms |
22348 KB |
Output is correct |
91 |
Correct |
235 ms |
22324 KB |
Output is correct |
92 |
Correct |
200 ms |
22004 KB |
Output is correct |
93 |
Correct |
200 ms |
34792 KB |
Output is correct |
94 |
Correct |
158 ms |
34764 KB |
Output is correct |
95 |
Correct |
183 ms |
34720 KB |
Output is correct |
96 |
Correct |
179 ms |
36772 KB |
Output is correct |
97 |
Correct |
249 ms |
39812 KB |
Output is correct |
98 |
Correct |
231 ms |
40268 KB |
Output is correct |
99 |
Correct |
73 ms |
29200 KB |
Output is correct |
100 |
Correct |
468 ms |
40936 KB |
Output is correct |
101 |
Correct |
461 ms |
40972 KB |
Output is correct |
102 |
Correct |
479 ms |
41028 KB |
Output is correct |
103 |
Correct |
428 ms |
40640 KB |
Output is correct |
104 |
Correct |
457 ms |
40256 KB |
Output is correct |
105 |
Correct |
469 ms |
40648 KB |
Output is correct |
106 |
Correct |
354 ms |
34124 KB |
Output is correct |
107 |
Correct |
381 ms |
35696 KB |
Output is correct |
108 |
Correct |
394 ms |
35516 KB |
Output is correct |
109 |
Correct |
371 ms |
35612 KB |
Output is correct |
110 |
Correct |
413 ms |
40920 KB |
Output is correct |
111 |
Correct |
478 ms |
40920 KB |
Output is correct |