#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
int n;
int q;
struct T {
int l;
int r;
int ind;
};
bool operator < (T a, T b) {
if (a.r != b.r) {
return a.r < b.r;
} else {
return a.l < b.l;
}
}
T a[N];
int l[N];
int r[N];
int where[N];
int bs(int x) {
int low = 1, high = n, sol = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (a[mid].r <= x) {
sol = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return sol;
}
pair<int, int> unite(pair<int, int> a, pair<int, int> b) {
return {min(a.first, b.first), max(a.second, b.second)};
}
const int K = 20;
pair<int, int> tab[K][N];
pair<int, int> segt[K][4 * N];
void build(int v, int tl, int tr, int k) {
if (tl == tr) {
segt[k][v] = tab[k][tl];
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm, k);
build(2 * v + 1, tm + 1, tr, k);
segt[k][v] = unite(segt[k][2 * v], segt[k][2 * v + 1]);
}
}
pair<int, int> get(int v, int tl, int tr, int l, int r, int k) {
if (l <= tl && tr <= r) {
return segt[k][v];
}
int tm = (tl + tr) / 2;
if (l <= tm && r <= tm) {
return get(2 * v, tl, tm, l, r, k);
}
if (tm + 1 <= l && tm + 1 <= r) {
return get(2 * v + 1, tm + 1, tr, l, r, k);
}
return unite(get(2 * v, tl, tm, l, tm, k), get(2 * v + 1, tm + 1, tr, tm + 1, r, k));
}
pair<int, int> get(int l, int r, int k) {
return get(1, 1, n, l, r, k);
}
void build(int k) {
build(1, 1, n, k);
}
pair<int, int> go_up(pair<int, int> cur, int k) {
return get(cur.first, cur.second, k);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen ("input", "r", stdin);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r;
a[i].ind = i;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
where[a[i].ind] = i;
}
for (int i = 1; i <= n; i++) {
l[i] = bs(a[i].l - 1) + 1;
r[i] = bs(a[i].r);
tab[0][i] = {l[i], r[i]};
}
build(0);
for (int k = 1; k < K; k++) {
for (int i = 1; i <= n; i++) {
tab[k][i] = go_up(tab[k - 1][i], k - 1);
}
build(k);
}
for (int iq = 1; iq <= q; iq++) {
int ff, ss;
cin >> ff >> ss;
ff = where[ff];
ss = where[ss];
int sol = 1;
pair<int, int> now = {ss, ss};
pair<int, int> nxt = go_up(now, K - 1);
if (nxt.second < ff || nxt.first > ff) {
cout << "impossible\n";
continue;
}
if (ff == ss) {
cout << "0\n";
continue;
}
for (int k = K - 1; k >= 0; k--) {
pair<int, int> nxt = go_up(now, k);
if (nxt.second < ff || nxt.first > ff) {
sol += (1 << k);
now = nxt;
}
}
cout << sol << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
57692 KB |
Output is correct |
2 |
Correct |
440 ms |
81516 KB |
Output is correct |
3 |
Correct |
606 ms |
84692 KB |
Output is correct |
4 |
Correct |
578 ms |
84516 KB |
Output is correct |
5 |
Correct |
578 ms |
84832 KB |
Output is correct |
6 |
Correct |
559 ms |
84916 KB |
Output is correct |
7 |
Correct |
543 ms |
85148 KB |
Output is correct |
8 |
Correct |
189 ms |
85072 KB |
Output is correct |
9 |
Correct |
189 ms |
85152 KB |
Output is correct |
10 |
Correct |
445 ms |
84952 KB |
Output is correct |
11 |
Correct |
360 ms |
85144 KB |
Output is correct |
12 |
Correct |
184 ms |
84368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
57692 KB |
Output is correct |
2 |
Correct |
6 ms |
57892 KB |
Output is correct |
3 |
Correct |
8 ms |
57692 KB |
Output is correct |
4 |
Correct |
8 ms |
57948 KB |
Output is correct |
5 |
Correct |
8 ms |
57944 KB |
Output is correct |
6 |
Correct |
7 ms |
57948 KB |
Output is correct |
7 |
Correct |
8 ms |
57904 KB |
Output is correct |
8 |
Correct |
8 ms |
57948 KB |
Output is correct |
9 |
Correct |
6 ms |
57944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
57692 KB |
Output is correct |
2 |
Correct |
6 ms |
57892 KB |
Output is correct |
3 |
Correct |
8 ms |
57692 KB |
Output is correct |
4 |
Correct |
8 ms |
57948 KB |
Output is correct |
5 |
Correct |
8 ms |
57944 KB |
Output is correct |
6 |
Correct |
7 ms |
57948 KB |
Output is correct |
7 |
Correct |
8 ms |
57904 KB |
Output is correct |
8 |
Correct |
8 ms |
57948 KB |
Output is correct |
9 |
Correct |
6 ms |
57944 KB |
Output is correct |
10 |
Correct |
6 ms |
57688 KB |
Output is correct |
11 |
Correct |
6 ms |
57688 KB |
Output is correct |
12 |
Correct |
8 ms |
57948 KB |
Output is correct |
13 |
Correct |
7 ms |
57928 KB |
Output is correct |
14 |
Correct |
8 ms |
57948 KB |
Output is correct |
15 |
Correct |
8 ms |
57944 KB |
Output is correct |
16 |
Correct |
8 ms |
57948 KB |
Output is correct |
17 |
Correct |
7 ms |
57944 KB |
Output is correct |
18 |
Correct |
6 ms |
57884 KB |
Output is correct |
19 |
Correct |
206 ms |
61560 KB |
Output is correct |
20 |
Correct |
222 ms |
61572 KB |
Output is correct |
21 |
Correct |
138 ms |
61776 KB |
Output is correct |
22 |
Correct |
67 ms |
61780 KB |
Output is correct |
23 |
Correct |
68 ms |
61748 KB |
Output is correct |
24 |
Correct |
68 ms |
61608 KB |
Output is correct |
25 |
Correct |
152 ms |
61228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
57692 KB |
Output is correct |
2 |
Correct |
6 ms |
57892 KB |
Output is correct |
3 |
Correct |
8 ms |
57692 KB |
Output is correct |
4 |
Correct |
8 ms |
57948 KB |
Output is correct |
5 |
Correct |
8 ms |
57944 KB |
Output is correct |
6 |
Correct |
7 ms |
57948 KB |
Output is correct |
7 |
Correct |
8 ms |
57904 KB |
Output is correct |
8 |
Correct |
8 ms |
57948 KB |
Output is correct |
9 |
Correct |
6 ms |
57944 KB |
Output is correct |
10 |
Correct |
6 ms |
57692 KB |
Output is correct |
11 |
Correct |
6 ms |
57692 KB |
Output is correct |
12 |
Correct |
8 ms |
57868 KB |
Output is correct |
13 |
Correct |
8 ms |
57948 KB |
Output is correct |
14 |
Correct |
8 ms |
57948 KB |
Output is correct |
15 |
Correct |
8 ms |
57948 KB |
Output is correct |
16 |
Correct |
7 ms |
57948 KB |
Output is correct |
17 |
Correct |
7 ms |
57948 KB |
Output is correct |
18 |
Correct |
6 ms |
57948 KB |
Output is correct |
19 |
Correct |
270 ms |
83012 KB |
Output is correct |
20 |
Correct |
271 ms |
82272 KB |
Output is correct |
21 |
Correct |
202 ms |
82824 KB |
Output is correct |
22 |
Correct |
246 ms |
83024 KB |
Output is correct |
23 |
Correct |
211 ms |
82772 KB |
Output is correct |
24 |
Correct |
262 ms |
82772 KB |
Output is correct |
25 |
Correct |
46 ms |
82256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
455 ms |
81560 KB |
Output is correct |
2 |
Correct |
614 ms |
84704 KB |
Output is correct |
3 |
Correct |
602 ms |
84664 KB |
Output is correct |
4 |
Correct |
190 ms |
85244 KB |
Output is correct |
5 |
Correct |
429 ms |
85076 KB |
Output is correct |
6 |
Correct |
533 ms |
84696 KB |
Output is correct |
7 |
Correct |
515 ms |
84684 KB |
Output is correct |
8 |
Correct |
434 ms |
85008 KB |
Output is correct |
9 |
Correct |
210 ms |
82928 KB |
Output is correct |
10 |
Correct |
472 ms |
84908 KB |
Output is correct |
11 |
Correct |
317 ms |
84308 KB |
Output is correct |
12 |
Correct |
485 ms |
84680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
57692 KB |
Output is correct |
2 |
Correct |
440 ms |
81516 KB |
Output is correct |
3 |
Correct |
606 ms |
84692 KB |
Output is correct |
4 |
Correct |
578 ms |
84516 KB |
Output is correct |
5 |
Correct |
578 ms |
84832 KB |
Output is correct |
6 |
Correct |
559 ms |
84916 KB |
Output is correct |
7 |
Correct |
543 ms |
85148 KB |
Output is correct |
8 |
Correct |
189 ms |
85072 KB |
Output is correct |
9 |
Correct |
189 ms |
85152 KB |
Output is correct |
10 |
Correct |
445 ms |
84952 KB |
Output is correct |
11 |
Correct |
360 ms |
85144 KB |
Output is correct |
12 |
Correct |
184 ms |
84368 KB |
Output is correct |
13 |
Correct |
6 ms |
57692 KB |
Output is correct |
14 |
Correct |
6 ms |
57892 KB |
Output is correct |
15 |
Correct |
8 ms |
57692 KB |
Output is correct |
16 |
Correct |
8 ms |
57948 KB |
Output is correct |
17 |
Correct |
8 ms |
57944 KB |
Output is correct |
18 |
Correct |
7 ms |
57948 KB |
Output is correct |
19 |
Correct |
8 ms |
57904 KB |
Output is correct |
20 |
Correct |
8 ms |
57948 KB |
Output is correct |
21 |
Correct |
6 ms |
57944 KB |
Output is correct |
22 |
Correct |
6 ms |
57688 KB |
Output is correct |
23 |
Correct |
6 ms |
57688 KB |
Output is correct |
24 |
Correct |
8 ms |
57948 KB |
Output is correct |
25 |
Correct |
7 ms |
57928 KB |
Output is correct |
26 |
Correct |
8 ms |
57948 KB |
Output is correct |
27 |
Correct |
8 ms |
57944 KB |
Output is correct |
28 |
Correct |
8 ms |
57948 KB |
Output is correct |
29 |
Correct |
7 ms |
57944 KB |
Output is correct |
30 |
Correct |
6 ms |
57884 KB |
Output is correct |
31 |
Correct |
206 ms |
61560 KB |
Output is correct |
32 |
Correct |
222 ms |
61572 KB |
Output is correct |
33 |
Correct |
138 ms |
61776 KB |
Output is correct |
34 |
Correct |
67 ms |
61780 KB |
Output is correct |
35 |
Correct |
68 ms |
61748 KB |
Output is correct |
36 |
Correct |
68 ms |
61608 KB |
Output is correct |
37 |
Correct |
152 ms |
61228 KB |
Output is correct |
38 |
Correct |
6 ms |
57692 KB |
Output is correct |
39 |
Correct |
6 ms |
57692 KB |
Output is correct |
40 |
Correct |
8 ms |
57868 KB |
Output is correct |
41 |
Correct |
8 ms |
57948 KB |
Output is correct |
42 |
Correct |
8 ms |
57948 KB |
Output is correct |
43 |
Correct |
8 ms |
57948 KB |
Output is correct |
44 |
Correct |
7 ms |
57948 KB |
Output is correct |
45 |
Correct |
7 ms |
57948 KB |
Output is correct |
46 |
Correct |
6 ms |
57948 KB |
Output is correct |
47 |
Correct |
270 ms |
83012 KB |
Output is correct |
48 |
Correct |
271 ms |
82272 KB |
Output is correct |
49 |
Correct |
202 ms |
82824 KB |
Output is correct |
50 |
Correct |
246 ms |
83024 KB |
Output is correct |
51 |
Correct |
211 ms |
82772 KB |
Output is correct |
52 |
Correct |
262 ms |
82772 KB |
Output is correct |
53 |
Correct |
46 ms |
82256 KB |
Output is correct |
54 |
Correct |
455 ms |
81560 KB |
Output is correct |
55 |
Correct |
614 ms |
84704 KB |
Output is correct |
56 |
Correct |
602 ms |
84664 KB |
Output is correct |
57 |
Correct |
190 ms |
85244 KB |
Output is correct |
58 |
Correct |
429 ms |
85076 KB |
Output is correct |
59 |
Correct |
533 ms |
84696 KB |
Output is correct |
60 |
Correct |
515 ms |
84684 KB |
Output is correct |
61 |
Correct |
434 ms |
85008 KB |
Output is correct |
62 |
Correct |
210 ms |
82928 KB |
Output is correct |
63 |
Correct |
472 ms |
84908 KB |
Output is correct |
64 |
Correct |
317 ms |
84308 KB |
Output is correct |
65 |
Correct |
485 ms |
84680 KB |
Output is correct |
66 |
Correct |
6 ms |
57688 KB |
Output is correct |
67 |
Correct |
440 ms |
84632 KB |
Output is correct |
68 |
Correct |
610 ms |
84712 KB |
Output is correct |
69 |
Correct |
586 ms |
84824 KB |
Output is correct |
70 |
Correct |
580 ms |
85044 KB |
Output is correct |
71 |
Correct |
558 ms |
84700 KB |
Output is correct |
72 |
Correct |
531 ms |
84832 KB |
Output is correct |
73 |
Correct |
188 ms |
85148 KB |
Output is correct |
74 |
Correct |
189 ms |
85140 KB |
Output is correct |
75 |
Correct |
438 ms |
85328 KB |
Output is correct |
76 |
Correct |
362 ms |
85072 KB |
Output is correct |
77 |
Correct |
186 ms |
84660 KB |
Output is correct |
78 |
Correct |
6 ms |
57688 KB |
Output is correct |
79 |
Correct |
8 ms |
57948 KB |
Output is correct |
80 |
Correct |
8 ms |
57944 KB |
Output is correct |
81 |
Correct |
8 ms |
57948 KB |
Output is correct |
82 |
Correct |
8 ms |
57948 KB |
Output is correct |
83 |
Correct |
8 ms |
57932 KB |
Output is correct |
84 |
Correct |
7 ms |
57944 KB |
Output is correct |
85 |
Correct |
6 ms |
57948 KB |
Output is correct |
86 |
Correct |
202 ms |
61672 KB |
Output is correct |
87 |
Correct |
222 ms |
61448 KB |
Output is correct |
88 |
Correct |
140 ms |
61672 KB |
Output is correct |
89 |
Correct |
60 ms |
61780 KB |
Output is correct |
90 |
Correct |
68 ms |
61572 KB |
Output is correct |
91 |
Correct |
67 ms |
61524 KB |
Output is correct |
92 |
Correct |
149 ms |
61264 KB |
Output is correct |
93 |
Correct |
271 ms |
83472 KB |
Output is correct |
94 |
Correct |
266 ms |
82100 KB |
Output is correct |
95 |
Correct |
201 ms |
82752 KB |
Output is correct |
96 |
Correct |
241 ms |
82964 KB |
Output is correct |
97 |
Correct |
207 ms |
82984 KB |
Output is correct |
98 |
Correct |
262 ms |
82772 KB |
Output is correct |
99 |
Correct |
46 ms |
82260 KB |
Output is correct |
100 |
Correct |
544 ms |
84876 KB |
Output is correct |
101 |
Correct |
544 ms |
84672 KB |
Output is correct |
102 |
Correct |
442 ms |
85472 KB |
Output is correct |
103 |
Correct |
471 ms |
84304 KB |
Output is correct |
104 |
Correct |
318 ms |
84364 KB |
Output is correct |
105 |
Correct |
484 ms |
84508 KB |
Output is correct |
106 |
Correct |
442 ms |
84116 KB |
Output is correct |
107 |
Correct |
564 ms |
84144 KB |
Output is correct |
108 |
Correct |
369 ms |
84716 KB |
Output is correct |
109 |
Correct |
306 ms |
84792 KB |
Output is correct |
110 |
Correct |
384 ms |
84760 KB |
Output is correct |
111 |
Correct |
371 ms |
84744 KB |
Output is correct |