#include <iostream>
#include <vector>
#include <atcoder/segtree>
using namespace std;
constexpr int INF = 100000000;
// Prepare a segment tree for segment max
using S = int;
int op(int a, int b) { return max(a, b); }
int e() { return -INF; }
using segtree = atcoder::segtree<S, op, e>;
void calc(const int N, const vector<int> &P, vector<int> &D) {
// chmin(D[i], P[i] + i - max_{j < i && P[j] < P[i]} {P[j] + j}
segtree segment_tree(N);
for (int i = 0; i < N; ++i) {
D[i] = min(D[i], P[i] + i - segment_tree.prod(0, P[i]));
segment_tree.set(P[i] - 1, P[i] + i);
}
}
int main() {
int N;
cin >> N;
vector<int> P(N);
for (auto &&p : P) cin >> p;
vector<int> D(N, INF);
// Handle j such that j < i and P[j] < P[i]
calc(N, P, D);
// j < i and P[j] > P[i]
for (auto &&p : P) p = N + 1 - p;
calc(N, P, D);
// j > i and P[j] > P[i]
reverse(begin(P), end(P));
reverse(begin(D), end(D));
calc(N, P, D);
// j > i and P[j] < P[i]
for (auto &&p : P) p = N + 1 - p;
calc(N, P, D);
// reverse again and print
reverse(begin(P), end(P));
reverse(begin(D), end(D));
for (int i = 0; i < N; ++i) cout << D[i] << " ";
cout << endl;
return 0;
}
就比如上面这篇,这算是篇伪代码吧?还是我打开的方式不对?