#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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20692 KB |
Output is correct |
2 |
Correct |
377 ms |
35380 KB |
Output is correct |
3 |
Correct |
418 ms |
35272 KB |
Output is correct |
4 |
Correct |
403 ms |
35480 KB |
Output is correct |
5 |
Correct |
375 ms |
35988 KB |
Output is correct |
6 |
Correct |
430 ms |
36512 KB |
Output is correct |
7 |
Correct |
374 ms |
36708 KB |
Output is correct |
8 |
Correct |
421 ms |
41576 KB |
Output is correct |
9 |
Correct |
381 ms |
41308 KB |
Output is correct |
10 |
Correct |
377 ms |
35748 KB |
Output is correct |
11 |
Correct |
429 ms |
37932 KB |
Output is correct |
12 |
Correct |
291 ms |
35796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
20692 KB |
Output is correct |
2 |
Correct |
17 ms |
20692 KB |
Output is correct |
3 |
Correct |
16 ms |
20784 KB |
Output is correct |
4 |
Correct |
16 ms |
20820 KB |
Output is correct |
5 |
Correct |
15 ms |
20820 KB |
Output is correct |
6 |
Correct |
15 ms |
20828 KB |
Output is correct |
7 |
Correct |
16 ms |
20948 KB |
Output is correct |
8 |
Correct |
16 ms |
20944 KB |
Output is correct |
9 |
Correct |
15 ms |
20724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
20692 KB |
Output is correct |
2 |
Correct |
17 ms |
20692 KB |
Output is correct |
3 |
Correct |
16 ms |
20784 KB |
Output is correct |
4 |
Correct |
16 ms |
20820 KB |
Output is correct |
5 |
Correct |
15 ms |
20820 KB |
Output is correct |
6 |
Correct |
15 ms |
20828 KB |
Output is correct |
7 |
Correct |
16 ms |
20948 KB |
Output is correct |
8 |
Correct |
16 ms |
20944 KB |
Output is correct |
9 |
Correct |
15 ms |
20724 KB |
Output is correct |
10 |
Correct |
13 ms |
20696 KB |
Output is correct |
11 |
Correct |
19 ms |
20684 KB |
Output is correct |
12 |
Correct |
21 ms |
20864 KB |
Output is correct |
13 |
Correct |
20 ms |
20828 KB |
Output is correct |
14 |
Correct |
15 ms |
20820 KB |
Output is correct |
15 |
Correct |
16 ms |
20820 KB |
Output is correct |
16 |
Correct |
15 ms |
20888 KB |
Output is correct |
17 |
Correct |
16 ms |
20860 KB |
Output is correct |
18 |
Correct |
14 ms |
20820 KB |
Output is correct |
19 |
Correct |
195 ms |
21928 KB |
Output is correct |
20 |
Correct |
211 ms |
21836 KB |
Output is correct |
21 |
Correct |
197 ms |
22164 KB |
Output is correct |
22 |
Correct |
195 ms |
22216 KB |
Output is correct |
23 |
Correct |
206 ms |
22260 KB |
Output is correct |
24 |
Correct |
190 ms |
22348 KB |
Output is correct |
25 |
Correct |
205 ms |
21892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
20692 KB |
Output is correct |
2 |
Correct |
17 ms |
20692 KB |
Output is correct |
3 |
Correct |
16 ms |
20784 KB |
Output is correct |
4 |
Correct |
16 ms |
20820 KB |
Output is correct |
5 |
Correct |
15 ms |
20820 KB |
Output is correct |
6 |
Correct |
15 ms |
20828 KB |
Output is correct |
7 |
Correct |
16 ms |
20948 KB |
Output is correct |
8 |
Correct |
16 ms |
20944 KB |
Output is correct |
9 |
Correct |
15 ms |
20724 KB |
Output is correct |
10 |
Correct |
14 ms |
20692 KB |
Output is correct |
11 |
Correct |
16 ms |
20740 KB |
Output is correct |
12 |
Correct |
15 ms |
20840 KB |
Output is correct |
13 |
Correct |
15 ms |
20884 KB |
Output is correct |
14 |
Correct |
20 ms |
20844 KB |
Output is correct |
15 |
Correct |
16 ms |
20896 KB |
Output is correct |
16 |
Correct |
16 ms |
20900 KB |
Output is correct |
17 |
Correct |
21 ms |
20852 KB |
Output is correct |
18 |
Correct |
17 ms |
20820 KB |
Output is correct |
19 |
Correct |
197 ms |
34680 KB |
Output is correct |
20 |
Correct |
150 ms |
34736 KB |
Output is correct |
21 |
Correct |
176 ms |
34764 KB |
Output is correct |
22 |
Correct |
216 ms |
36688 KB |
Output is correct |
23 |
Correct |
239 ms |
39856 KB |
Output is correct |
24 |
Correct |
279 ms |
40264 KB |
Output is correct |
25 |
Correct |
74 ms |
29404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
381 ms |
35544 KB |
Output is correct |
2 |
Correct |
384 ms |
35356 KB |
Output is correct |
3 |
Correct |
457 ms |
35404 KB |
Output is correct |
4 |
Correct |
462 ms |
41364 KB |
Output is correct |
5 |
Correct |
407 ms |
35664 KB |
Output is correct |
6 |
Correct |
493 ms |
40996 KB |
Output is correct |
7 |
Correct |
525 ms |
41004 KB |
Output is correct |
8 |
Correct |
464 ms |
41068 KB |
Output is correct |
9 |
Correct |
267 ms |
39796 KB |
Output is correct |
10 |
Correct |
459 ms |
40612 KB |
Output is correct |
11 |
Correct |
467 ms |
40444 KB |
Output is correct |
12 |
Correct |
463 ms |
40556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20692 KB |
Output is correct |
2 |
Correct |
377 ms |
35380 KB |
Output is correct |
3 |
Correct |
418 ms |
35272 KB |
Output is correct |
4 |
Correct |
403 ms |
35480 KB |
Output is correct |
5 |
Correct |
375 ms |
35988 KB |
Output is correct |
6 |
Correct |
430 ms |
36512 KB |
Output is correct |
7 |
Correct |
374 ms |
36708 KB |
Output is correct |
8 |
Correct |
421 ms |
41576 KB |
Output is correct |
9 |
Correct |
381 ms |
41308 KB |
Output is correct |
10 |
Correct |
377 ms |
35748 KB |
Output is correct |
11 |
Correct |
429 ms |
37932 KB |
Output is correct |
12 |
Correct |
291 ms |
35796 KB |
Output is correct |
13 |
Correct |
15 ms |
20692 KB |
Output is correct |
14 |
Correct |
17 ms |
20692 KB |
Output is correct |
15 |
Correct |
16 ms |
20784 KB |
Output is correct |
16 |
Correct |
16 ms |
20820 KB |
Output is correct |
17 |
Correct |
15 ms |
20820 KB |
Output is correct |
18 |
Correct |
15 ms |
20828 KB |
Output is correct |
19 |
Correct |
16 ms |
20948 KB |
Output is correct |
20 |
Correct |
16 ms |
20944 KB |
Output is correct |
21 |
Correct |
15 ms |
20724 KB |
Output is correct |
22 |
Correct |
13 ms |
20696 KB |
Output is correct |
23 |
Correct |
19 ms |
20684 KB |
Output is correct |
24 |
Correct |
21 ms |
20864 KB |
Output is correct |
25 |
Correct |
20 ms |
20828 KB |
Output is correct |
26 |
Correct |
15 ms |
20820 KB |
Output is correct |
27 |
Correct |
16 ms |
20820 KB |
Output is correct |
28 |
Correct |
15 ms |
20888 KB |
Output is correct |
29 |
Correct |
16 ms |
20860 KB |
Output is correct |
30 |
Correct |
14 ms |
20820 KB |
Output is correct |
31 |
Correct |
195 ms |
21928 KB |
Output is correct |
32 |
Correct |
211 ms |
21836 KB |
Output is correct |
33 |
Correct |
197 ms |
22164 KB |
Output is correct |
34 |
Correct |
195 ms |
22216 KB |
Output is correct |
35 |
Correct |
206 ms |
22260 KB |
Output is correct |
36 |
Correct |
190 ms |
22348 KB |
Output is correct |
37 |
Correct |
205 ms |
21892 KB |
Output is correct |
38 |
Correct |
14 ms |
20692 KB |
Output is correct |
39 |
Correct |
16 ms |
20740 KB |
Output is correct |
40 |
Correct |
15 ms |
20840 KB |
Output is correct |
41 |
Correct |
15 ms |
20884 KB |
Output is correct |
42 |
Correct |
20 ms |
20844 KB |
Output is correct |
43 |
Correct |
16 ms |
20896 KB |
Output is correct |
44 |
Correct |
16 ms |
20900 KB |
Output is correct |
45 |
Correct |
21 ms |
20852 KB |
Output is correct |
46 |
Correct |
17 ms |
20820 KB |
Output is correct |
47 |
Correct |
197 ms |
34680 KB |
Output is correct |
48 |
Correct |
150 ms |
34736 KB |
Output is correct |
49 |
Correct |
176 ms |
34764 KB |
Output is correct |
50 |
Correct |
216 ms |
36688 KB |
Output is correct |
51 |
Correct |
239 ms |
39856 KB |
Output is correct |
52 |
Correct |
279 ms |
40264 KB |
Output is correct |
53 |
Correct |
74 ms |
29404 KB |
Output is correct |
54 |
Correct |
381 ms |
35544 KB |
Output is correct |
55 |
Correct |
384 ms |
35356 KB |
Output is correct |
56 |
Correct |
457 ms |
35404 KB |
Output is correct |
57 |
Correct |
462 ms |
41364 KB |
Output is correct |
58 |
Correct |
407 ms |
35664 KB |
Output is correct |
59 |
Correct |
493 ms |
40996 KB |
Output is correct |
60 |
Correct |
525 ms |
41004 KB |
Output is correct |
61 |
Correct |
464 ms |
41068 KB |
Output is correct |
62 |
Correct |
267 ms |
39796 KB |
Output is correct |
63 |
Correct |
459 ms |
40612 KB |
Output is correct |
64 |
Correct |
467 ms |
40444 KB |
Output is correct |
65 |
Correct |
463 ms |
40556 KB |
Output is correct |
66 |
Correct |
21 ms |
20692 KB |
Output is correct |
67 |
Correct |
402 ms |
35312 KB |
Output is correct |
68 |
Correct |
453 ms |
35368 KB |
Output is correct |
69 |
Correct |
440 ms |
35264 KB |
Output is correct |
70 |
Correct |
443 ms |
35944 KB |
Output is correct |
71 |
Correct |
471 ms |
36588 KB |
Output is correct |
72 |
Correct |
432 ms |
36596 KB |
Output is correct |
73 |
Correct |
456 ms |
41420 KB |
Output is correct |
74 |
Correct |
458 ms |
41300 KB |
Output is correct |
75 |
Correct |
468 ms |
35720 KB |
Output is correct |
76 |
Correct |
534 ms |
37884 KB |
Output is correct |
77 |
Correct |
341 ms |
35956 KB |
Output is correct |
78 |
Correct |
16 ms |
20736 KB |
Output is correct |
79 |
Correct |
18 ms |
20804 KB |
Output is correct |
80 |
Correct |
16 ms |
20820 KB |
Output is correct |
81 |
Correct |
18 ms |
20820 KB |
Output is correct |
82 |
Correct |
16 ms |
20880 KB |
Output is correct |
83 |
Correct |
17 ms |
20948 KB |
Output is correct |
84 |
Correct |
16 ms |
20884 KB |
Output is correct |
85 |
Correct |
22 ms |
20816 KB |
Output is correct |
86 |
Correct |
291 ms |
22056 KB |
Output is correct |
87 |
Correct |
221 ms |
21880 KB |
Output is correct |
88 |
Correct |
266 ms |
22280 KB |
Output is correct |
89 |
Correct |
242 ms |
22160 KB |
Output is correct |
90 |
Correct |
268 ms |
22300 KB |
Output is correct |
91 |
Correct |
207 ms |
22360 KB |
Output is correct |
92 |
Correct |
259 ms |
21984 KB |
Output is correct |
93 |
Correct |
186 ms |
34708 KB |
Output is correct |
94 |
Correct |
183 ms |
34740 KB |
Output is correct |
95 |
Correct |
198 ms |
34732 KB |
Output is correct |
96 |
Correct |
211 ms |
36892 KB |
Output is correct |
97 |
Correct |
280 ms |
39892 KB |
Output is correct |
98 |
Correct |
344 ms |
40192 KB |
Output is correct |
99 |
Correct |
80 ms |
29220 KB |
Output is correct |
100 |
Correct |
592 ms |
41016 KB |
Output is correct |
101 |
Correct |
564 ms |
40932 KB |
Output is correct |
102 |
Correct |
567 ms |
41124 KB |
Output is correct |
103 |
Correct |
510 ms |
40584 KB |
Output is correct |
104 |
Correct |
490 ms |
40352 KB |
Output is correct |
105 |
Correct |
608 ms |
40660 KB |
Output is correct |
106 |
Correct |
403 ms |
34068 KB |
Output is correct |
107 |
Correct |
432 ms |
35588 KB |
Output is correct |
108 |
Correct |
416 ms |
35556 KB |
Output is correct |
109 |
Correct |
413 ms |
35596 KB |
Output is correct |
110 |
Correct |
573 ms |
40844 KB |
Output is correct |
111 |
Correct |
613 ms |
40920 KB |
Output is correct |