#include <bits/stdc++.h>
using namespace std;
template<typename T>
struct SparseTable
{
int n;
vector<vector<T>> mat;
SparseTable(const vector<T>& a)
{
n = int(a.size());
int maxLog = 32 - __builtin_clz(n);
mat.resize(maxLog);
mat[0] = a;
for (int j = 1; j < maxLog; j++)
{
mat[j].resize(n - (1 << j) + 1);
for (int i = 0; i <= n - (1 << j); i++)
mat[j][i] = min(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]);
}
}
T get(int from, int to)
{
assert(0 <= from && from <= to && to <= n - 1);
int lg = 31 - __builtin_clz(to - from + 1);
return min(mat[lg][from], mat[lg][to - (1 << lg) + 1]);
}
};
const int oo = 27081993;
const int LG = 17;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<pair<int, int>> a;
map<int, int> values;
for (int i = 0; i < n; i++)
{
int l, r;
cin >> l >> r;
a.push_back({l, r});
values[l] = values[r] = 0;
}
int valueCnt = 0;
for (auto [k, _] : values)
values[k] = valueCnt++;
vector<pair<int, int>> minL(valueCnt, make_pair(oo, -1));
for (int i = 0; i < n; i++)
{
auto [l, r] = a[i];
l = values[l];
r = values[r];
a[i] = {l, r};
minL[r] = min(minL[r], make_pair(l, i));
}
SparseTable<pair<int, int>> st(minL);
vector<vector<int>> prev(n, vector<int>(LG, -1));
for (int i = 0; i < n; i++)
{
auto [l, id] = st.get(a[i].first, a[i].second);
if (l < a[i].first)
prev[i][0] = id;
}
for (int j = 0; j + 1 < LG; j++)
for (int i = 0; i < n; i++)
if (prev[i][j] >= 0)
prev[i][j + 1] = prev[prev[i][j]][j];
while (q--)
{
int s, t;
cin >> s >> t;
s--; t--;
if (s == t) cout << "0\n";
else if (a[t].first <= a[s].second && a[s].second <= a[t].second) cout << "1\n";
else if (a[s].second > a[t].second) cout << "impossible\n";
else
{
int ans = 1;
for (int i = LG - 1; i >= 0; i--)
if (prev[t][i] >= 0 && a[prev[t][i]].first > a[s].second)
{
ans += 1 << i;
t = prev[t][i];
}
if (prev[t][0] >= 0 && a[prev[t][0]].first <= a[s].second) ans++;
else ans = -1;
if (ans < 0) cout << "impossible\n";
else cout << ans << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
149 ms |
32884 KB |
Output is correct |
3 |
Correct |
177 ms |
32688 KB |
Output is correct |
4 |
Correct |
179 ms |
32700 KB |
Output is correct |
5 |
Correct |
188 ms |
34756 KB |
Output is correct |
6 |
Correct |
184 ms |
36540 KB |
Output is correct |
7 |
Correct |
180 ms |
37052 KB |
Output is correct |
8 |
Correct |
171 ms |
52668 KB |
Output is correct |
9 |
Correct |
159 ms |
52668 KB |
Output is correct |
10 |
Correct |
159 ms |
33164 KB |
Output is correct |
11 |
Correct |
161 ms |
40824 KB |
Output is correct |
12 |
Correct |
89 ms |
32444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
548 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
548 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
756 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
848 KB |
Output is correct |
17 |
Correct |
2 ms |
848 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
26 ms |
3264 KB |
Output is correct |
20 |
Correct |
28 ms |
3152 KB |
Output is correct |
21 |
Correct |
29 ms |
3240 KB |
Output is correct |
22 |
Correct |
19 ms |
3408 KB |
Output is correct |
23 |
Correct |
20 ms |
4168 KB |
Output is correct |
24 |
Correct |
19 ms |
3992 KB |
Output is correct |
25 |
Correct |
28 ms |
3408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
548 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
760 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
848 KB |
Output is correct |
17 |
Correct |
2 ms |
848 KB |
Output is correct |
18 |
Correct |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
122 ms |
31044 KB |
Output is correct |
20 |
Correct |
123 ms |
30268 KB |
Output is correct |
21 |
Correct |
137 ms |
30908 KB |
Output is correct |
22 |
Correct |
149 ms |
38076 KB |
Output is correct |
23 |
Correct |
172 ms |
50336 KB |
Output is correct |
24 |
Correct |
198 ms |
50364 KB |
Output is correct |
25 |
Correct |
28 ms |
12492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
29628 KB |
Output is correct |
2 |
Correct |
169 ms |
29628 KB |
Output is correct |
3 |
Correct |
173 ms |
29636 KB |
Output is correct |
4 |
Correct |
178 ms |
49596 KB |
Output is correct |
5 |
Correct |
191 ms |
33224 KB |
Output is correct |
6 |
Correct |
219 ms |
52156 KB |
Output is correct |
7 |
Correct |
227 ms |
52368 KB |
Output is correct |
8 |
Correct |
212 ms |
50412 KB |
Output is correct |
9 |
Correct |
161 ms |
50364 KB |
Output is correct |
10 |
Correct |
236 ms |
51900 KB |
Output is correct |
11 |
Correct |
209 ms |
51652 KB |
Output is correct |
12 |
Correct |
210 ms |
52004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
149 ms |
32884 KB |
Output is correct |
3 |
Correct |
177 ms |
32688 KB |
Output is correct |
4 |
Correct |
179 ms |
32700 KB |
Output is correct |
5 |
Correct |
188 ms |
34756 KB |
Output is correct |
6 |
Correct |
184 ms |
36540 KB |
Output is correct |
7 |
Correct |
180 ms |
37052 KB |
Output is correct |
8 |
Correct |
171 ms |
52668 KB |
Output is correct |
9 |
Correct |
159 ms |
52668 KB |
Output is correct |
10 |
Correct |
159 ms |
33164 KB |
Output is correct |
11 |
Correct |
161 ms |
40824 KB |
Output is correct |
12 |
Correct |
89 ms |
32444 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
548 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
760 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
2 ms |
592 KB |
Output is correct |
19 |
Correct |
2 ms |
848 KB |
Output is correct |
20 |
Correct |
2 ms |
848 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
756 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
848 KB |
Output is correct |
29 |
Correct |
2 ms |
848 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
26 ms |
3264 KB |
Output is correct |
32 |
Correct |
28 ms |
3152 KB |
Output is correct |
33 |
Correct |
29 ms |
3240 KB |
Output is correct |
34 |
Correct |
19 ms |
3408 KB |
Output is correct |
35 |
Correct |
20 ms |
4168 KB |
Output is correct |
36 |
Correct |
19 ms |
3992 KB |
Output is correct |
37 |
Correct |
28 ms |
3408 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
2 ms |
592 KB |
Output is correct |
42 |
Correct |
2 ms |
592 KB |
Output is correct |
43 |
Correct |
2 ms |
592 KB |
Output is correct |
44 |
Correct |
2 ms |
848 KB |
Output is correct |
45 |
Correct |
2 ms |
848 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
122 ms |
31044 KB |
Output is correct |
48 |
Correct |
123 ms |
30268 KB |
Output is correct |
49 |
Correct |
137 ms |
30908 KB |
Output is correct |
50 |
Correct |
149 ms |
38076 KB |
Output is correct |
51 |
Correct |
172 ms |
50336 KB |
Output is correct |
52 |
Correct |
198 ms |
50364 KB |
Output is correct |
53 |
Correct |
28 ms |
12492 KB |
Output is correct |
54 |
Correct |
145 ms |
29628 KB |
Output is correct |
55 |
Correct |
169 ms |
29628 KB |
Output is correct |
56 |
Correct |
173 ms |
29636 KB |
Output is correct |
57 |
Correct |
178 ms |
49596 KB |
Output is correct |
58 |
Correct |
191 ms |
33224 KB |
Output is correct |
59 |
Correct |
219 ms |
52156 KB |
Output is correct |
60 |
Correct |
227 ms |
52368 KB |
Output is correct |
61 |
Correct |
212 ms |
50412 KB |
Output is correct |
62 |
Correct |
161 ms |
50364 KB |
Output is correct |
63 |
Correct |
236 ms |
51900 KB |
Output is correct |
64 |
Correct |
209 ms |
51652 KB |
Output is correct |
65 |
Correct |
210 ms |
52004 KB |
Output is correct |
66 |
Correct |
1 ms |
336 KB |
Output is correct |
67 |
Correct |
136 ms |
32792 KB |
Output is correct |
68 |
Correct |
149 ms |
32640 KB |
Output is correct |
69 |
Correct |
164 ms |
32188 KB |
Output is correct |
70 |
Correct |
175 ms |
34748 KB |
Output is correct |
71 |
Correct |
179 ms |
35516 KB |
Output is correct |
72 |
Correct |
163 ms |
37052 KB |
Output is correct |
73 |
Correct |
165 ms |
52736 KB |
Output is correct |
74 |
Correct |
163 ms |
52668 KB |
Output is correct |
75 |
Correct |
184 ms |
33212 KB |
Output is correct |
76 |
Correct |
165 ms |
40644 KB |
Output is correct |
77 |
Correct |
95 ms |
32600 KB |
Output is correct |
78 |
Correct |
1 ms |
336 KB |
Output is correct |
79 |
Correct |
2 ms |
592 KB |
Output is correct |
80 |
Correct |
2 ms |
524 KB |
Output is correct |
81 |
Correct |
2 ms |
760 KB |
Output is correct |
82 |
Correct |
2 ms |
592 KB |
Output is correct |
83 |
Correct |
2 ms |
728 KB |
Output is correct |
84 |
Correct |
2 ms |
848 KB |
Output is correct |
85 |
Correct |
2 ms |
592 KB |
Output is correct |
86 |
Correct |
28 ms |
3400 KB |
Output is correct |
87 |
Correct |
30 ms |
3156 KB |
Output is correct |
88 |
Correct |
31 ms |
3480 KB |
Output is correct |
89 |
Correct |
21 ms |
3400 KB |
Output is correct |
90 |
Correct |
22 ms |
4176 KB |
Output is correct |
91 |
Correct |
29 ms |
4176 KB |
Output is correct |
92 |
Correct |
26 ms |
3664 KB |
Output is correct |
93 |
Correct |
125 ms |
31164 KB |
Output is correct |
94 |
Correct |
119 ms |
30140 KB |
Output is correct |
95 |
Correct |
131 ms |
30844 KB |
Output is correct |
96 |
Correct |
145 ms |
38088 KB |
Output is correct |
97 |
Correct |
168 ms |
50364 KB |
Output is correct |
98 |
Correct |
199 ms |
50392 KB |
Output is correct |
99 |
Correct |
26 ms |
12484 KB |
Output is correct |
100 |
Correct |
209 ms |
52156 KB |
Output is correct |
101 |
Correct |
220 ms |
52156 KB |
Output is correct |
102 |
Correct |
230 ms |
52488 KB |
Output is correct |
103 |
Correct |
218 ms |
51900 KB |
Output is correct |
104 |
Correct |
225 ms |
51644 KB |
Output is correct |
105 |
Correct |
227 ms |
51900 KB |
Output is correct |
106 |
Correct |
148 ms |
27580 KB |
Output is correct |
107 |
Correct |
188 ms |
32064 KB |
Output is correct |
108 |
Correct |
139 ms |
32980 KB |
Output is correct |
109 |
Correct |
136 ms |
32956 KB |
Output is correct |
110 |
Correct |
195 ms |
52156 KB |
Output is correct |
111 |
Correct |
196 ms |
52160 KB |
Output is correct |