#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010, SZ = 1200;
int n, q, b[maxn], rmq[17][maxn], bel[maxn], bl[maxn], br[maxn];
int num[maxn / SZ + 5][SZ + 5], tmp[maxn], sz[maxn], rb[maxn];
short pos[maxn][SZ + 5], tr[maxn][SZ + 5];
array<int, 2> a[maxn];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> q;
vector<int> V;
for (int i = 1; i <= n; i++) {
cin >> a[i][0] >> a[i][1], V.push_back(a[i][1]);
}
sort(V.begin(), V.end());
V.resize(unique(V.begin(), V.end()) - V.begin());
fill(b + 1, b + n + 1, n + 1);
for (int i = 1; i <= n; i++) {
a[i][0] = lower_bound(V.begin(), V.end(), a[i][0]) - V.begin() + 1;
a[i][1] = lower_bound(V.begin(), V.end(), a[i][1]) - V.begin() + 1;
b[a[i][1]] = min(b[a[i][1]], a[i][0]);
}
for (int i = 1; i <= n; i++) {
br[bel[i] = (i - 1) / SZ + 1] = i;
if (!bl[bel[i]]) bl[bel[i]] = i;
}
copy(b + 1, b + n + 1, rmq[0] + 1);
for (int k = 1; k < 17; k++) {
memcpy(rmq[k], rmq[k - 1], sizeof(rmq[k]));
for (int i = (1 << (k - 1)) + 1; i <= n; i++) {
rmq[k][i] = min(rmq[k][i], rmq[k - 1][i - (1 << (k - 1))]);
}
}
auto qmin = [&](int l, int r) {
int k = __lg(r - l + 1);
return min(rmq[k][l + (1 << k) - 1], rmq[k][r]);
};
auto go = [&](int s, int r) {
r = min(r, rb[s]);
if (b[r] <= s) return r;
for (int i = __lg(r - s + 1); ~i && r > s; i--) {
if (rmq[i][r] > s) r -= 1 << i;
}
return r <= s ? 0 : r;
};
for (int i = 1; i <= n; i++) {
rb[i] = n, rb[i] = go(i, n);
}
for (int k = 1; k <= bel[n]; k++) {
for (int i = br[k], j = br[k]; i >= bl[k]; i--) {
for (; j >= max(bl[k], b[i]); j--) num[k][j - bl[k]] = j < i ? i : 0;
}
}
for (int i = 1; i <= n; i++) {
int k = bel[i], L = bl[k], R = br[k];
for (int j = num[k][i - L]; j && j <= R; j = num[k][j - L]) {
tr[i][sz[i]++] = j - L;
}
for (int j = 0, k = 0; j <= R - L; j++) {
while (k < sz[i] && tr[i][k] < j) k++;
pos[i][j] = k;
}
}
while (q--) {
int s, t, ans = 0;
cin >> s >> t;
if (s == t) { cout << "0\n"; continue; }
for (s = a[s][1]; s && s < a[t][0]; ans++) {
if (bel[s] == bel[a[t][0]]) {
for (int i = a[t][0], j = a[t][0]; i >= s; i--) {
int v = max(s, i == a[t][0] ? qmin(i, a[t][1]) : b[i]);
for (; j >= v; j--) tmp[j] = j < i ? i : 0;
}
for (; s && s < a[t][0]; ans++) s = tmp[s];
break;
}
int nxt = qmin(bl[bel[s] + 1], a[t][1]);
if (nxt <= s) {
s = go(s, a[t][1]);
} else {
if (!sz[s] || nxt - bl[bel[s]] > tr[s][sz[s] - 1]) { s = 0; break; }
int i = pos[s][nxt - bl[bel[s]]];
ans += i, s = bl[bel[s]] + tr[s][i];
if (s < a[t][0]) s = go(s, a[t][1]), ans++;
}
}
if (a[t][0] <= s && s <= a[t][1]) cout << ans + 1 << "\n";
else cout << "impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6656 KB |
Output is correct |
2 |
Correct |
1110 ms |
482428 KB |
Output is correct |
3 |
Correct |
1072 ms |
482456 KB |
Output is correct |
4 |
Correct |
1174 ms |
482488 KB |
Output is correct |
5 |
Correct |
1174 ms |
482364 KB |
Output is correct |
6 |
Correct |
1286 ms |
482420 KB |
Output is correct |
7 |
Correct |
1244 ms |
482420 KB |
Output is correct |
8 |
Correct |
309 ms |
247652 KB |
Output is correct |
9 |
Correct |
299 ms |
246780 KB |
Output is correct |
10 |
Correct |
845 ms |
483180 KB |
Output is correct |
11 |
Correct |
877 ms |
483052 KB |
Output is correct |
12 |
Correct |
225 ms |
246492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6588 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
8 ms |
11348 KB |
Output is correct |
4 |
Correct |
8 ms |
11292 KB |
Output is correct |
5 |
Correct |
8 ms |
11348 KB |
Output is correct |
6 |
Correct |
6 ms |
10196 KB |
Output is correct |
7 |
Correct |
8 ms |
11348 KB |
Output is correct |
8 |
Correct |
6 ms |
11348 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6588 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
8 ms |
11348 KB |
Output is correct |
4 |
Correct |
8 ms |
11292 KB |
Output is correct |
5 |
Correct |
8 ms |
11348 KB |
Output is correct |
6 |
Correct |
6 ms |
10196 KB |
Output is correct |
7 |
Correct |
8 ms |
11348 KB |
Output is correct |
8 |
Correct |
6 ms |
11348 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
6612 KB |
Output is correct |
11 |
Correct |
4 ms |
6612 KB |
Output is correct |
12 |
Correct |
8 ms |
11292 KB |
Output is correct |
13 |
Correct |
8 ms |
11304 KB |
Output is correct |
14 |
Correct |
8 ms |
11348 KB |
Output is correct |
15 |
Correct |
7 ms |
10196 KB |
Output is correct |
16 |
Correct |
7 ms |
11344 KB |
Output is correct |
17 |
Correct |
9 ms |
11348 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
301 ms |
30868 KB |
Output is correct |
20 |
Correct |
279 ms |
30856 KB |
Output is correct |
21 |
Correct |
206 ms |
31136 KB |
Output is correct |
22 |
Correct |
57 ms |
25332 KB |
Output is correct |
23 |
Correct |
52 ms |
31092 KB |
Output is correct |
24 |
Correct |
44 ms |
31004 KB |
Output is correct |
25 |
Correct |
233 ms |
30620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6588 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
8 ms |
11348 KB |
Output is correct |
4 |
Correct |
8 ms |
11292 KB |
Output is correct |
5 |
Correct |
8 ms |
11348 KB |
Output is correct |
6 |
Correct |
6 ms |
10196 KB |
Output is correct |
7 |
Correct |
8 ms |
11348 KB |
Output is correct |
8 |
Correct |
6 ms |
11348 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
3 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
9 ms |
11348 KB |
Output is correct |
13 |
Correct |
8 ms |
11360 KB |
Output is correct |
14 |
Correct |
7 ms |
11348 KB |
Output is correct |
15 |
Correct |
6 ms |
10136 KB |
Output is correct |
16 |
Correct |
7 ms |
11348 KB |
Output is correct |
17 |
Correct |
7 ms |
11348 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
625 ms |
481716 KB |
Output is correct |
20 |
Correct |
628 ms |
481996 KB |
Output is correct |
21 |
Correct |
315 ms |
363028 KB |
Output is correct |
22 |
Correct |
665 ms |
481808 KB |
Output is correct |
23 |
Correct |
370 ms |
481756 KB |
Output is correct |
24 |
Correct |
439 ms |
481772 KB |
Output is correct |
25 |
Correct |
195 ms |
245196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1059 ms |
482372 KB |
Output is correct |
2 |
Correct |
999 ms |
482308 KB |
Output is correct |
3 |
Correct |
1045 ms |
482616 KB |
Output is correct |
4 |
Correct |
243 ms |
246736 KB |
Output is correct |
5 |
Correct |
732 ms |
483308 KB |
Output is correct |
6 |
Correct |
686 ms |
483052 KB |
Output is correct |
7 |
Correct |
653 ms |
482932 KB |
Output is correct |
8 |
Correct |
619 ms |
482804 KB |
Output is correct |
9 |
Correct |
343 ms |
481944 KB |
Output is correct |
10 |
Correct |
1372 ms |
482248 KB |
Output is correct |
11 |
Correct |
373 ms |
481900 KB |
Output is correct |
12 |
Correct |
1036 ms |
482136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6656 KB |
Output is correct |
2 |
Correct |
1110 ms |
482428 KB |
Output is correct |
3 |
Correct |
1072 ms |
482456 KB |
Output is correct |
4 |
Correct |
1174 ms |
482488 KB |
Output is correct |
5 |
Correct |
1174 ms |
482364 KB |
Output is correct |
6 |
Correct |
1286 ms |
482420 KB |
Output is correct |
7 |
Correct |
1244 ms |
482420 KB |
Output is correct |
8 |
Correct |
309 ms |
247652 KB |
Output is correct |
9 |
Correct |
299 ms |
246780 KB |
Output is correct |
10 |
Correct |
845 ms |
483180 KB |
Output is correct |
11 |
Correct |
877 ms |
483052 KB |
Output is correct |
12 |
Correct |
225 ms |
246492 KB |
Output is correct |
13 |
Correct |
5 ms |
6588 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
15 |
Correct |
8 ms |
11348 KB |
Output is correct |
16 |
Correct |
8 ms |
11292 KB |
Output is correct |
17 |
Correct |
8 ms |
11348 KB |
Output is correct |
18 |
Correct |
6 ms |
10196 KB |
Output is correct |
19 |
Correct |
8 ms |
11348 KB |
Output is correct |
20 |
Correct |
6 ms |
11348 KB |
Output is correct |
21 |
Correct |
5 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
4 ms |
6612 KB |
Output is correct |
24 |
Correct |
8 ms |
11292 KB |
Output is correct |
25 |
Correct |
8 ms |
11304 KB |
Output is correct |
26 |
Correct |
8 ms |
11348 KB |
Output is correct |
27 |
Correct |
7 ms |
10196 KB |
Output is correct |
28 |
Correct |
7 ms |
11344 KB |
Output is correct |
29 |
Correct |
9 ms |
11348 KB |
Output is correct |
30 |
Correct |
5 ms |
8916 KB |
Output is correct |
31 |
Correct |
301 ms |
30868 KB |
Output is correct |
32 |
Correct |
279 ms |
30856 KB |
Output is correct |
33 |
Correct |
206 ms |
31136 KB |
Output is correct |
34 |
Correct |
57 ms |
25332 KB |
Output is correct |
35 |
Correct |
52 ms |
31092 KB |
Output is correct |
36 |
Correct |
44 ms |
31004 KB |
Output is correct |
37 |
Correct |
233 ms |
30620 KB |
Output is correct |
38 |
Correct |
3 ms |
6612 KB |
Output is correct |
39 |
Correct |
3 ms |
6612 KB |
Output is correct |
40 |
Correct |
9 ms |
11348 KB |
Output is correct |
41 |
Correct |
8 ms |
11360 KB |
Output is correct |
42 |
Correct |
7 ms |
11348 KB |
Output is correct |
43 |
Correct |
6 ms |
10136 KB |
Output is correct |
44 |
Correct |
7 ms |
11348 KB |
Output is correct |
45 |
Correct |
7 ms |
11348 KB |
Output is correct |
46 |
Correct |
5 ms |
8916 KB |
Output is correct |
47 |
Correct |
625 ms |
481716 KB |
Output is correct |
48 |
Correct |
628 ms |
481996 KB |
Output is correct |
49 |
Correct |
315 ms |
363028 KB |
Output is correct |
50 |
Correct |
665 ms |
481808 KB |
Output is correct |
51 |
Correct |
370 ms |
481756 KB |
Output is correct |
52 |
Correct |
439 ms |
481772 KB |
Output is correct |
53 |
Correct |
195 ms |
245196 KB |
Output is correct |
54 |
Correct |
1059 ms |
482372 KB |
Output is correct |
55 |
Correct |
999 ms |
482308 KB |
Output is correct |
56 |
Correct |
1045 ms |
482616 KB |
Output is correct |
57 |
Correct |
243 ms |
246736 KB |
Output is correct |
58 |
Correct |
732 ms |
483308 KB |
Output is correct |
59 |
Correct |
686 ms |
483052 KB |
Output is correct |
60 |
Correct |
653 ms |
482932 KB |
Output is correct |
61 |
Correct |
619 ms |
482804 KB |
Output is correct |
62 |
Correct |
343 ms |
481944 KB |
Output is correct |
63 |
Correct |
1372 ms |
482248 KB |
Output is correct |
64 |
Correct |
373 ms |
481900 KB |
Output is correct |
65 |
Correct |
1036 ms |
482136 KB |
Output is correct |
66 |
Correct |
3 ms |
6612 KB |
Output is correct |
67 |
Correct |
1068 ms |
482372 KB |
Output is correct |
68 |
Correct |
1006 ms |
482444 KB |
Output is correct |
69 |
Correct |
1040 ms |
482508 KB |
Output is correct |
70 |
Correct |
1056 ms |
482544 KB |
Output is correct |
71 |
Correct |
1079 ms |
482448 KB |
Output is correct |
72 |
Correct |
1095 ms |
482344 KB |
Output is correct |
73 |
Correct |
236 ms |
247600 KB |
Output is correct |
74 |
Correct |
239 ms |
246756 KB |
Output is correct |
75 |
Correct |
739 ms |
483132 KB |
Output is correct |
76 |
Correct |
828 ms |
483148 KB |
Output is correct |
77 |
Correct |
215 ms |
246576 KB |
Output is correct |
78 |
Correct |
3 ms |
6612 KB |
Output is correct |
79 |
Correct |
9 ms |
11404 KB |
Output is correct |
80 |
Correct |
7 ms |
11304 KB |
Output is correct |
81 |
Correct |
7 ms |
11380 KB |
Output is correct |
82 |
Correct |
7 ms |
10180 KB |
Output is correct |
83 |
Correct |
9 ms |
11348 KB |
Output is correct |
84 |
Correct |
6 ms |
11348 KB |
Output is correct |
85 |
Correct |
5 ms |
8916 KB |
Output is correct |
86 |
Correct |
282 ms |
30828 KB |
Output is correct |
87 |
Correct |
267 ms |
30924 KB |
Output is correct |
88 |
Correct |
196 ms |
31176 KB |
Output is correct |
89 |
Correct |
52 ms |
25288 KB |
Output is correct |
90 |
Correct |
48 ms |
31052 KB |
Output is correct |
91 |
Correct |
38 ms |
31004 KB |
Output is correct |
92 |
Correct |
215 ms |
30712 KB |
Output is correct |
93 |
Correct |
580 ms |
481740 KB |
Output is correct |
94 |
Correct |
565 ms |
481912 KB |
Output is correct |
95 |
Correct |
304 ms |
363100 KB |
Output is correct |
96 |
Correct |
633 ms |
481740 KB |
Output is correct |
97 |
Correct |
339 ms |
481912 KB |
Output is correct |
98 |
Correct |
413 ms |
481740 KB |
Output is correct |
99 |
Correct |
192 ms |
245308 KB |
Output is correct |
100 |
Correct |
645 ms |
482904 KB |
Output is correct |
101 |
Correct |
649 ms |
482944 KB |
Output is correct |
102 |
Correct |
636 ms |
482768 KB |
Output is correct |
103 |
Correct |
1363 ms |
482160 KB |
Output is correct |
104 |
Correct |
384 ms |
481904 KB |
Output is correct |
105 |
Correct |
1022 ms |
482248 KB |
Output is correct |
106 |
Correct |
717 ms |
423684 KB |
Output is correct |
107 |
Correct |
1096 ms |
482240 KB |
Output is correct |
108 |
Correct |
330 ms |
364104 KB |
Output is correct |
109 |
Correct |
329 ms |
364008 KB |
Output is correct |
110 |
Correct |
412 ms |
482780 KB |
Output is correct |
111 |
Correct |
430 ms |
482640 KB |
Output is correct |