Desant 2
Description
给定序列
Limitations
Solution
视
考虑建一个图
显然
类似猫树分治,每次拿长边的中线把
但是这样漏掉了斜边,显然只有在横切时会影响,考虑在第一次横切时枚举最上面的行(只有它们可以用斜边),同样做 DP 求答案即可,不影响复杂度。
分治复杂度
::::info[code]
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;
template<class T>
bool chmax(T &a, const T &b){
if(a < b){ a = b; return true; }
return false;
}
template<class T>
bool chmin(T &a, const T &b){
if(a > b){ a = b; return true; }
return false;
}
namespace Fastio {}
using Fastio::qin;
using Fastio::qout;
constexpr i64 inf = 1e18;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m, q;
qin >> n >> m >> q;
vector<i64> pre(n + 1);
for (int i = 0, x; i < n; i++) {
qin >> x;
pre[i + 1] = pre[i] + x;
}
vector<i64> a(n + 1);
for (int i = 0; i <= n - m; i++) a[i] = pre[i + m] - pre[i];
vector<int> L(q), R(q), rec;
for (int i = 0; i < q; i++) {
qin >> L[i] >> R[i];
L[i]--;
rec.push_back(i);
}
const int blocks = n / m + 1, S = blocks * m;
auto getcod = [&](int x, int y) { return x * m + y; };
auto getp = [&](int cod) { return make_pair(cod / m, cod % m); };
vector<i64> ans(q), f(S), g(S);
auto divide = [&](auto&& self, int xl, int xr, int yl, int yr, int tag, const vector<int>& rec) -> void {
if (!rec.size() || xl > xr || yl > yr) return;
if (xl == xr && yl == yr) {
if (m == 1) for (auto i : rec)
chmax(ans[i], a[getcod(xl, yl)]);
return;
}
vector<int> recl, recr, recn;
if (xr - xl >= yr - yl) {
const int mid = (xl + xr) / 2;
for (auto i : rec) {
if (getp(L[i]).first <= mid && mid <= getp(R[i]).first)
recn.push_back(i);
else if (min(getp(L[i]).first, getp(R[i]).first) < mid)
recl.push_back(i);
else
recr.push_back(i);
}
for (int p = yl; p <= yr; p++) {
int codn = getcod(mid, p);
for (int i = xl; i <= xr; i++) {
for (int j = yl; j <= yr; j++)
f[getcod(i, j)] = g[getcod(i, j)] = -inf;
}
f[codn] = g[codn] = 0;
for (int i = xl; i <= xr; i++) {
for (int j = yl; j <= yr; j++) {
int cod = getcod(i, j);
if (cod + 1 < S) chmax(f[cod + 1], f[cod]);
if (cod + m < S) chmax(f[cod + m], f[cod] + a[cod]);
}
}
for (int i = xr; i >= xl; i--) {
for (int j = yr; j >= yl; j--) {
int cod = getcod(i, j);
if (cod >= 1) chmax(g[cod - 1], g[cod]);
if (cod >= m) chmax(g[cod - m], g[cod] + a[cod - m]);
}
}
for (auto i : recn) chmax(ans[i], f[R[i]] + g[L[i]]);
}
self(self, xl, mid - 1, yl, yr, tag, recl);
self(self, mid + 1, xr, yl, yr, tag, recr);
}
else {
const int mid = (yl + yr) / 2;
for (auto i : rec) {
if ((getp(L[i]).second <= mid && mid <= getp(R[i]).second) || (getp(R[i]).second <= mid && mid <= getp(L[i]).second))
recn.push_back(i);
else if (min(getp(L[i]).second, getp(R[i]).second) < mid)
recl.push_back(i);
else
recr.push_back(i);
}
for (int p = xl; p <= xr; p++) {
int codn = getcod(p, mid);
for (int i = xl; i <= xr; i++) {
for (int j = yl; j <= yr; j++)
f[getcod(i, j)] = g[getcod(i, j)] = -inf;
}
f[codn] = g[codn] = 0;
for (int i = xl; i <= xr; i++) {
for (int j = yl; j <= yr; j++) {
int cod = getcod(i, j);
if (cod + 1 < S) chmax(f[cod + 1], f[cod]);
if (cod + m < S) chmax(f[cod + m], f[cod] + a[cod]);
}
}
for (int i = xr; i >= xl; i--) {
for (int j = yr; j >= yl; j--) {
int cod = getcod(i, j);
if (cod >= 1) chmax(g[cod - 1], g[cod]);
if (cod >= m) chmax(g[cod - m], g[cod] + a[cod - m]);
}
}
for (auto i : recn) chmax(ans[i], f[R[i]] + g[L[i]]);
}
for (int p = xl; p <= xr && tag; p++) {
int codn = getcod(p, yr);
for (int i = xl; i <= xr; i++) {
for (int j = yl; j <= yr; j++)
f[getcod(i, j)] = g[getcod(i, j)] = -inf;
}
f[codn] = g[codn] = 0;
for (int i = xl; i <= xr; i++) {
for (int j = yl; j <= yr; j++) {
int cod = getcod(i, j);
if (cod + 1 < S) chmax(f[cod + 1], f[cod]);
if (cod + m < S) chmax(f[cod + m], f[cod] + a[cod]);
}
}
for (int i = xr; i >= xl; i--) {
for (int j = yr; j >= yl; j--) {
int cod = getcod(i, j);
if (cod >= 1) chmax(g[cod - 1], g[cod]);
if (cod >= m) chmax(g[cod - m], g[cod] + a[cod - m]);
}
}
for (auto i : rec) chmax(ans[i], f[R[i]] + g[L[i]]);
}
self(self, xl, xr, yl, mid - 1, 0, recl);
self(self, xl, xr, mid + 1, yr, 0, recr);
}
};
divide(divide, 0, blocks - 1, 0, m - 1, 1, rec);
for (int i = 0; i < q; i++) qout << ans[i] << '\n';
return 0;
}
::::