// https://github.com/tmwilliamlin168/CompetitiveProgramming/blob/master/JOI/17S-Railway(2).cpp
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5, LG = 17;
int n, k, q;
int a[N], L[LG][N], R[LG][N];
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> k >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<int> st;
for (int i = 1; i <= n; ++i) {
while (st.size() && a[st.back()] < a[i]) {
st.pop_back();
}
L[0][i] = st.size() ? st.back() : 1;
st.push_back(i);
}
st.clear();
for (int i = n; i > 0; --i) {
while (st.size() && a[st.back()] < a[i]) {
st.pop_back();
}
R[0][i] = st.size() ? st.back() : n;
st.push_back(i);
}
for (int j = 1; j < LG; ++j) {
for (int i = 1; i <= n; ++i) {
int u = L[j - 1][i], v = R[j - 1][i];
L[j][i] = min(L[j - 1][u], L[j - 1][v]);
R[j][i] = max(R[j - 1][u], R[j - 1][v]);
}
}
while (q--) {
int s, t; cin >> s >> t;
if (s > t) {
swap(s, t);
}
int l = s, r = s, res = 0, b = t;
for (int j = LG - 1; ~j; --j) {
if (max(R[j][l], R[j][r]) < b) {
res += 1 << j;
int u = l, v = r;
l = min(L[j][u], L[j][v]);
r = max(R[j][u], R[j][v]);
}
}
b = r, l = t, r = t;
for (int j = LG - 1; ~j; --j) {
if (min(L[j][l], L[j][r]) > b) {
res += 1 << j;
int u = l, v = r;
l = min(L[j][u], L[j][v]);
r = max(R[j][u], R[j][v]);
}
}
cout << res << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12756 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
12636 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
1 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12632 KB |
Output is correct |
2 |
Correct |
8 ms |
14428 KB |
Output is correct |
3 |
Correct |
9 ms |
14424 KB |
Output is correct |
4 |
Correct |
8 ms |
14428 KB |
Output is correct |
5 |
Correct |
9 ms |
14424 KB |
Output is correct |
6 |
Correct |
10 ms |
14428 KB |
Output is correct |
7 |
Correct |
9 ms |
14684 KB |
Output is correct |
8 |
Correct |
7 ms |
14744 KB |
Output is correct |
9 |
Correct |
8 ms |
15068 KB |
Output is correct |
10 |
Correct |
8 ms |
14812 KB |
Output is correct |
11 |
Correct |
10 ms |
14816 KB |
Output is correct |
12 |
Correct |
9 ms |
14812 KB |
Output is correct |
13 |
Correct |
9 ms |
14816 KB |
Output is correct |
14 |
Correct |
14 ms |
14808 KB |
Output is correct |
15 |
Correct |
15 ms |
14812 KB |
Output is correct |
16 |
Correct |
9 ms |
14816 KB |
Output is correct |
17 |
Correct |
9 ms |
14680 KB |
Output is correct |
18 |
Correct |
9 ms |
14508 KB |
Output is correct |
19 |
Correct |
8 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
15964 KB |
Output is correct |
2 |
Correct |
43 ms |
15952 KB |
Output is correct |
3 |
Correct |
43 ms |
15952 KB |
Output is correct |
4 |
Correct |
44 ms |
15952 KB |
Output is correct |
5 |
Correct |
44 ms |
15812 KB |
Output is correct |
6 |
Correct |
45 ms |
15996 KB |
Output is correct |
7 |
Correct |
43 ms |
15952 KB |
Output is correct |
8 |
Correct |
45 ms |
16348 KB |
Output is correct |
9 |
Correct |
62 ms |
16504 KB |
Output is correct |
10 |
Correct |
58 ms |
16600 KB |
Output is correct |
11 |
Correct |
58 ms |
16588 KB |
Output is correct |
12 |
Correct |
57 ms |
16588 KB |
Output is correct |
13 |
Correct |
65 ms |
16428 KB |
Output is correct |
14 |
Correct |
55 ms |
16212 KB |
Output is correct |
15 |
Correct |
47 ms |
15956 KB |
Output is correct |
16 |
Correct |
63 ms |
15808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
15960 KB |
Output is correct |
2 |
Correct |
35 ms |
16036 KB |
Output is correct |
3 |
Correct |
33 ms |
16044 KB |
Output is correct |
4 |
Correct |
33 ms |
15964 KB |
Output is correct |
5 |
Correct |
59 ms |
16592 KB |
Output is correct |
6 |
Correct |
60 ms |
16592 KB |
Output is correct |
7 |
Correct |
49 ms |
16756 KB |
Output is correct |
8 |
Correct |
60 ms |
16592 KB |
Output is correct |
9 |
Correct |
48 ms |
16600 KB |
Output is correct |
10 |
Correct |
58 ms |
16596 KB |
Output is correct |
11 |
Correct |
49 ms |
16600 KB |
Output is correct |
12 |
Correct |
67 ms |
16596 KB |
Output is correct |
13 |
Correct |
48 ms |
16600 KB |
Output is correct |
14 |
Correct |
50 ms |
16596 KB |
Output is correct |
15 |
Correct |
50 ms |
16600 KB |
Output is correct |
16 |
Correct |
50 ms |
16592 KB |
Output is correct |
17 |
Correct |
51 ms |
16468 KB |
Output is correct |
18 |
Correct |
51 ms |
16464 KB |
Output is correct |
19 |
Correct |
50 ms |
16472 KB |
Output is correct |
20 |
Correct |
46 ms |
16212 KB |
Output is correct |
21 |
Correct |
34 ms |
16108 KB |
Output is correct |
22 |
Correct |
33 ms |
15952 KB |
Output is correct |
23 |
Correct |
31 ms |
15964 KB |
Output is correct |