// ===== smawk.hpp ===== #ifndef SMAWK_HPP #define SMAWK_HPP #include #include #include template std::vector smawk_inner(const F &f, const std::vector &rows, const std::vector &cols) { if (rows.empty()) { return std::vector(0); } std::vector red_cols; red_cols.reserve(rows.size()); for (std::size_t c : cols) { while (!red_cols.empty() && f(rows[red_cols.size() - 1], red_cols.back()) >= f(rows[red_cols.size() - 1], c)) { red_cols.pop_back(); } if (red_cols.size() < rows.size()) { red_cols.push_back(c); } } std::vector red_rows; red_rows.reserve(rows.size() / 2); for (std::size_t i = 1; i < rows.size(); i += 2) { red_rows.push_back(rows[i]); } std::vector sub = smawk_inner(f, red_rows, red_cols); std::vector ret(rows.size()); for (std::size_t i = 0; i < sub.size(); ++i) { ret[2 * i + 1] = sub[i]; } std::size_t it = 0; for (std::size_t i = 0; i < rows.size(); i += 2) { ret[i] = red_cols[it]; std::size_t fin = (i + 1 == rows.size() ? red_cols.back() : ret[i + 1]); while (red_cols[it] != fin) { ++it; if (f(rows[i], ret[i]) > f(rows[i], red_cols[it])) { ret[i] = red_cols[it]; } } } return ret; } template std::vector smawk(const F &f, std::size_t h, std::size_t w) { std::vector rows(h); std::iota(rows.begin(), rows.end(), 0); std::vector cols(w); std::iota(cols.begin(), cols.end(), 0); return smawk_inner(f, rows, cols); } template std::vector smawk_maxima(const F &f, std::size_t h, std::size_t w) { const auto g = [&](std::size_t i, std::size_t j) { return -f(h - 1 - i, j); }; std::vector ret = smawk(g, h, w); std::reverse(ret.begin(), ret.end()); return ret; /*using V = decltype(f(h, w)); std::vector ret(h, 0); for (std::size_t i = 0; i < h; ++i) { V cur = f(i, 0ul); for (std::size_t j = 1; j < w; ++j) { V tmp = f(i, j); if (tmp > cur) { cur = tmp; ret[i] = j; } } } return ret;*/ } #endif // ===== smawk.hpp ===== #include #include #include #include using usize = std::size_t; std::vector> calc_inv(std::vector p) { usize n = p.size(); std::vector> sum(n + 1, std::vector(n + 1, 0)); for (usize i = 0; i < n; ++i) { sum[i + 1] = sum[i]; for (usize j = p[i]; j < n; ++j) { ++sum[i + 1][j + 1]; } } std::vector> inv(n, std::vector(n + 1, 0)); for (usize i = 0; i < n; ++i) { usize s = 0; for (usize j = i; j < n; ++j) { s += sum[j][n] - sum[i][n] - sum[j][p[j]] + sum[i][p[j]]; inv[i][j + 1] = s; } } return inv; } void fast_dp( const std::vector> &f, const std::vector &from, std::vector &to, usize l, usize r ) { if (l + 1 == r) { return; } usize mid = (l + r) / 2; fast_dp(f, from, to, l, mid); const auto jump = [&](usize i, usize j) -> long { i += mid; j += l; return from[j] + f[j][i]; }; std::vector max_index = smawk_maxima(jump, r - mid, mid - l); for (usize i = 0; i < r - mid; ++i) { usize tmp = jump(i, max_index[i]); if (tmp > to[i + mid]) { to[i + mid] = tmp; } } fast_dp(f, from, to, mid, r); } std::vector> calc_dp(std::vector> inv) { usize n = inv.size(); std::vector> dp(n + 1, std::vector(n + 1, 0)); for (usize i = 0; i < n; ++i) { fast_dp(inv, dp[i], dp[i + 1], i, n + 1); } return dp; } int main() { usize n; std::cin >> n; std::vector p(n); for (std::size_t &ele : p) { std::cin >> ele; --ele; } std::vector> inv = calc_inv(std::move(p)); usize all_inv = inv[0][n]; std::vector> dp = calc_dp(std::move(inv)); for (usize i = 1; i <= n; ++i) { std::cout << all_inv - dp[i][n] << '\n'; } }