跳到正文

题解

雪的魔法 / qwq

pystraf11约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年5月20日
收录
2026年10月2日
更新
2026年11月13日
篇幅
约 9 分钟
获赞
1
题目
P8182

版本

  1. v1当前

Description

给定序列 a1∼ana_1\sim a_n 和常数 mm,回答 qq 次查询 l,rl,r,求:

  • 每次可以选择一个长度 ≤m\le m 的区间减 11,求将 al∼ara_l\sim a_r 都变成 00 的最少次数。

Limitations

1≤m≤n≤105,1≤q≤1051\le m\le n\le 10^5,1\le q\le 10^5

0≤ai≤109,1≤l≤r≤n0\le a_i\le 10^9,1\le l\le r\le n

1.2s,512MB1.2\text{s},512\text{MB}

Solution

视 n,qn,q 同阶,先消去所有 ai>ai+1a_i>a_{i+1} 的位置,那么右端为 ii 的操作要 ai−ai+1a_i-a_{i+1} 次,显然直接消 [i−m+1,i][i-m+1,i] 最优。然后 aia_i 不降,从后往前每 mm 个一块,那么块内若干次后缀消除即可,于是变为求块右端的 aia_i 之和,由于 aia_i 不降可以改写为:

qq 次查询 l,rl,r,求在 a[l,r]a[l,r] 中选择若干距离 ≥m\ge m 的位置,求它们和的最大可能值。

发现这和 Desant 2 非常像,把转移画成 m×nmm\times \frac{n}{m} 的类网格 DAG 最长路,直接套那题的网格图分治即可,可以参考 this,复杂度为 O(nn)O(n\sqrt n),空间 O(n)O(n)。

code
C++
#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> a(n), b(n);
	for (int i = 0; i < n; i++) qin >> a[i], b[i] = a[i];
	
	vector<i64> tag(n), sum(n);
	for (int i = 0; i < n - 1; i++) {
		if (a[i] > a[i + 1]) {
			tag[max(0, i - m + 1)] -= a[i] - a[i + 1];
			tag[i + 1] += a[i] - a[i + 1];
			sum[i] = a[i] - a[i + 1];
		}
	}
	
	for (int i = 0; i < n; i++) {
		if (i > 0) tag[i] += tag[i - 1], sum[i] += sum[i - 1];
		a[i] = max(0LL, a[i] + tag[i]);
	}
	
	vector<int> L(q), R(q), rec;
	for (int i = 0; i < q; i++) {
		qin >> L[i] >> R[i];
		L[i]--, R[i]--;
		rec.push_back(i);
	}
	
	const int blocks = (n + m - 1) / m, 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;
		}
		
		auto work = [&](int codn) {
			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]);
				}
			}
		};
		
		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++) {
				work(getcod(mid, p)); 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++) {
				work(getcod(p, mid)); for (auto i : recn) chmax(ans[i], f[R[i]] + g[L[i]]);
			}
			
			for (int p = xl; p <= xr && tag; p++) {
				work(getcod(p, yr)); 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++) {
		i64 tmp = b[R[i]] + (R[i] ? sum[R[i] - 1] : 0) - (L[i] ? sum[L[i] - 1] : 0);
		qout << tmp + ans[i] << '\n';
	}
	
	return 0;
}
原文