#include <bits/stdc++.h>
using namespace std;
#define ar array
#define int long long
#define ld long double
const int N = 5e5 + 20;
const int LOG = 21;
const int INF = 1e17;
namespace seggy{
ar<int, 2> s[4 * N];
void build(int k,int tl,int tr){
s[k] = {INF, -1};
if(tl == tr)return;
int tm = (tl + tr) / 2;
build(k * 2, tl, tm);
build(k *2 + 1,tm + 1, tr);
}
void upd(int k,int tl,int tr,int p,ar<int, 2> v){
if(tl == tr){
s[k] = min(s[k], v);
return;
}
int tm = (tl + tr) / 2;
if(p <= tm)upd(k * 2,tl, tm, p, v);
else upd(k * 2 + 1, tm + 1, tr, p, v);
s[k] = min(s[k * 2], s[k * 2 + 1]);
}
ar<int, 2> qry(int k,int tl,int tr,int l,int r){
if(l > tr || tl > r)return {INF, -1};
if(l <= tl && tr <= r)return s[k];
int tm = (tl + tr) / 2;
return min(qry(k * 2,tl, tm, l,r), qry(k *2 + 1, tm + 1, tr, l, r));
}
}
signed main(){ios::sync_with_stdio(false);cin.tie(0);
int n, q;
cin>>n>>q;
int s[n], e[n];
vector<int> v;
for(int i = 0;i < n;i++){
cin>>s[i]>>e[i];
v.push_back(s[i]);v.push_back(e[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int m = v.size();
seggy::build(1, 0, m - 1);
for(int i = 0;i < n;i++){
s[i] = lower_bound(v.begin(), v.end(), s[i]) - v.begin();
e[i] = lower_bound(v.begin(), v.end(), e[i]) - v.begin();
seggy::upd(1, 0, m - 1, e[i], {s[i], i});
}
int up[n][LOG];
for(int i = 0;i < n;i++){
ar<int, 2> j = seggy::qry(1, 0, m-1, s[i], e[i]);
up[i][0] = j[1];
}
for(int j = 1;j < LOG;j++){
for(int i = 0;i < n;i++){
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
while(q--){
int x,y;
cin>>x>>y;
--x, --y;
int ans = 0;
for(int i = LOG - 1;i >= 0;i--){
int j = up[y][i];
if(j == -1)continue;
if(s[j] > e[x]){
y = j;
ans += (1 << i);
}
}
if(x == y)cout<<ans<<'\n';
else{
if(s[y] > e[x]){
y = up[y][0];
++ans;
}
if(s[y] <= e[x] && e[x] <= e[y])cout<<ans + 1<<'\n';
else cout<<"impossible\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
92 ms |
24900 KB |
Output is correct |
3 |
Correct |
96 ms |
25028 KB |
Output is correct |
4 |
Correct |
106 ms |
24768 KB |
Output is correct |
5 |
Correct |
108 ms |
25024 KB |
Output is correct |
6 |
Correct |
103 ms |
28104 KB |
Output is correct |
7 |
Correct |
103 ms |
28136 KB |
Output is correct |
8 |
Correct |
91 ms |
32708 KB |
Output is correct |
9 |
Correct |
92 ms |
32712 KB |
Output is correct |
10 |
Correct |
100 ms |
28336 KB |
Output is correct |
11 |
Correct |
98 ms |
32452 KB |
Output is correct |
12 |
Correct |
58 ms |
27832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
648 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
648 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
20 ms |
5084 KB |
Output is correct |
20 |
Correct |
19 ms |
5212 KB |
Output is correct |
21 |
Correct |
23 ms |
5460 KB |
Output is correct |
22 |
Correct |
17 ms |
5468 KB |
Output is correct |
23 |
Correct |
17 ms |
5296 KB |
Output is correct |
24 |
Correct |
17 ms |
5468 KB |
Output is correct |
25 |
Correct |
17 ms |
5116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
648 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
520 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
76 ms |
26140 KB |
Output is correct |
20 |
Correct |
74 ms |
25544 KB |
Output is correct |
21 |
Correct |
85 ms |
26052 KB |
Output is correct |
22 |
Correct |
85 ms |
30408 KB |
Output is correct |
23 |
Correct |
89 ms |
30248 KB |
Output is correct |
24 |
Correct |
87 ms |
30408 KB |
Output is correct |
25 |
Correct |
26 ms |
21204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
25032 KB |
Output is correct |
2 |
Correct |
98 ms |
25024 KB |
Output is correct |
3 |
Correct |
108 ms |
24776 KB |
Output is correct |
4 |
Correct |
92 ms |
29636 KB |
Output is correct |
5 |
Correct |
113 ms |
25140 KB |
Output is correct |
6 |
Correct |
110 ms |
29128 KB |
Output is correct |
7 |
Correct |
112 ms |
29244 KB |
Output is correct |
8 |
Correct |
114 ms |
29380 KB |
Output is correct |
9 |
Correct |
90 ms |
28356 KB |
Output is correct |
10 |
Correct |
107 ms |
28872 KB |
Output is correct |
11 |
Correct |
105 ms |
28544 KB |
Output is correct |
12 |
Correct |
114 ms |
28872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
92 ms |
24900 KB |
Output is correct |
3 |
Correct |
96 ms |
25028 KB |
Output is correct |
4 |
Correct |
106 ms |
24768 KB |
Output is correct |
5 |
Correct |
108 ms |
25024 KB |
Output is correct |
6 |
Correct |
103 ms |
28104 KB |
Output is correct |
7 |
Correct |
103 ms |
28136 KB |
Output is correct |
8 |
Correct |
91 ms |
32708 KB |
Output is correct |
9 |
Correct |
92 ms |
32712 KB |
Output is correct |
10 |
Correct |
100 ms |
28336 KB |
Output is correct |
11 |
Correct |
98 ms |
32452 KB |
Output is correct |
12 |
Correct |
58 ms |
27832 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
648 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
20 ms |
5084 KB |
Output is correct |
32 |
Correct |
19 ms |
5212 KB |
Output is correct |
33 |
Correct |
23 ms |
5460 KB |
Output is correct |
34 |
Correct |
17 ms |
5468 KB |
Output is correct |
35 |
Correct |
17 ms |
5296 KB |
Output is correct |
36 |
Correct |
17 ms |
5468 KB |
Output is correct |
37 |
Correct |
17 ms |
5116 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
520 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
0 ms |
604 KB |
Output is correct |
47 |
Correct |
76 ms |
26140 KB |
Output is correct |
48 |
Correct |
74 ms |
25544 KB |
Output is correct |
49 |
Correct |
85 ms |
26052 KB |
Output is correct |
50 |
Correct |
85 ms |
30408 KB |
Output is correct |
51 |
Correct |
89 ms |
30248 KB |
Output is correct |
52 |
Correct |
87 ms |
30408 KB |
Output is correct |
53 |
Correct |
26 ms |
21204 KB |
Output is correct |
54 |
Correct |
88 ms |
25032 KB |
Output is correct |
55 |
Correct |
98 ms |
25024 KB |
Output is correct |
56 |
Correct |
108 ms |
24776 KB |
Output is correct |
57 |
Correct |
92 ms |
29636 KB |
Output is correct |
58 |
Correct |
113 ms |
25140 KB |
Output is correct |
59 |
Correct |
110 ms |
29128 KB |
Output is correct |
60 |
Correct |
112 ms |
29244 KB |
Output is correct |
61 |
Correct |
114 ms |
29380 KB |
Output is correct |
62 |
Correct |
90 ms |
28356 KB |
Output is correct |
63 |
Correct |
107 ms |
28872 KB |
Output is correct |
64 |
Correct |
105 ms |
28544 KB |
Output is correct |
65 |
Correct |
114 ms |
28872 KB |
Output is correct |
66 |
Correct |
0 ms |
344 KB |
Output is correct |
67 |
Correct |
92 ms |
28168 KB |
Output is correct |
68 |
Correct |
97 ms |
28100 KB |
Output is correct |
69 |
Correct |
111 ms |
28100 KB |
Output is correct |
70 |
Correct |
108 ms |
28108 KB |
Output is correct |
71 |
Correct |
98 ms |
28104 KB |
Output is correct |
72 |
Correct |
104 ms |
28100 KB |
Output is correct |
73 |
Correct |
106 ms |
32708 KB |
Output is correct |
74 |
Correct |
93 ms |
32504 KB |
Output is correct |
75 |
Correct |
98 ms |
28364 KB |
Output is correct |
76 |
Correct |
99 ms |
32436 KB |
Output is correct |
77 |
Correct |
58 ms |
27636 KB |
Output is correct |
78 |
Correct |
1 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
604 KB |
Output is correct |
80 |
Correct |
1 ms |
604 KB |
Output is correct |
81 |
Correct |
1 ms |
604 KB |
Output is correct |
82 |
Correct |
1 ms |
604 KB |
Output is correct |
83 |
Correct |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
0 ms |
604 KB |
Output is correct |
86 |
Correct |
19 ms |
5200 KB |
Output is correct |
87 |
Correct |
19 ms |
5212 KB |
Output is correct |
88 |
Correct |
22 ms |
5336 KB |
Output is correct |
89 |
Correct |
18 ms |
5548 KB |
Output is correct |
90 |
Correct |
17 ms |
5468 KB |
Output is correct |
91 |
Correct |
17 ms |
5468 KB |
Output is correct |
92 |
Correct |
18 ms |
4956 KB |
Output is correct |
93 |
Correct |
77 ms |
26176 KB |
Output is correct |
94 |
Correct |
72 ms |
25544 KB |
Output is correct |
95 |
Correct |
84 ms |
26224 KB |
Output is correct |
96 |
Correct |
80 ms |
30408 KB |
Output is correct |
97 |
Correct |
88 ms |
30416 KB |
Output is correct |
98 |
Correct |
87 ms |
30404 KB |
Output is correct |
99 |
Correct |
24 ms |
21192 KB |
Output is correct |
100 |
Correct |
116 ms |
32196 KB |
Output is correct |
101 |
Correct |
109 ms |
32196 KB |
Output is correct |
102 |
Correct |
105 ms |
32264 KB |
Output is correct |
103 |
Correct |
109 ms |
31944 KB |
Output is correct |
104 |
Correct |
108 ms |
31684 KB |
Output is correct |
105 |
Correct |
111 ms |
31948 KB |
Output is correct |
106 |
Correct |
92 ms |
27564 KB |
Output is correct |
107 |
Correct |
108 ms |
27380 KB |
Output is correct |
108 |
Correct |
105 ms |
28104 KB |
Output is correct |
109 |
Correct |
113 ms |
28196 KB |
Output is correct |
110 |
Correct |
128 ms |
32184 KB |
Output is correct |
111 |
Correct |
118 ms |
32184 KB |
Output is correct |