#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';
const int N = 1e5+7, LG = 20;
int n, k, q;
int l[N][LG], r[N][LG], a[N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> k >> q;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < LG; ++j) {
l[i][j] = 0;
r[i][j] = n-1;
}
}
{
vector <int> s;
for (int i = 0; i < n; ++i) {
while (s.size() && a[s.back()] <= a[i]) {
r[s.back()][0] = i;
s.pop_back();
}
s.app(i);
}
}
{
vector <int> s;
for (int i = n - 1; i >= 0; --i) {
while (s.size() && a[s.back()] <= a[i]) {
l[s.back()][0] = i;
s.pop_back();
}
s.app(i);
}
}
for (int bit = 1; bit < LG; ++bit) {
for (int i = 0; i < n; ++i) {
l[i][bit] = min(l[l[i][bit-1]][bit-1], l[r[i][bit-1]][bit-1]);
r[i][bit] = max(r[l[i][bit-1]][bit-1], r[r[i][bit-1]][bit-1]);
}
}
while (q--) {
int u, v;
cin >> u >> v;
--u; --v;
if (v < u) {
swap(u, v);
}
//debug(u);
//debug(v);
int ans = 0;
int lu = u, ru = u;
for (int i = LG - 1; i >= 0; --i) {
int l1 = min(l[lu][i], l[ru][i]);
int r1 = max(r[lu][i], r[ru][i]);
if (r1 < v) {
ans += 1 << i;
lu = l1;
ru = r1;
}
}
//debug(lu);
//debug(ru);
//debug(ans);
int lv = v, rv = v;
for (int i = LG - 1; i >= 0; --i) {
int l1 = min(l[lv][i], l[rv][i]);
int r1 = max(r[lv][i], r[rv][i]);
if (l1 > ru) {
ans += 1 << i;
lv = l1;
rv = r1;
}
}
cout << ans << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
31616 KB |
Output is correct |
2 |
Correct |
21 ms |
31616 KB |
Output is correct |
3 |
Correct |
21 ms |
31744 KB |
Output is correct |
4 |
Correct |
21 ms |
31616 KB |
Output is correct |
5 |
Correct |
21 ms |
31616 KB |
Output is correct |
6 |
Correct |
21 ms |
31744 KB |
Output is correct |
7 |
Correct |
27 ms |
31608 KB |
Output is correct |
8 |
Correct |
28 ms |
31616 KB |
Output is correct |
9 |
Correct |
20 ms |
31616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
31616 KB |
Output is correct |
2 |
Correct |
73 ms |
32640 KB |
Output is correct |
3 |
Correct |
78 ms |
32768 KB |
Output is correct |
4 |
Correct |
71 ms |
32640 KB |
Output is correct |
5 |
Correct |
70 ms |
32768 KB |
Output is correct |
6 |
Correct |
72 ms |
32888 KB |
Output is correct |
7 |
Correct |
75 ms |
33072 KB |
Output is correct |
8 |
Correct |
72 ms |
32760 KB |
Output is correct |
9 |
Correct |
83 ms |
33400 KB |
Output is correct |
10 |
Correct |
78 ms |
33068 KB |
Output is correct |
11 |
Correct |
79 ms |
33144 KB |
Output is correct |
12 |
Correct |
82 ms |
33144 KB |
Output is correct |
13 |
Correct |
80 ms |
33272 KB |
Output is correct |
14 |
Correct |
81 ms |
33144 KB |
Output is correct |
15 |
Correct |
90 ms |
33148 KB |
Output is correct |
16 |
Correct |
77 ms |
33144 KB |
Output is correct |
17 |
Correct |
74 ms |
33016 KB |
Output is correct |
18 |
Correct |
77 ms |
33016 KB |
Output is correct |
19 |
Correct |
75 ms |
33144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
34296 KB |
Output is correct |
2 |
Correct |
184 ms |
34264 KB |
Output is correct |
3 |
Correct |
179 ms |
34296 KB |
Output is correct |
4 |
Correct |
175 ms |
34296 KB |
Output is correct |
5 |
Correct |
184 ms |
34424 KB |
Output is correct |
6 |
Correct |
172 ms |
34296 KB |
Output is correct |
7 |
Correct |
181 ms |
34296 KB |
Output is correct |
8 |
Correct |
230 ms |
34552 KB |
Output is correct |
9 |
Correct |
279 ms |
34424 KB |
Output is correct |
10 |
Correct |
274 ms |
34424 KB |
Output is correct |
11 |
Correct |
275 ms |
34552 KB |
Output is correct |
12 |
Correct |
271 ms |
34424 KB |
Output is correct |
13 |
Correct |
272 ms |
34552 KB |
Output is correct |
14 |
Correct |
252 ms |
34296 KB |
Output is correct |
15 |
Correct |
200 ms |
34220 KB |
Output is correct |
16 |
Correct |
180 ms |
34296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
34428 KB |
Output is correct |
2 |
Correct |
149 ms |
34424 KB |
Output is correct |
3 |
Correct |
148 ms |
34296 KB |
Output is correct |
4 |
Correct |
149 ms |
34432 KB |
Output is correct |
5 |
Correct |
275 ms |
34428 KB |
Output is correct |
6 |
Correct |
246 ms |
34904 KB |
Output is correct |
7 |
Correct |
236 ms |
34908 KB |
Output is correct |
8 |
Correct |
259 ms |
34908 KB |
Output is correct |
9 |
Correct |
230 ms |
34936 KB |
Output is correct |
10 |
Correct |
223 ms |
34808 KB |
Output is correct |
11 |
Correct |
240 ms |
34936 KB |
Output is correct |
12 |
Correct |
219 ms |
34808 KB |
Output is correct |
13 |
Correct |
225 ms |
34808 KB |
Output is correct |
14 |
Correct |
237 ms |
35036 KB |
Output is correct |
15 |
Correct |
234 ms |
34864 KB |
Output is correct |
16 |
Correct |
239 ms |
34836 KB |
Output is correct |
17 |
Correct |
214 ms |
34880 KB |
Output is correct |
18 |
Correct |
220 ms |
34980 KB |
Output is correct |
19 |
Correct |
218 ms |
34848 KB |
Output is correct |
20 |
Correct |
182 ms |
34552 KB |
Output is correct |
21 |
Correct |
156 ms |
34568 KB |
Output is correct |
22 |
Correct |
144 ms |
34424 KB |
Output is correct |
23 |
Correct |
149 ms |
34424 KB |
Output is correct |