#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];
ans++;
if(e == 0) cout << "impossible" << endl;
else cout << ans + 1 << endl;
}
}
int main(){
int t = 1;
//cin>>t;
for(int i = 1; i <= t; i++){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
20692 KB |
Output is correct |
2 |
Correct |
374 ms |
35308 KB |
Output is correct |
3 |
Correct |
406 ms |
35320 KB |
Output is correct |
4 |
Correct |
506 ms |
35320 KB |
Output is correct |
5 |
Correct |
450 ms |
39052 KB |
Output is correct |
6 |
Correct |
385 ms |
39560 KB |
Output is correct |
7 |
Correct |
386 ms |
39756 KB |
Output is correct |
8 |
Correct |
388 ms |
44424 KB |
Output is correct |
9 |
Correct |
450 ms |
44400 KB |
Output is correct |
10 |
Correct |
396 ms |
38752 KB |
Output is correct |
11 |
Correct |
407 ms |
41076 KB |
Output is correct |
12 |
Correct |
291 ms |
38196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20636 KB |
Output is correct |
2 |
Correct |
14 ms |
20752 KB |
Output is correct |
3 |
Correct |
17 ms |
20844 KB |
Output is correct |
4 |
Correct |
18 ms |
20832 KB |
Output is correct |
5 |
Correct |
16 ms |
20860 KB |
Output is correct |
6 |
Correct |
17 ms |
20820 KB |
Output is correct |
7 |
Correct |
16 ms |
20924 KB |
Output is correct |
8 |
Correct |
16 ms |
20856 KB |
Output is correct |
9 |
Correct |
16 ms |
20856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20636 KB |
Output is correct |
2 |
Correct |
14 ms |
20752 KB |
Output is correct |
3 |
Correct |
17 ms |
20844 KB |
Output is correct |
4 |
Correct |
18 ms |
20832 KB |
Output is correct |
5 |
Correct |
16 ms |
20860 KB |
Output is correct |
6 |
Correct |
17 ms |
20820 KB |
Output is correct |
7 |
Correct |
16 ms |
20924 KB |
Output is correct |
8 |
Correct |
16 ms |
20856 KB |
Output is correct |
9 |
Correct |
16 ms |
20856 KB |
Output is correct |
10 |
Correct |
14 ms |
20692 KB |
Output is correct |
11 |
Correct |
15 ms |
20676 KB |
Output is correct |
12 |
Correct |
16 ms |
20820 KB |
Output is correct |
13 |
Correct |
16 ms |
20804 KB |
Output is correct |
14 |
Correct |
16 ms |
20868 KB |
Output is correct |
15 |
Correct |
16 ms |
20812 KB |
Output is correct |
16 |
Correct |
19 ms |
20940 KB |
Output is correct |
17 |
Correct |
16 ms |
20948 KB |
Output is correct |
18 |
Correct |
15 ms |
20820 KB |
Output is correct |
19 |
Correct |
194 ms |
22956 KB |
Output is correct |
20 |
Correct |
193 ms |
22860 KB |
Output is correct |
21 |
Correct |
196 ms |
23100 KB |
Output is correct |
22 |
Correct |
193 ms |
23244 KB |
Output is correct |
23 |
Correct |
191 ms |
23292 KB |
Output is correct |
24 |
Correct |
192 ms |
23324 KB |
Output is correct |
25 |
Correct |
186 ms |
22976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20636 KB |
Output is correct |
2 |
Correct |
14 ms |
20752 KB |
Output is correct |
3 |
Correct |
17 ms |
20844 KB |
Output is correct |
4 |
Correct |
18 ms |
20832 KB |
Output is correct |
5 |
Correct |
16 ms |
20860 KB |
Output is correct |
6 |
Correct |
17 ms |
20820 KB |
Output is correct |
7 |
Correct |
16 ms |
20924 KB |
Output is correct |
8 |
Correct |
16 ms |
20856 KB |
Output is correct |
9 |
Correct |
16 ms |
20856 KB |
Output is correct |
10 |
Correct |
15 ms |
20692 KB |
Output is correct |
11 |
Correct |
14 ms |
20756 KB |
Output is correct |
12 |
Correct |
17 ms |
20948 KB |
Output is correct |
13 |
Correct |
16 ms |
20872 KB |
Output is correct |
14 |
Correct |
15 ms |
20876 KB |
Output is correct |
15 |
Correct |
18 ms |
20804 KB |
Output is correct |
16 |
Correct |
15 ms |
20944 KB |
Output is correct |
17 |
Correct |
16 ms |
20928 KB |
Output is correct |
18 |
Correct |
15 ms |
20800 KB |
Output is correct |
19 |
Correct |
166 ms |
36676 KB |
Output is correct |
20 |
Correct |
139 ms |
35820 KB |
Output is correct |
21 |
Correct |
174 ms |
36644 KB |
Output is correct |
22 |
Correct |
194 ms |
38648 KB |
Output is correct |
23 |
Correct |
227 ms |
41804 KB |
Output is correct |
24 |
Correct |
251 ms |
42180 KB |
Output is correct |
25 |
Correct |
71 ms |
30532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
351 ms |
35280 KB |
Output is correct |
2 |
Correct |
376 ms |
35404 KB |
Output is correct |
3 |
Correct |
436 ms |
35276 KB |
Output is correct |
4 |
Correct |
399 ms |
44352 KB |
Output is correct |
5 |
Correct |
394 ms |
38860 KB |
Output is correct |
6 |
Correct |
463 ms |
44184 KB |
Output is correct |
7 |
Correct |
475 ms |
44200 KB |
Output is correct |
8 |
Correct |
461 ms |
44124 KB |
Output is correct |
9 |
Correct |
237 ms |
41780 KB |
Output is correct |
10 |
Correct |
435 ms |
43656 KB |
Output is correct |
11 |
Correct |
438 ms |
43548 KB |
Output is correct |
12 |
Correct |
412 ms |
43728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
20692 KB |
Output is correct |
2 |
Correct |
374 ms |
35308 KB |
Output is correct |
3 |
Correct |
406 ms |
35320 KB |
Output is correct |
4 |
Correct |
506 ms |
35320 KB |
Output is correct |
5 |
Correct |
450 ms |
39052 KB |
Output is correct |
6 |
Correct |
385 ms |
39560 KB |
Output is correct |
7 |
Correct |
386 ms |
39756 KB |
Output is correct |
8 |
Correct |
388 ms |
44424 KB |
Output is correct |
9 |
Correct |
450 ms |
44400 KB |
Output is correct |
10 |
Correct |
396 ms |
38752 KB |
Output is correct |
11 |
Correct |
407 ms |
41076 KB |
Output is correct |
12 |
Correct |
291 ms |
38196 KB |
Output is correct |
13 |
Correct |
14 ms |
20636 KB |
Output is correct |
14 |
Correct |
14 ms |
20752 KB |
Output is correct |
15 |
Correct |
17 ms |
20844 KB |
Output is correct |
16 |
Correct |
18 ms |
20832 KB |
Output is correct |
17 |
Correct |
16 ms |
20860 KB |
Output is correct |
18 |
Correct |
17 ms |
20820 KB |
Output is correct |
19 |
Correct |
16 ms |
20924 KB |
Output is correct |
20 |
Correct |
16 ms |
20856 KB |
Output is correct |
21 |
Correct |
16 ms |
20856 KB |
Output is correct |
22 |
Correct |
14 ms |
20692 KB |
Output is correct |
23 |
Correct |
15 ms |
20676 KB |
Output is correct |
24 |
Correct |
16 ms |
20820 KB |
Output is correct |
25 |
Correct |
16 ms |
20804 KB |
Output is correct |
26 |
Correct |
16 ms |
20868 KB |
Output is correct |
27 |
Correct |
16 ms |
20812 KB |
Output is correct |
28 |
Correct |
19 ms |
20940 KB |
Output is correct |
29 |
Correct |
16 ms |
20948 KB |
Output is correct |
30 |
Correct |
15 ms |
20820 KB |
Output is correct |
31 |
Correct |
194 ms |
22956 KB |
Output is correct |
32 |
Correct |
193 ms |
22860 KB |
Output is correct |
33 |
Correct |
196 ms |
23100 KB |
Output is correct |
34 |
Correct |
193 ms |
23244 KB |
Output is correct |
35 |
Correct |
191 ms |
23292 KB |
Output is correct |
36 |
Correct |
192 ms |
23324 KB |
Output is correct |
37 |
Correct |
186 ms |
22976 KB |
Output is correct |
38 |
Correct |
15 ms |
20692 KB |
Output is correct |
39 |
Correct |
14 ms |
20756 KB |
Output is correct |
40 |
Correct |
17 ms |
20948 KB |
Output is correct |
41 |
Correct |
16 ms |
20872 KB |
Output is correct |
42 |
Correct |
15 ms |
20876 KB |
Output is correct |
43 |
Correct |
18 ms |
20804 KB |
Output is correct |
44 |
Correct |
15 ms |
20944 KB |
Output is correct |
45 |
Correct |
16 ms |
20928 KB |
Output is correct |
46 |
Correct |
15 ms |
20800 KB |
Output is correct |
47 |
Correct |
166 ms |
36676 KB |
Output is correct |
48 |
Correct |
139 ms |
35820 KB |
Output is correct |
49 |
Correct |
174 ms |
36644 KB |
Output is correct |
50 |
Correct |
194 ms |
38648 KB |
Output is correct |
51 |
Correct |
227 ms |
41804 KB |
Output is correct |
52 |
Correct |
251 ms |
42180 KB |
Output is correct |
53 |
Correct |
71 ms |
30532 KB |
Output is correct |
54 |
Correct |
351 ms |
35280 KB |
Output is correct |
55 |
Correct |
376 ms |
35404 KB |
Output is correct |
56 |
Correct |
436 ms |
35276 KB |
Output is correct |
57 |
Correct |
399 ms |
44352 KB |
Output is correct |
58 |
Correct |
394 ms |
38860 KB |
Output is correct |
59 |
Correct |
463 ms |
44184 KB |
Output is correct |
60 |
Correct |
475 ms |
44200 KB |
Output is correct |
61 |
Correct |
461 ms |
44124 KB |
Output is correct |
62 |
Correct |
237 ms |
41780 KB |
Output is correct |
63 |
Correct |
435 ms |
43656 KB |
Output is correct |
64 |
Correct |
438 ms |
43548 KB |
Output is correct |
65 |
Correct |
412 ms |
43728 KB |
Output is correct |
66 |
Correct |
13 ms |
20692 KB |
Output is correct |
67 |
Correct |
347 ms |
38420 KB |
Output is correct |
68 |
Correct |
368 ms |
38416 KB |
Output is correct |
69 |
Correct |
414 ms |
38476 KB |
Output is correct |
70 |
Correct |
387 ms |
38996 KB |
Output is correct |
71 |
Correct |
382 ms |
39564 KB |
Output is correct |
72 |
Correct |
378 ms |
39904 KB |
Output is correct |
73 |
Correct |
392 ms |
44452 KB |
Output is correct |
74 |
Correct |
385 ms |
44424 KB |
Output is correct |
75 |
Correct |
390 ms |
38752 KB |
Output is correct |
76 |
Correct |
402 ms |
41112 KB |
Output is correct |
77 |
Correct |
283 ms |
38112 KB |
Output is correct |
78 |
Correct |
14 ms |
20656 KB |
Output is correct |
79 |
Correct |
18 ms |
20820 KB |
Output is correct |
80 |
Correct |
15 ms |
20848 KB |
Output is correct |
81 |
Correct |
15 ms |
20792 KB |
Output is correct |
82 |
Correct |
16 ms |
20800 KB |
Output is correct |
83 |
Correct |
18 ms |
20956 KB |
Output is correct |
84 |
Correct |
18 ms |
20952 KB |
Output is correct |
85 |
Correct |
15 ms |
20744 KB |
Output is correct |
86 |
Correct |
193 ms |
22988 KB |
Output is correct |
87 |
Correct |
197 ms |
22888 KB |
Output is correct |
88 |
Correct |
193 ms |
23092 KB |
Output is correct |
89 |
Correct |
185 ms |
23244 KB |
Output is correct |
90 |
Correct |
193 ms |
23360 KB |
Output is correct |
91 |
Correct |
185 ms |
23256 KB |
Output is correct |
92 |
Correct |
185 ms |
23028 KB |
Output is correct |
93 |
Correct |
171 ms |
36808 KB |
Output is correct |
94 |
Correct |
154 ms |
35956 KB |
Output is correct |
95 |
Correct |
174 ms |
36552 KB |
Output is correct |
96 |
Correct |
192 ms |
38756 KB |
Output is correct |
97 |
Correct |
211 ms |
41776 KB |
Output is correct |
98 |
Correct |
244 ms |
42160 KB |
Output is correct |
99 |
Correct |
72 ms |
30500 KB |
Output is correct |
100 |
Correct |
431 ms |
44044 KB |
Output is correct |
101 |
Correct |
428 ms |
44012 KB |
Output is correct |
102 |
Correct |
430 ms |
44120 KB |
Output is correct |
103 |
Correct |
420 ms |
43684 KB |
Output is correct |
104 |
Correct |
423 ms |
43412 KB |
Output is correct |
105 |
Correct |
431 ms |
43800 KB |
Output is correct |
106 |
Correct |
341 ms |
36548 KB |
Output is correct |
107 |
Correct |
423 ms |
37956 KB |
Output is correct |
108 |
Correct |
365 ms |
38576 KB |
Output is correct |
109 |
Correct |
366 ms |
38604 KB |
Output is correct |
110 |
Correct |
445 ms |
43988 KB |
Output is correct |
111 |
Correct |
441 ms |
43956 KB |
Output is correct |