// Be name khoda //
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
#define mp make_pair
const int maxn5 = 2e5 + 10;
const int lg = 19;
vector <int> ch, av, req[maxn5];
int l[maxn5], r[maxn5], ans[maxn5], par[lg][maxn5];
int x[maxn5], y[maxn5], ind[maxn5], per[maxn5];
pair <int, int> fen[maxn5];
inline bool cmp(int i, int j){return r[i] < r[j] || (r[i] == r[j] && l[i] < l[j]);}
inline void max_mos(int id, pair <int, int> val){
for(++id; id < maxn5; id += id & -id){
fen[id] = max(fen[id], val);
ch.pb(id);
}
}
inline pair <int, int> get_max(int id){
pair <int, int> ret = {-1, -1};
for(++id; id; id -= id & -id)
ret = max(ret, fen[id]);
return ret;
}
inline void fen_reset(){
for(auto u : ch)
fen[u] = {-1, -1};
ch.clear();
}
void solve(int lq, int rq){
if(rq - lq == 1)
return;
int mid = (lq + rq) >> 1;
solve(lq, mid);
int mn = l[per[mid]];
for(int i = mid; i < rq; i++){
mn = min(mn, l[per[i]]);
max_mos(l[per[i]], mp(r[per[i]], i));
for(auto id : req[i]) if(x[id] >= lq && x[id] < mid){
int v = x[id];
if(r[per[v]] < mn){
for(int j = lg - 1; j >= 0; j--) if(par[j][v] != -1 && r[per[par[j][v]]] < mn){
v = par[j][v];
ans[id] += (1 << j);
}
v = par[0][v];
ans[id]++;
}
if(v == -1){
ans[id] = -1;
continue;
}
//cout << "hey it's " << i << ' ' << id << ' ' << x[id] << ' ' << v << '\n';
auto mx = get_max(r[per[v]]);
x[id] = mx.se;
ans[id]++;
}
}
for(int i = mid - 1; i >= lq; i--){
for(int j = 0; j < lg; j++)
par[j][i] = -1;
par[0][i] = get_max(r[per[i]]).se;
max_mos(l[per[i]], mp(r[per[i]], i));
}
fen_reset();
solve(mid, rq);
for(int i = 1; i < lg; i++) for(int v = lq; v < mid; v++)
if(par[i - 1][v] != -1) par[i][v] = par[i - 1][par[i - 1][v]];
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
fill(fen, fen + maxn5, mp(-1, -1));
int n, q; cin >> n >> q;
for(int i = 0; i < n; i++){
cin >> l[i] >> r[i];
av.pb(l[i]);
av.pb(r[i]);
per[i] = i;
}
sort(all(av));
av.resize(unique(all(av)) - av.begin());
for(int i = 0; i < n; i++){
l[i] = lower_bound(all(av), l[i]) - av.begin();
r[i] = lower_bound(all(av), r[i]) - av.begin();
//cout << i << ' ' << l[i] << ' ' << r[i] << '\n';
}
sort(per, per + n, cmp);
for(int i = 0; i < n; i++)
ind[per[i]] = i;
for(int i = 0; i < q; i++){
cin >> x[i] >> y[i];
x[i]--; y[i]--;
if(x[i] == y[i])
continue;
if(ind[x[i]] > ind[y[i]])
ans[i] = r[y[i]] == r[x[i]] ? 1 : -1;
else{
req[ind[y[i]]].pb(i);
x[i] = ind[x[i]];
y[i] = ind[y[i]];
}
//cout << "ok " << i << ' ' << x[i] << ' ' << y[i] << '\n';
}
memset(par, -1, sizeof par);
solve(0, n);
for(int i = 0; i < q; i++){
if(ans[i] == -1)
cout << "impossible\n";
else
cout << ans[i] << '\n';
}
}
/*
5 1
1 3
2 4
4 7
7 9
3 7
1 4
3 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
403 ms |
33928 KB |
Output is correct |
3 |
Correct |
376 ms |
33372 KB |
Output is correct |
4 |
Correct |
348 ms |
34208 KB |
Output is correct |
5 |
Correct |
384 ms |
33288 KB |
Output is correct |
6 |
Correct |
364 ms |
33232 KB |
Output is correct |
7 |
Correct |
397 ms |
33224 KB |
Output is correct |
8 |
Correct |
304 ms |
33224 KB |
Output is correct |
9 |
Correct |
340 ms |
33740 KB |
Output is correct |
10 |
Correct |
366 ms |
34140 KB |
Output is correct |
11 |
Correct |
379 ms |
34008 KB |
Output is correct |
12 |
Correct |
275 ms |
33876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
21484 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21716 KB |
Output is correct |
4 |
Correct |
12 ms |
21584 KB |
Output is correct |
5 |
Correct |
12 ms |
21576 KB |
Output is correct |
6 |
Correct |
12 ms |
21664 KB |
Output is correct |
7 |
Correct |
13 ms |
21576 KB |
Output is correct |
8 |
Correct |
12 ms |
21580 KB |
Output is correct |
9 |
Correct |
13 ms |
21644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
21484 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21716 KB |
Output is correct |
4 |
Correct |
12 ms |
21584 KB |
Output is correct |
5 |
Correct |
12 ms |
21576 KB |
Output is correct |
6 |
Correct |
12 ms |
21664 KB |
Output is correct |
7 |
Correct |
13 ms |
21576 KB |
Output is correct |
8 |
Correct |
12 ms |
21580 KB |
Output is correct |
9 |
Correct |
13 ms |
21644 KB |
Output is correct |
10 |
Correct |
9 ms |
21460 KB |
Output is correct |
11 |
Correct |
9 ms |
21460 KB |
Output is correct |
12 |
Correct |
11 ms |
21588 KB |
Output is correct |
13 |
Correct |
11 ms |
21588 KB |
Output is correct |
14 |
Correct |
11 ms |
21576 KB |
Output is correct |
15 |
Correct |
12 ms |
21568 KB |
Output is correct |
16 |
Correct |
12 ms |
21576 KB |
Output is correct |
17 |
Correct |
12 ms |
21580 KB |
Output is correct |
18 |
Correct |
14 ms |
21640 KB |
Output is correct |
19 |
Correct |
52 ms |
25196 KB |
Output is correct |
20 |
Correct |
84 ms |
25128 KB |
Output is correct |
21 |
Correct |
63 ms |
25212 KB |
Output is correct |
22 |
Correct |
50 ms |
25280 KB |
Output is correct |
23 |
Correct |
55 ms |
25140 KB |
Output is correct |
24 |
Correct |
50 ms |
25116 KB |
Output is correct |
25 |
Correct |
67 ms |
25024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
21484 KB |
Output is correct |
2 |
Correct |
10 ms |
21460 KB |
Output is correct |
3 |
Correct |
11 ms |
21716 KB |
Output is correct |
4 |
Correct |
12 ms |
21584 KB |
Output is correct |
5 |
Correct |
12 ms |
21576 KB |
Output is correct |
6 |
Correct |
12 ms |
21664 KB |
Output is correct |
7 |
Correct |
13 ms |
21576 KB |
Output is correct |
8 |
Correct |
12 ms |
21580 KB |
Output is correct |
9 |
Correct |
13 ms |
21644 KB |
Output is correct |
10 |
Correct |
9 ms |
21496 KB |
Output is correct |
11 |
Correct |
9 ms |
21460 KB |
Output is correct |
12 |
Correct |
11 ms |
21588 KB |
Output is correct |
13 |
Correct |
12 ms |
21668 KB |
Output is correct |
14 |
Correct |
12 ms |
21588 KB |
Output is correct |
15 |
Correct |
11 ms |
21580 KB |
Output is correct |
16 |
Correct |
13 ms |
21612 KB |
Output is correct |
17 |
Correct |
11 ms |
21580 KB |
Output is correct |
18 |
Correct |
11 ms |
21716 KB |
Output is correct |
19 |
Correct |
287 ms |
29960 KB |
Output is correct |
20 |
Correct |
292 ms |
29228 KB |
Output is correct |
21 |
Correct |
340 ms |
34004 KB |
Output is correct |
22 |
Correct |
302 ms |
30012 KB |
Output is correct |
23 |
Correct |
300 ms |
30008 KB |
Output is correct |
24 |
Correct |
309 ms |
29980 KB |
Output is correct |
25 |
Correct |
351 ms |
33588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
368 ms |
33928 KB |
Output is correct |
2 |
Correct |
374 ms |
33252 KB |
Output is correct |
3 |
Correct |
359 ms |
34180 KB |
Output is correct |
4 |
Correct |
328 ms |
33616 KB |
Output is correct |
5 |
Correct |
377 ms |
34112 KB |
Output is correct |
6 |
Correct |
363 ms |
33848 KB |
Output is correct |
7 |
Correct |
380 ms |
33756 KB |
Output is correct |
8 |
Correct |
377 ms |
33696 KB |
Output is correct |
9 |
Correct |
314 ms |
29908 KB |
Output is correct |
10 |
Correct |
371 ms |
33000 KB |
Output is correct |
11 |
Correct |
356 ms |
32932 KB |
Output is correct |
12 |
Correct |
370 ms |
32952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
21460 KB |
Output is correct |
2 |
Correct |
403 ms |
33928 KB |
Output is correct |
3 |
Correct |
376 ms |
33372 KB |
Output is correct |
4 |
Correct |
348 ms |
34208 KB |
Output is correct |
5 |
Correct |
384 ms |
33288 KB |
Output is correct |
6 |
Correct |
364 ms |
33232 KB |
Output is correct |
7 |
Correct |
397 ms |
33224 KB |
Output is correct |
8 |
Correct |
304 ms |
33224 KB |
Output is correct |
9 |
Correct |
340 ms |
33740 KB |
Output is correct |
10 |
Correct |
366 ms |
34140 KB |
Output is correct |
11 |
Correct |
379 ms |
34008 KB |
Output is correct |
12 |
Correct |
275 ms |
33876 KB |
Output is correct |
13 |
Correct |
10 ms |
21484 KB |
Output is correct |
14 |
Correct |
10 ms |
21460 KB |
Output is correct |
15 |
Correct |
11 ms |
21716 KB |
Output is correct |
16 |
Correct |
12 ms |
21584 KB |
Output is correct |
17 |
Correct |
12 ms |
21576 KB |
Output is correct |
18 |
Correct |
12 ms |
21664 KB |
Output is correct |
19 |
Correct |
13 ms |
21576 KB |
Output is correct |
20 |
Correct |
12 ms |
21580 KB |
Output is correct |
21 |
Correct |
13 ms |
21644 KB |
Output is correct |
22 |
Correct |
9 ms |
21460 KB |
Output is correct |
23 |
Correct |
9 ms |
21460 KB |
Output is correct |
24 |
Correct |
11 ms |
21588 KB |
Output is correct |
25 |
Correct |
11 ms |
21588 KB |
Output is correct |
26 |
Correct |
11 ms |
21576 KB |
Output is correct |
27 |
Correct |
12 ms |
21568 KB |
Output is correct |
28 |
Correct |
12 ms |
21576 KB |
Output is correct |
29 |
Correct |
12 ms |
21580 KB |
Output is correct |
30 |
Correct |
14 ms |
21640 KB |
Output is correct |
31 |
Correct |
52 ms |
25196 KB |
Output is correct |
32 |
Correct |
84 ms |
25128 KB |
Output is correct |
33 |
Correct |
63 ms |
25212 KB |
Output is correct |
34 |
Correct |
50 ms |
25280 KB |
Output is correct |
35 |
Correct |
55 ms |
25140 KB |
Output is correct |
36 |
Correct |
50 ms |
25116 KB |
Output is correct |
37 |
Correct |
67 ms |
25024 KB |
Output is correct |
38 |
Correct |
9 ms |
21496 KB |
Output is correct |
39 |
Correct |
9 ms |
21460 KB |
Output is correct |
40 |
Correct |
11 ms |
21588 KB |
Output is correct |
41 |
Correct |
12 ms |
21668 KB |
Output is correct |
42 |
Correct |
12 ms |
21588 KB |
Output is correct |
43 |
Correct |
11 ms |
21580 KB |
Output is correct |
44 |
Correct |
13 ms |
21612 KB |
Output is correct |
45 |
Correct |
11 ms |
21580 KB |
Output is correct |
46 |
Correct |
11 ms |
21716 KB |
Output is correct |
47 |
Correct |
287 ms |
29960 KB |
Output is correct |
48 |
Correct |
292 ms |
29228 KB |
Output is correct |
49 |
Correct |
340 ms |
34004 KB |
Output is correct |
50 |
Correct |
302 ms |
30012 KB |
Output is correct |
51 |
Correct |
300 ms |
30008 KB |
Output is correct |
52 |
Correct |
309 ms |
29980 KB |
Output is correct |
53 |
Correct |
351 ms |
33588 KB |
Output is correct |
54 |
Correct |
368 ms |
33928 KB |
Output is correct |
55 |
Correct |
374 ms |
33252 KB |
Output is correct |
56 |
Correct |
359 ms |
34180 KB |
Output is correct |
57 |
Correct |
328 ms |
33616 KB |
Output is correct |
58 |
Correct |
377 ms |
34112 KB |
Output is correct |
59 |
Correct |
363 ms |
33848 KB |
Output is correct |
60 |
Correct |
380 ms |
33756 KB |
Output is correct |
61 |
Correct |
377 ms |
33696 KB |
Output is correct |
62 |
Correct |
314 ms |
29908 KB |
Output is correct |
63 |
Correct |
371 ms |
33000 KB |
Output is correct |
64 |
Correct |
356 ms |
32932 KB |
Output is correct |
65 |
Correct |
370 ms |
32952 KB |
Output is correct |
66 |
Correct |
9 ms |
21460 KB |
Output is correct |
67 |
Correct |
383 ms |
34008 KB |
Output is correct |
68 |
Correct |
370 ms |
33172 KB |
Output is correct |
69 |
Correct |
378 ms |
34212 KB |
Output is correct |
70 |
Correct |
368 ms |
33308 KB |
Output is correct |
71 |
Correct |
379 ms |
33236 KB |
Output is correct |
72 |
Correct |
406 ms |
33228 KB |
Output is correct |
73 |
Correct |
361 ms |
33448 KB |
Output is correct |
74 |
Correct |
352 ms |
33684 KB |
Output is correct |
75 |
Correct |
387 ms |
34064 KB |
Output is correct |
76 |
Correct |
408 ms |
34000 KB |
Output is correct |
77 |
Correct |
302 ms |
33836 KB |
Output is correct |
78 |
Correct |
10 ms |
21460 KB |
Output is correct |
79 |
Correct |
12 ms |
21588 KB |
Output is correct |
80 |
Correct |
13 ms |
21620 KB |
Output is correct |
81 |
Correct |
15 ms |
21592 KB |
Output is correct |
82 |
Correct |
14 ms |
21580 KB |
Output is correct |
83 |
Correct |
14 ms |
21576 KB |
Output is correct |
84 |
Correct |
13 ms |
21588 KB |
Output is correct |
85 |
Correct |
12 ms |
21716 KB |
Output is correct |
86 |
Correct |
54 ms |
25092 KB |
Output is correct |
87 |
Correct |
87 ms |
25148 KB |
Output is correct |
88 |
Correct |
87 ms |
25216 KB |
Output is correct |
89 |
Correct |
52 ms |
25292 KB |
Output is correct |
90 |
Correct |
53 ms |
25216 KB |
Output is correct |
91 |
Correct |
47 ms |
25160 KB |
Output is correct |
92 |
Correct |
74 ms |
24916 KB |
Output is correct |
93 |
Correct |
293 ms |
29932 KB |
Output is correct |
94 |
Correct |
295 ms |
29128 KB |
Output is correct |
95 |
Correct |
345 ms |
33972 KB |
Output is correct |
96 |
Correct |
297 ms |
29944 KB |
Output is correct |
97 |
Correct |
337 ms |
29976 KB |
Output is correct |
98 |
Correct |
311 ms |
30008 KB |
Output is correct |
99 |
Correct |
366 ms |
33336 KB |
Output is correct |
100 |
Correct |
381 ms |
33872 KB |
Output is correct |
101 |
Correct |
377 ms |
33852 KB |
Output is correct |
102 |
Correct |
372 ms |
33864 KB |
Output is correct |
103 |
Correct |
354 ms |
33064 KB |
Output is correct |
104 |
Correct |
337 ms |
32848 KB |
Output is correct |
105 |
Correct |
383 ms |
32968 KB |
Output is correct |
106 |
Correct |
392 ms |
33360 KB |
Output is correct |
107 |
Correct |
349 ms |
33140 KB |
Output is correct |
108 |
Correct |
410 ms |
37372 KB |
Output is correct |
109 |
Correct |
387 ms |
37432 KB |
Output is correct |
110 |
Correct |
386 ms |
33676 KB |
Output is correct |
111 |
Correct |
434 ms |
33856 KB |
Output is correct |