#include <bits/stdc++.h>
using namespace std;
#define len(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define fr first
#define sc second
const int INF = 1e9;
const int N = 2e5 + 5, lN = 18;
int n, q;
pii a[N];
int par[N][lN];
vector<int> temp, ind;
map<int, int> mp;
pii t[4 * N];
void build(int v, int tl, int tr){
if(tl == tr){
t[v] = {INF, -1};
return;
}
int tm = (tl + tr) / 2;
build(2 * v + 0, tl, tm + 0);
build(2 * v + 1, tm + 1, tr);
t[v] = {INF, -1};
}
void update(int v, int tl, int tr, int ind, int val, int i){
if(tl == tr){
t[v] = min(t[v], {val, i});
return;
}
int tm = (tl + tr) / 2;
if(ind <= tm) update(2 * v + 0, tl, tm + 0, ind, val, i);
else update(2 * v + 1, tm + 1, tr, ind, val, i);
t[v] = min(t[2 * v + 0], t[2 * v + 1]);
}
pii get(int v, int tl, int tr, int l, int r){
if(l > r) return (pii){INF, -1};
if(tl == l && tr == r) return t[v];
int tm = (tl + tr) / 2;
return min(get(2 * v + 0, tl, tm + 0, l, min(r, tm + 0)),
get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for(int i = 0; i < n; i++){
cin >> a[i].fr >> a[i].sc;
temp.push_back(a[i].fr);
temp.push_back(a[i].sc);
ind.push_back(i);
}
sort(all(temp));
sort(all(ind), [](int i, int j){ return a[i] < a[j]; });
int last = -1, cnt = 0;
for(int i = 0; i < len(temp); i++){
if(temp[i] == last) continue;
cnt++;
last = temp[i];
mp[temp[i]] = cnt;
}
build(1, 1, cnt);
for(int i = 0; i < n; i++){
a[i].fr = mp[a[i].fr];
a[i].sc = mp[a[i].sc];
update(1, 1, cnt, a[i].sc, a[i].fr, i);
}
for(int i = 0; i < n; i++){
pii h = get(1, 1, cnt, a[i].fr, a[i].sc);
par[i][0] = (h.fr >= a[i].fr ? i : h.sc);
}
for(auto i : ind){
for(int j = 1; j < lN; j++){
par[i][j] = par[par[i][j - 1]][j - 1];
}
}
int x, y;
while(q--){
cin >> x >> y;
x--, y--;
if(x == y){
cout << "0\n";
continue;
}
if(a[x].sc > a[y].sc){
cout << "impossible\n";
continue;
}
if(a[x].sc >= a[y].fr){
cout << "1\n";
continue;
}
int ans = 2;
for(int i = lN - 1; ~i; i--){
if(a[par[y][i]].fr <= a[x].sc) continue;
ans += (1 << i);
y = par[y][i];
}
if(a[par[y][0]].fr > a[x].sc){
cout << "impossible\n";
continue;
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
211 ms |
16632 KB |
Output is correct |
3 |
Correct |
183 ms |
16644 KB |
Output is correct |
4 |
Correct |
230 ms |
16576 KB |
Output is correct |
5 |
Correct |
219 ms |
17216 KB |
Output is correct |
6 |
Correct |
211 ms |
17668 KB |
Output is correct |
7 |
Correct |
227 ms |
17764 KB |
Output is correct |
8 |
Correct |
256 ms |
23868 KB |
Output is correct |
9 |
Correct |
246 ms |
23852 KB |
Output is correct |
10 |
Correct |
268 ms |
17024 KB |
Output is correct |
11 |
Correct |
264 ms |
21024 KB |
Output is correct |
12 |
Correct |
115 ms |
19428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
44 ms |
1740 KB |
Output is correct |
20 |
Correct |
31 ms |
1584 KB |
Output is correct |
21 |
Correct |
32 ms |
1936 KB |
Output is correct |
22 |
Correct |
25 ms |
1984 KB |
Output is correct |
23 |
Correct |
32 ms |
2100 KB |
Output is correct |
24 |
Correct |
24 ms |
2120 KB |
Output is correct |
25 |
Correct |
37 ms |
1728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
182 ms |
15992 KB |
Output is correct |
20 |
Correct |
160 ms |
16064 KB |
Output is correct |
21 |
Correct |
191 ms |
16024 KB |
Output is correct |
22 |
Correct |
196 ms |
19844 KB |
Output is correct |
23 |
Correct |
256 ms |
24780 KB |
Output is correct |
24 |
Correct |
273 ms |
24792 KB |
Output is correct |
25 |
Correct |
33 ms |
11096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
16652 KB |
Output is correct |
2 |
Correct |
217 ms |
16652 KB |
Output is correct |
3 |
Correct |
207 ms |
16704 KB |
Output is correct |
4 |
Correct |
225 ms |
23952 KB |
Output is correct |
5 |
Correct |
222 ms |
17056 KB |
Output is correct |
6 |
Correct |
285 ms |
23624 KB |
Output is correct |
7 |
Correct |
286 ms |
26672 KB |
Output is correct |
8 |
Correct |
283 ms |
26808 KB |
Output is correct |
9 |
Correct |
255 ms |
24764 KB |
Output is correct |
10 |
Correct |
288 ms |
26304 KB |
Output is correct |
11 |
Correct |
269 ms |
26016 KB |
Output is correct |
12 |
Correct |
303 ms |
26400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
211 ms |
16632 KB |
Output is correct |
3 |
Correct |
183 ms |
16644 KB |
Output is correct |
4 |
Correct |
230 ms |
16576 KB |
Output is correct |
5 |
Correct |
219 ms |
17216 KB |
Output is correct |
6 |
Correct |
211 ms |
17668 KB |
Output is correct |
7 |
Correct |
227 ms |
17764 KB |
Output is correct |
8 |
Correct |
256 ms |
23868 KB |
Output is correct |
9 |
Correct |
246 ms |
23852 KB |
Output is correct |
10 |
Correct |
268 ms |
17024 KB |
Output is correct |
11 |
Correct |
264 ms |
21024 KB |
Output is correct |
12 |
Correct |
115 ms |
19428 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
44 ms |
1740 KB |
Output is correct |
32 |
Correct |
31 ms |
1584 KB |
Output is correct |
33 |
Correct |
32 ms |
1936 KB |
Output is correct |
34 |
Correct |
25 ms |
1984 KB |
Output is correct |
35 |
Correct |
32 ms |
2100 KB |
Output is correct |
36 |
Correct |
24 ms |
2120 KB |
Output is correct |
37 |
Correct |
37 ms |
1728 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
182 ms |
15992 KB |
Output is correct |
48 |
Correct |
160 ms |
16064 KB |
Output is correct |
49 |
Correct |
191 ms |
16024 KB |
Output is correct |
50 |
Correct |
196 ms |
19844 KB |
Output is correct |
51 |
Correct |
256 ms |
24780 KB |
Output is correct |
52 |
Correct |
273 ms |
24792 KB |
Output is correct |
53 |
Correct |
33 ms |
11096 KB |
Output is correct |
54 |
Correct |
176 ms |
16652 KB |
Output is correct |
55 |
Correct |
217 ms |
16652 KB |
Output is correct |
56 |
Correct |
207 ms |
16704 KB |
Output is correct |
57 |
Correct |
225 ms |
23952 KB |
Output is correct |
58 |
Correct |
222 ms |
17056 KB |
Output is correct |
59 |
Correct |
285 ms |
23624 KB |
Output is correct |
60 |
Correct |
286 ms |
26672 KB |
Output is correct |
61 |
Correct |
283 ms |
26808 KB |
Output is correct |
62 |
Correct |
255 ms |
24764 KB |
Output is correct |
63 |
Correct |
288 ms |
26304 KB |
Output is correct |
64 |
Correct |
269 ms |
26016 KB |
Output is correct |
65 |
Correct |
303 ms |
26400 KB |
Output is correct |
66 |
Correct |
1 ms |
336 KB |
Output is correct |
67 |
Correct |
198 ms |
19888 KB |
Output is correct |
68 |
Correct |
199 ms |
19704 KB |
Output is correct |
69 |
Correct |
257 ms |
19748 KB |
Output is correct |
70 |
Correct |
208 ms |
20376 KB |
Output is correct |
71 |
Correct |
266 ms |
20740 KB |
Output is correct |
72 |
Correct |
216 ms |
21016 KB |
Output is correct |
73 |
Correct |
238 ms |
27044 KB |
Output is correct |
74 |
Correct |
239 ms |
26944 KB |
Output is correct |
75 |
Correct |
218 ms |
20208 KB |
Output is correct |
76 |
Correct |
259 ms |
24080 KB |
Output is correct |
77 |
Correct |
123 ms |
19520 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
2 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
468 KB |
Output is correct |
83 |
Correct |
3 ms |
596 KB |
Output is correct |
84 |
Correct |
3 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
34 ms |
2640 KB |
Output is correct |
87 |
Correct |
33 ms |
2696 KB |
Output is correct |
88 |
Correct |
38 ms |
2824 KB |
Output is correct |
89 |
Correct |
29 ms |
2932 KB |
Output is correct |
90 |
Correct |
39 ms |
3168 KB |
Output is correct |
91 |
Correct |
29 ms |
3120 KB |
Output is correct |
92 |
Correct |
36 ms |
2864 KB |
Output is correct |
93 |
Correct |
217 ms |
18044 KB |
Output is correct |
94 |
Correct |
224 ms |
17248 KB |
Output is correct |
95 |
Correct |
206 ms |
17940 KB |
Output is correct |
96 |
Correct |
219 ms |
21756 KB |
Output is correct |
97 |
Correct |
261 ms |
24768 KB |
Output is correct |
98 |
Correct |
272 ms |
24712 KB |
Output is correct |
99 |
Correct |
37 ms |
11124 KB |
Output is correct |
100 |
Correct |
298 ms |
26668 KB |
Output is correct |
101 |
Correct |
347 ms |
26692 KB |
Output is correct |
102 |
Correct |
275 ms |
26700 KB |
Output is correct |
103 |
Correct |
305 ms |
26200 KB |
Output is correct |
104 |
Correct |
270 ms |
26180 KB |
Output is correct |
105 |
Correct |
274 ms |
26284 KB |
Output is correct |
106 |
Correct |
199 ms |
17964 KB |
Output is correct |
107 |
Correct |
224 ms |
19424 KB |
Output is correct |
108 |
Correct |
213 ms |
19828 KB |
Output is correct |
109 |
Correct |
197 ms |
19820 KB |
Output is correct |
110 |
Correct |
303 ms |
26680 KB |
Output is correct |
111 |
Correct |
282 ms |
26564 KB |
Output is correct |