This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 1e5;
const int MAXP = 20;
int goleft[MAXP][MAXN], goright[MAXP][MAXN];
int N, K, Q;
pair<int, int> advance(int L, int R, int p) {
int nL, nR;
if (L != -1)
nL = goleft[p][L];
else
nL = -1;
if (R != N)
nL = min(nL, goleft[p][R]);
if (R != N)
nR = goright[p][R];
else
nR = N;
if (L != -1)
nR = max(nR, goright[p][L]);
return pair(nL, nR);
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> K >> Q;
vector<int> level(N);
for (int &x : level)
cin >> x;
for (int i = 0; i < N; ++i) {
int j = i - 1;
while (j >= 0 and level[j] < level[i])
j = goleft[0][j];
goleft[0][i] = j;
}
for (int i = N - 1; i >= 0; --i) {
int j = i + 1;
while (j < N and level[j] < level[i])
j = goright[0][j];
goright[0][i] = j;
}
for (int p = 0; p < MAXP - 1; ++p)
for (int i = 0; i < N; ++i) {
int L = goleft[p][i];
int R = goright[p][i];
if (L != -1)
goleft[p + 1][i] = goleft[p][L];
else
goleft[p + 1][i] = -1;
if (R != N)
goleft[p + 1][i] = min(goleft[p + 1][i], goleft[p][R]);
if (R != N)
goright[p + 1][i] = goright[p][R];
else
goright[p + 1][i] = N;
if (L != -1)
goright[p + 1][i] = max(goright[p + 1][i], goright[p][L]);
}
while (Q--) {
int A, B;
cin >> A >> B;
--A, --B;
if (A > B)
swap(A, B);
int sol = 0;
int LA = A, RA = A;
/*while (advance(LA, RA, 0).second <= B) {
sol++;
tie(LA, RA) = advance(LA, RA, 0);
}
int LB = B, RB = B;
while (advance(LB, RB, 0).first >= RA) {
sol++;
tie(LB, RB) = advance(LB, RB, 0);
}*/
for (int p = MAXP - 1; p >= 0; --p)
if (advance(LA, RA, p).second <= B) {
sol += 1 << p;
tie(LA, RA) = advance(LA, RA, p);
}
int LB = B, RB = B;
for (int p = MAXP - 1; p >= 0; --p)
if (advance(LB, RB, p).first >= RA) {
sol += 1 << p;
tie(LB, RB) = advance(LB, RB, p);
}
if (RA < LB)
sol++;
cout << sol - 1 << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |