#include "bits/stdc++.h"
#define intt long long
#define pb push_back
#define endl '\n'
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<intt,intt>
#define ld long double
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int sz = 2e5+5;
const int inf = 1e9+7;
int a[sz], b[sz];
map<int,int> mp;
pii st[sz<<2];
int par[sz][20];
void build(int l, int r, int in){
st[in]={inf, inf};
if ( l == r ){
return;
}
int mid = (l+r)>>1;
build(l, mid, in<<1);
build(mid+1, r, in<<1|1);
}
void update(int l, int r, int in, int x, int val){
if ( x > r or x < l ) return;
if ( l == r ){
st[in] = min(st[in], {a[val], val});
return;
}
int mid = (l+r)>>1;
update(l, mid, in<<1, x, val);
update(mid+1, r, in<<1|1, x, val);
st[in] = min(st[in<<1], st[in<<1|1]);
}
pii getans(int a, int b, int l, int r, int in){
if ( l > b or r < a ) return {inf, inf};
if ( a <= l and r <= b ) return st[in];
int mid = (l+r)>>1;
return min(getans(a, b, l, mid, in<<1), getans(a, b, mid+1, r, in<<1|1));
}
int i,j;
int main(){
int n, q;
cin >> n >> q;
for ( i = 1; i <= n; i++ ){
cin >> a[i] >> b[i];
mp[a[i]];
mp[b[i]];
}
int cnt = 0;
for ( pii i : mp ){
mp[i.F] = ++cnt;
}
build(1, n*2, 1);
for ( i = 1; i <= n; i++ ){
a[i] = mp[a[i]];
b[i] = mp[b[i]];
update(1, n*2, 1, b[i], i);
}
for ( i = 1; i <= n; i++ ){
par[i][0] = getans(a[i], b[i], 1, n*2, 1).S;
if ( par[i][0] == i ) par[i][0] = 0;
}
for ( i = 1; i < 20; i++ ){
for ( j = 1; j <= n; j++ ) par[j][i] = par[par[j][i-1]][i-1];
}
while(q--){
int x, y;
cin >> x >> y;
if ( x == y ){
cout << 0 << endl;
continue;
}
if ( b[x] > b[y] ){
cout << "impossible" << endl;
continue;
}
if ( b[x] >= a[y] and b[x] <= b[y] ){
cout << 1 << endl;
continue;
}
int res = 0;
for ( i = 19; i >= 0; i-- ){
if ( a[par[y][i]] > b[x] ){
y = par[y][i];
res+=(1<<i);
}
}
y = par[y][0];
res++;
if ( y == 0 ){
cout << "impossible" << endl;
continue;
}
cout << res+1 << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
484 ms |
18416 KB |
Output is correct |
3 |
Correct |
447 ms |
18288 KB |
Output is correct |
4 |
Correct |
575 ms |
18320 KB |
Output is correct |
5 |
Correct |
447 ms |
18876 KB |
Output is correct |
6 |
Correct |
480 ms |
19272 KB |
Output is correct |
7 |
Correct |
428 ms |
19540 KB |
Output is correct |
8 |
Correct |
483 ms |
23652 KB |
Output is correct |
9 |
Correct |
568 ms |
23488 KB |
Output is correct |
10 |
Correct |
525 ms |
18652 KB |
Output is correct |
11 |
Correct |
491 ms |
20620 KB |
Output is correct |
12 |
Correct |
335 ms |
18852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
4 ms |
448 KB |
Output is correct |
14 |
Correct |
2 ms |
448 KB |
Output is correct |
15 |
Correct |
3 ms |
456 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
229 ms |
1940 KB |
Output is correct |
20 |
Correct |
258 ms |
1828 KB |
Output is correct |
21 |
Correct |
221 ms |
2008 KB |
Output is correct |
22 |
Correct |
241 ms |
2028 KB |
Output is correct |
23 |
Correct |
204 ms |
2212 KB |
Output is correct |
24 |
Correct |
258 ms |
2096 KB |
Output is correct |
25 |
Correct |
231 ms |
1832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
452 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
448 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
8 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
290 ms |
17776 KB |
Output is correct |
20 |
Correct |
278 ms |
17740 KB |
Output is correct |
21 |
Correct |
276 ms |
17744 KB |
Output is correct |
22 |
Correct |
301 ms |
19492 KB |
Output is correct |
23 |
Correct |
345 ms |
22472 KB |
Output is correct |
24 |
Correct |
350 ms |
22476 KB |
Output is correct |
25 |
Correct |
91 ms |
12988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
448 ms |
18616 KB |
Output is correct |
2 |
Correct |
440 ms |
18388 KB |
Output is correct |
3 |
Correct |
477 ms |
18300 KB |
Output is correct |
4 |
Correct |
482 ms |
23564 KB |
Output is correct |
5 |
Correct |
530 ms |
18664 KB |
Output is correct |
6 |
Correct |
633 ms |
23188 KB |
Output is correct |
7 |
Correct |
621 ms |
23252 KB |
Output is correct |
8 |
Correct |
590 ms |
23352 KB |
Output is correct |
9 |
Correct |
385 ms |
22568 KB |
Output is correct |
10 |
Correct |
533 ms |
22884 KB |
Output is correct |
11 |
Correct |
558 ms |
22664 KB |
Output is correct |
12 |
Correct |
631 ms |
22796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
484 ms |
18416 KB |
Output is correct |
3 |
Correct |
447 ms |
18288 KB |
Output is correct |
4 |
Correct |
575 ms |
18320 KB |
Output is correct |
5 |
Correct |
447 ms |
18876 KB |
Output is correct |
6 |
Correct |
480 ms |
19272 KB |
Output is correct |
7 |
Correct |
428 ms |
19540 KB |
Output is correct |
8 |
Correct |
483 ms |
23652 KB |
Output is correct |
9 |
Correct |
568 ms |
23488 KB |
Output is correct |
10 |
Correct |
525 ms |
18652 KB |
Output is correct |
11 |
Correct |
491 ms |
20620 KB |
Output is correct |
12 |
Correct |
335 ms |
18852 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
316 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
452 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
4 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
448 KB |
Output is correct |
27 |
Correct |
3 ms |
456 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
229 ms |
1940 KB |
Output is correct |
32 |
Correct |
258 ms |
1828 KB |
Output is correct |
33 |
Correct |
221 ms |
2008 KB |
Output is correct |
34 |
Correct |
241 ms |
2028 KB |
Output is correct |
35 |
Correct |
204 ms |
2212 KB |
Output is correct |
36 |
Correct |
258 ms |
2096 KB |
Output is correct |
37 |
Correct |
231 ms |
1832 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
3 ms |
452 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
448 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
8 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
340 KB |
Output is correct |
47 |
Correct |
290 ms |
17776 KB |
Output is correct |
48 |
Correct |
278 ms |
17740 KB |
Output is correct |
49 |
Correct |
276 ms |
17744 KB |
Output is correct |
50 |
Correct |
301 ms |
19492 KB |
Output is correct |
51 |
Correct |
345 ms |
22472 KB |
Output is correct |
52 |
Correct |
350 ms |
22476 KB |
Output is correct |
53 |
Correct |
91 ms |
12988 KB |
Output is correct |
54 |
Correct |
448 ms |
18616 KB |
Output is correct |
55 |
Correct |
440 ms |
18388 KB |
Output is correct |
56 |
Correct |
477 ms |
18300 KB |
Output is correct |
57 |
Correct |
482 ms |
23564 KB |
Output is correct |
58 |
Correct |
530 ms |
18664 KB |
Output is correct |
59 |
Correct |
633 ms |
23188 KB |
Output is correct |
60 |
Correct |
621 ms |
23252 KB |
Output is correct |
61 |
Correct |
590 ms |
23352 KB |
Output is correct |
62 |
Correct |
385 ms |
22568 KB |
Output is correct |
63 |
Correct |
533 ms |
22884 KB |
Output is correct |
64 |
Correct |
558 ms |
22664 KB |
Output is correct |
65 |
Correct |
631 ms |
22796 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
470 ms |
18448 KB |
Output is correct |
68 |
Correct |
477 ms |
18364 KB |
Output is correct |
69 |
Correct |
503 ms |
18296 KB |
Output is correct |
70 |
Correct |
478 ms |
18848 KB |
Output is correct |
71 |
Correct |
536 ms |
19340 KB |
Output is correct |
72 |
Correct |
502 ms |
19504 KB |
Output is correct |
73 |
Correct |
489 ms |
23504 KB |
Output is correct |
74 |
Correct |
502 ms |
23592 KB |
Output is correct |
75 |
Correct |
509 ms |
18776 KB |
Output is correct |
76 |
Correct |
500 ms |
20724 KB |
Output is correct |
77 |
Correct |
343 ms |
18892 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
468 KB |
Output is correct |
80 |
Correct |
4 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
3 ms |
488 KB |
Output is correct |
83 |
Correct |
4 ms |
440 KB |
Output is correct |
84 |
Correct |
3 ms |
452 KB |
Output is correct |
85 |
Correct |
2 ms |
340 KB |
Output is correct |
86 |
Correct |
220 ms |
1848 KB |
Output is correct |
87 |
Correct |
211 ms |
1732 KB |
Output is correct |
88 |
Correct |
194 ms |
2088 KB |
Output is correct |
89 |
Correct |
217 ms |
2208 KB |
Output is correct |
90 |
Correct |
204 ms |
2128 KB |
Output is correct |
91 |
Correct |
179 ms |
2196 KB |
Output is correct |
92 |
Correct |
178 ms |
1824 KB |
Output is correct |
93 |
Correct |
261 ms |
17788 KB |
Output is correct |
94 |
Correct |
223 ms |
17864 KB |
Output is correct |
95 |
Correct |
277 ms |
17692 KB |
Output is correct |
96 |
Correct |
260 ms |
19412 KB |
Output is correct |
97 |
Correct |
332 ms |
22384 KB |
Output is correct |
98 |
Correct |
375 ms |
22488 KB |
Output is correct |
99 |
Correct |
120 ms |
12992 KB |
Output is correct |
100 |
Correct |
614 ms |
23256 KB |
Output is correct |
101 |
Correct |
572 ms |
23108 KB |
Output is correct |
102 |
Correct |
615 ms |
23248 KB |
Output is correct |
103 |
Correct |
570 ms |
22864 KB |
Output is correct |
104 |
Correct |
528 ms |
22628 KB |
Output is correct |
105 |
Correct |
511 ms |
22956 KB |
Output is correct |
106 |
Correct |
445 ms |
17304 KB |
Output is correct |
107 |
Correct |
499 ms |
18560 KB |
Output is correct |
108 |
Correct |
442 ms |
18672 KB |
Output is correct |
109 |
Correct |
460 ms |
18620 KB |
Output is correct |
110 |
Correct |
644 ms |
23180 KB |
Output is correct |
111 |
Correct |
580 ms |
23072 KB |
Output is correct |