跳到正文

题解

题解:P14311 【MX-S8-T4】平衡三元组

TobyHu约 29 分钟
原文

这是 2025年11月15日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月24日
篇幅
约 29 分钟

版本

  1. v2当前
  2. v1

数据结构好题,本题解仅介绍个人的分块做法。

前置芝士

分块,不会的可以先做 模板题。

10pts

对于每次询问,暴力枚举 xx、yy、zz,判断是否合法,合法的话更新答案,复杂度 O(qn3)O(qn^3)。

25pts

考虑枚举中间项 yy,发现 BxB_x 与 BzB_z 越大,越容易满足 By−Bx≤Bz−ByB_y - B_x \le B_z - B_y 的题设条件,且要求的值 Bx+By+BzB_x + B_y + B_z 越大,故可能更新答案的 BxB_x 与 BzB_z 即为区间 [l,r][l,r] 内 ByB_y 左右的最大值,可以用线段树或者分块来维护,单次修改复杂度为 O(log⁡n)O(\log{n}) 或 O(n)O(\sqrt{n}),单词查询复杂度为 O(nlog⁡n)O(n\log{n}) 或 O(nn)O(n\sqrt{n}),总复杂度 O(qnlog⁡n)O(qn\log{n}) 或 O(qnn)O(qn\sqrt{n})。

70pts~80pts

可以发现上述算法的瓶颈在于查询,因此需要优化查询,将部分复杂度分到修改上。

由于 qq 较小,可用思维量更小且更易实现的分块实现,将枚举 yy 的 O(n)O(n) 复杂度降为 O(n)O(\sqrt{n})。

具体实现方法:

  1. 每次修改后维护块内排序,每次查询时每块看做一个点,取块内最大值,预处理出前缀最大值和后缀最大值
  2. 对于散块查询,枚举每个点,直接利用前缀最大值和后缀最大值判断该点是否可作为合法的 yy 并更新答案
  3. 对于整块查询,枚举每个块,分三类讨论:
  • xx 在块左侧,zz 在块右侧:对维护的块内排序进行二分查找,找到块内最大的 ByB_y 使得 By×2≤Bx+BzB_y \times 2 \le B_x + B_z (其中 BxB_x 为前缀最大值,BzB_z 为后缀最大值),更新答案。
  • xx 和 yy 均在块内:每次修改后暴力维护块内答案,查询到时直接更新答案。
  • xx 和 yy 一个在块内,一个在块外:每次修改后分别维护 x,zx,z 在块内时不同 yy 所需的最小前/后缀最大值以及产生的贡献,分别进行排序,去重,使其具有单调性。查询时二分找到当前的前/后缀最大值能达到的最大需求,用其对应贡献与前/后缀最大值加和更新答案。

时间复杂度:O(qnlog⁡n)O(q\sqrt{n}\log{n})

代码实现

75pts代码
C++
#include <iostream>
#include <algorithm> 
#include <cstdio>
#include <cmath>
#define int long long
#define INF 1000000000000000000
using namespace std;

char *p1,*p2,in[100000];
#define gc() (p1 == p2 && (p2 = (p1 = in) + fread(in,1,100000,stdin),p1 == p2)?EOF:*p1++) 

inline int read()
{
	int x = 0,f = 1;
	char c = gc();
	while (c < 48 || c > 57)
	{
		if (c == '-') f = -1;
		c = gc();
	}
	while (c >= 48 && c <= 57) x = x * 10 + c - 48,c = gc();
	return x * f;
}

void write(int x)
{
	if (x < 0) putchar('-'),x = -x; 
	if (x / 10) write(x/10);
	putchar(x%10+48); 
}

struct node
{
	int x,y;
	bool operator ==(const node &z)const
	{
		return x == z.x && y == z.y;
	}
}ndl[1010][1010],ndr[1010][1010];

int n,q,k,m,op,x,y,z,ans,st[1010],ed[1010],add[1010],tpl[1010],tpr[1010],a[1000010],b[1000010],s[1000010],id[1000010],mx[1000010][2];

bool cmp(node x,node y)
{
	return x.x != y.x ? x.x < y.x : x.y > y.y;
}

void modify(int m)
{
	int i = st[m],j = ed[m];
	s[m] = -INF;
	for (int t = i;t <= j;t++) mx[t][0] = max(mx[t - 1][0],a[t]);
	for (int t = j;t >= i;t--) mx[t][1] = max(mx[t + 1][1],a[t]);
	for (int t = i + 1;t < j;t++) if (a[t] * 2 <= mx[t - 1][0] + mx[t + 1][1]) s[m] = max(s[m],mx[t - 1][0] + a[t] + mx[t + 1][1]);
	tpl[m] = tpr[m] = 0;
	for (int t = i;t < j;t++) ndl[m][++tpl[m]] = {a[t] * 2 - mx[t + 1][1],a[t] + mx[t + 1][1]};
	for (int t = i + 1;t <= j;t++) ndr[m][++tpr[m]] = {a[t] * 2 - mx[t - 1][0],a[t] + mx[t - 1][0]}; 
	sort(ndl[m] + 1,ndl[m] + tpl[m] + 1,cmp);
	for (int t = 2,lst = 1;t <= tpl[m];t++)
	{
		if (ndl[m][t].y <= ndl[m][lst].y) ndl[m][t] = ndl[m][t - 1];
		else lst = t;
	}
    tpl[m] = unique(ndl[m] + 1,ndl[m] + tpl[m] + 1) - ndl[m] - 1;
	sort(ndr[m] + 1,ndr[m] + tpr[m] + 1,cmp);
	for (int t = 2,lst = 1;t <= tpr[m];t++)
	{
		if (ndr[m][t].y <= ndr[m][lst].y) ndr[m][t] = ndr[m][t - 1];
		else lst = t;
	}
    tpr[m] = unique(ndr[m] + 1,ndr[m] + tpr[m] + 1) - ndr[m] - 1;
	for (int t = i;t <= j;t++) mx[t][0] = mx[t][1] = -INF;
	return;
}

signed main()
{
    n = read(),q = read();
	k = sqrt(n);
	for (int i = 0;i <= n + 1;i++) mx[i][0] = mx[i][1] = -INF;
	for (int i = 1,j = k;i <= n;i += k,j = min(j + k,n))
	{
		m++,st[m] = i,ed[m] = j;
		for (int t = i;t <= j;t++) b[t] = a[t] = read(),id[t] = m;
		sort(b + i,b + j + 1);
		ndl[m][0] = ndr[m][0] = {-INF,-INF};
		modify(m);
	}
	while (q--)
	{
        op = read(),x = read(),y = read();
		if (op == 1)
		{
			ans = -INF;
			if (id[x] == id[y])
			{
				for (int i = x;i <= y;i++) mx[i][0] = max(mx[i - 1][0],a[i] + add[id[x]]);
				for (int i = y;i >= x;i--) mx[i][1] = max(mx[i + 1][1],a[i] + add[id[x]]);
				for (int i = x + 1;i < y;i++) if ((a[i] + add[id[x]]) * 2 <= mx[i - 1][0] + mx[i + 1][1]) ans = max(ans,mx[i - 1][0] + a[i] + add[id[x]] + mx[i + 1][1]);
				if (ans <= -1e17) puts("No");
				else write(ans),putchar('\n');
				for (int i = x;i <= y;i++) mx[i][0] = mx[i][1] = -INF;
			}
			else
			{
				for (int i = x;i <= ed[id[x]];i++) mx[i][0] = max(mx[i - 1][0],a[i] + add[id[x]]);
				for (int i = id[x] + 1;i < id[y];i++) mx[st[i]][0] = mx[ed[i]][0] = max(mx[st[i] - 1][0],b[ed[i]] + add[i]);
				for (int i = st[id[y]];i <= y;i++) mx[i][0] = max(mx[i - 1][0],a[i] + add[id[y]]);
				for (int i = y;i >= st[id[y]];i--) mx[i][1] = max(mx[i + 1][1],a[i] + add[id[y]]);
				for (int i = id[y] - 1;i > id[x];i--) mx[st[i]][1] = mx[ed[i]][1] = max(mx[ed[i] + 1][1],b[ed[i]] + add[i]);
				for (int i = ed[id[x]];i >= x;i--) mx[i][1] = max(mx[i + 1][1],a[i] + add[id[x]]);
				for (int i = x + 1;i <= ed[id[x]];i++) if ((a[i] + add[id[x]]) * 2 <= mx[i - 1][0] + mx[i + 1][1]) ans = max(ans,mx[i - 1][0] + a[i] + add[id[x]] + mx[i + 1][1]);
				for (int i = id[x] + 1;i < id[y];i++)
				{
					ans = max(ans,s[i] + add[i] * 3);
					int l = 0,r = tpl[i],mid;
					while (l < r)
					{ 
						mid = (l + r + 1) >> 1;
						if (ndl[i][mid].x + add[i] <= mx[st[i] - 1][0]) l = mid;
						else r = mid - 1; 
					}
					ans = max(ans,ndl[i][l].y + mx[st[i] - 1][0] + add[i] * 2);
					l = 0,r = tpr[i];
					while (l < r)
					{
						mid = (l + r + 1) >> 1;
						if (ndr[i][mid].x + add[i] <= mx[ed[i] + 1][1]) l = mid;
						else r = mid - 1; 
					}
					ans = max(ans,ndr[i][l].y + mx[ed[i] + 1][1] + add[i] * 2);
					if ((b[st[i]] + add[i]) * 2 > mx[st[i] - 1][0] + mx[ed[i] + 1][1]) continue;
					l = st[i],r = ed[i];
					while (l < r)
					{
						mid = (l + r + 1) >> 1;
						if ((b[mid] + add[i]) * 2 <= mx[st[i] - 1][0] + mx[ed[i] + 1][1]) l = mid;
						else r = mid - 1;
					}
					ans = max(ans,mx[st[i] - 1][0] + b[l] + add[i] + mx[ed[i] + 1][1]);
				}
				for (int i = st[id[y]];i < y;i++) if ((a[i] + add[id[y]]) * 2 <= mx[i - 1][0] + mx[i + 1][1]) ans = max(ans,mx[i - 1][0] + a[i] + add[id[y]] + mx[i + 1][1]);
				if (ans <= -1e17) puts("No");
				else write(ans),putchar('\n');
				for (int i = x;i <= ed[id[x]];i++) mx[i][0] = mx[i][1] = -INF;
				for (int i = id[x] + 1;i < id[y];i++) mx[st[i]][0] = mx[ed[i]][0] = mx[st[i]][1] = mx[ed[i]][1] = -INF;
				for (int i = st[id[y]];i <= y;i++) mx[i][0] = mx[i][1] = -INF;
			}
		}
		else
		{
            z = read();
			if (id[x] == id[y])
			{
				for (int i = x;i <= y;i++) a[i] += z;
				for (int i = st[id[x]];i <= ed[id[x]];i++) b[i] = (a[i] += add[id[x]]);
				add[id[x]] = 0;
				sort(b + st[id[x]],b + ed[id[x]] + 1);
				modify(id[x]);
			}
			else
			{
				for (int i = x;i <= ed[id[x]];i++) a[i] += z;
				for (int i = st[id[x]];i <= ed[id[x]];i++) b[i] = (a[i] += add[id[x]]);
				add[id[x]] = 0;
				sort(b + st[id[x]],b + ed[id[x]] + 1);
				modify(id[x]);
				for (int i = id[x] + 1;i < id[y];i++) add[i] += z;
				for (int i = st[id[y]];i <= y;i++) a[i] += z;
				for (int i = st[id[y]];i <= ed[id[y]];i++) b[i] = (a[i] += add[id[y]]);
				add[id[y]] = 0;
				sort(b + st[id[y]],b + ed[id[y]] + 1);
				modify(id[y]);
			}
		}
	}
	return 0;
}

85~100pts

由于分块算法的巨大常数,上述做法虽然理论上可以通过本题,但会出现 TLE 的问题,我们需要对代码进行一些优化:

1.减少排序,优化排序

修改时对 bb 数组的排序可以优化到 O(n)O(n),使用类似于 这道题 的做法,由于修改过的元素与未修改的元素分别有序,可以分别提取出来进行一次归并操作,实现 O(n)O(n) 维护 bb 数组。

修改时对于 ndlndl,ndrndr 数组的排序,可以使用归并排序,或者更进一步的,使用基数排序,以降低复杂度。

2.利用你所知道的一切卡常技巧,疯狂卡常

例如:加快 IO、部分 long long 改 int、inline、register、乘法改位运算……

参考代码
C++
#include <iostream>
#include <algorithm> 
#include <cstdio>
#include <cmath>
#define int long long
#define re register
using namespace std;

char *p1,*p2,in[100000];
#define gc() (p1 == p2 && (p2 = (p1 = in) + fread(in,1,100000,stdin),p1 == p2)?EOF:*p1++) 

inline signed read()
{
	signed x = 0,f = 1;
	char c = gc();
	while (c < 48 || c > 57)
	{
		if (c == '-') f = -1;
		c = gc();
	}
	while (c >= 48 && c <= 57) x = x * 10 + c - 48,c = gc();
	return x * f;
}

void write(int x)
{
	if (x < 0) putchar('-'),x = -x; 
	if (x / 10) write(x/10);
	putchar(x%10+48); 
}

struct node
{
	int x,y;
	bool operator ==(const node &z)const
	{
		return x == z.x && y == z.y;
	}
	bool operator <(const node &z)const
	{
		return x != z.x ? x < z.x : y > z.y;
	}
}ndl[2010][2010],ndr[2010][2010];

signed n,q,k,m,op,x,y,z,len[2],st[2010],ed[2010],add[2010],tpl[2010],tpr[2010],hp[2010][2],a[1000010],b[1000010],id[1000010],mx[1000010][2];
int ans,s[1000010];

inline bool comp(signed x,signed y)
{
	return a[x] < a[y];
}

inline void modify(signed m)
{
	int i = st[m],j = ed[m];
	s[m] = -1000000000000;
	for (re signed t = i;t <= j;t++) mx[t][0] = max(mx[t - 1][0],a[t]);
	for (re signed t = j;t >= i;t--) mx[t][1] = max(mx[t + 1][1],a[t]);
	for (re signed t = i + 1;t < j;t++) if ((a[t] << 1) <= mx[t - 1][0] + mx[t + 1][1]) s[m] = max(s[m],mx[t - 1][0] + a[t] * 1ll + mx[t + 1][1]);
	tpl[m] = tpr[m] = 0;
	for (re signed t = i;t < j;t++) ndl[m][++tpl[m]] = {(a[t] << 1ll) - mx[t + 1][1],a[t] + mx[t + 1][1]};
	for (re signed t = i + 1;t <= j;t++) ndr[m][++tpr[m]] = {(a[t] << 1ll) - mx[t - 1][0],a[t] + mx[t - 1][0]};
	stable_sort(ndl[m] + 1,ndl[m] + tpl[m] + 1);
	for (re signed t = 2,lst = 1;t <= tpl[m];t++)
	{
		if (ndl[m][t].y <= ndl[m][lst].y) ndl[m][t] = ndl[m][t - 1];
		else lst = t;
	}
	stable_sort(ndr[m] + 1,ndr[m] + tpr[m] + 1);
	for (re signed t = 2,lst = 1;t <= tpr[m];t++)
	{
		if (ndr[m][t].y <= ndr[m][lst].y) ndr[m][t] = ndr[m][t - 1];
		else lst = t;
	}
	for (re signed t = i;t <= j;t++) mx[t][0] = mx[t][1] = -2000000000;
	return;
}

signed main()
{
    n = read(),q = read();
	k = sqrt(n);
	for (re signed i = 0;i <= n + 1;i++) mx[i][0] = mx[i][1] = -2000000000;
	for (re signed i = 1,j = k;i <= n;i += k,j = min(j + k,n))
	{
		m++,st[m] = i,ed[m] = j;
		for (int t = i;t <= j;t++) a[t] = read(),b[t] = t,id[t] = m;
		sort(b + i,b + j + 1,comp);
		ndl[m][0] = ndr[m][0] = {-1000000000000,-1000000000000};
		modify(m);
	}
	while (q--)
	{
        op = read(),x = read(),y = read();
		if (op == 1)
		{
			ans = -10000000000000;
			if (id[x] == id[y])
			{
				for (re signed i = x;i <= y;i++) mx[i][0] = max(mx[i - 1][0],a[i] + add[id[x]]);
				for (re signed i = y;i >= x;i--) mx[i][1] = max(mx[i + 1][1],a[i] + add[id[x]]);
				for (re signed i = x + 1;i < y;i++) if ((a[i] + add[id[x]] << 1ll) <= mx[i - 1][0] + mx[i + 1][1]) ans = max(ans,mx[i - 1][0] + a[i] * 1ll + add[id[x]] + mx[i + 1][1]);
				if (ans <= -10000000000) puts("No");
				else write(ans),putchar('\n');
				for (re signed i = x;i <= y;i++) mx[i][0] = mx[i][1] = -2000000000;
			}
			else
			{
				for (re signed i = x;i <= ed[id[x]];i++) mx[i][0] = max(mx[i - 1][0],a[i] + add[id[x]]);
				for (re signed i = id[x] + 1;i < id[y];i++) mx[st[i]][0] = mx[ed[i]][0] = max(mx[st[i] - 1][0],a[b[ed[i]]] + add[i]);
				for (re signed i = st[id[y]];i <= y;i++) mx[i][0] = max(mx[i - 1][0],a[i] + add[id[y]]);
				for (re signed i = y;i >= st[id[y]];i--) mx[i][1] = max(mx[i + 1][1],a[i] + add[id[y]]);
				for (re signed i = id[y] - 1;i > id[x];i--) mx[st[i]][1] = mx[ed[i]][1] = max(mx[ed[i] + 1][1],a[b[ed[i]]] + add[i]);
				for (re signed i = ed[id[x]];i >= x;i--) mx[i][1] = max(mx[i + 1][1],a[i] + add[id[x]]);
				for (re signed i = x + 1;i <= ed[id[x]];i++) if ((a[i] + add[id[x]] << 1ll) <= mx[i - 1][0] + mx[i + 1][1] * 1ll) ans = max(ans,mx[i - 1][0] + a[i] * 1ll + add[id[x]] + mx[i + 1][1]);
				for (re signed i = id[x] + 1;i < id[y];i++)
				{
					ans = max(ans,s[i] + (add[i] << 1ll) + add[i]);
					int l = 0,r = tpl[i],mid;
					while (l < r)
					{ 
						mid = (l + r + 1) >> 1;
						if (ndl[i][mid].x + add[i] <= mx[st[i] - 1][0]) l = mid;
						else r = mid - 1; 
					}
					ans = max(ans,ndl[i][l].y + mx[st[i] - 1][0] + (add[i] << 1ll));
					l = 0,r = tpr[i];
					while (l < r)
					{
						mid = (l + r + 1) >> 1;
						if (ndr[i][mid].x + add[i] <= mx[ed[i] + 1][1]) l = mid;
						else r = mid - 1; 
					}
					ans = max(ans,ndr[i][l].y + mx[ed[i] + 1][1] + (add[i] << 1ll));
					if ((a[b[st[i]]] + add[i] << 1ll) > mx[st[i] - 1][0] + mx[ed[i] + 1][1]) continue;
					l = st[i],r = ed[i];
					while (l < r)
					{
						mid = (l + r + 1) >> 1;
						if ((a[b[mid]] + add[i] << 1ll) <= mx[st[i] - 1][0] + mx[ed[i] + 1][1] * 1ll) l = mid;
						else r = mid - 1;
					}
					ans = max(ans,mx[st[i] - 1][0] + a[b[l]] * 1ll + add[i] + mx[ed[i] + 1][1]);
				}
				for (re signed i = st[id[y]];i < y;i++) if ((a[i] + add[id[y]] << 1ll) <= mx[i - 1][0] + mx[i + 1][1] * 1ll) ans = max(ans,mx[i - 1][0] + a[i] * 1ll + add[id[y]] + mx[i + 1][1]);
				if (ans <= -10000000000) puts("No");
				else write(ans),putchar('\n');
				for (re signed i = x;i <= ed[id[x]];i++) mx[i][0] = mx[i][1] = -2000000000;
				for (re signed i = id[x] + 1;i < id[y];i++) mx[st[i]][0] = mx[ed[i]][0] = mx[st[i]][1] = mx[ed[i]][1] = -2000000000;
				for (re signed i = st[id[y]];i <= y;i++) mx[i][0] = mx[i][1] = -2000000000;
			}
		}
		else
		{
            z = read();
			if (id[x] == id[y])
			{
				len[0] = len[1] = 0;
				for (re signed i = st[id[x]];i <= ed[id[x]];i++)
				{
					if (b[i] >= x && b[i] <= y) hp[++len[1]][1] = b[i],a[b[i]] += add[id[x]] + z;
					else hp[++len[0]][0] = b[i],a[b[i]] += add[id[x]];
				}
				add[id[x]] = 0;
			    for (re signed i = 1,j = 1,t = st[id[x]];t <= ed[id[x]];t++)
			    {
			        if (j > len[1] || (i <= len[0] && a[hp[i][0]] < a[hp[j][1]])) b[t] = hp[i++][0];
			        else b[t] = hp[j++][1];
			    }
				modify(id[x]);
			}
			else
			{
				len[0] = len[1] = 0;
				for (re signed i = st[id[x]];i <= ed[id[x]];i++)
				{
					if (b[i] >= x) hp[++len[1]][1] = b[i],a[b[i]] += add[id[x]] + z;
					else hp[++len[0]][0] = b[i],a[b[i]] += add[id[x]];
				}
				add[id[x]] = 0;
			    for (re signed i = 1,j = 1,t = st[id[x]];t <= ed[id[x]];t++)
			    {
			        if (j > len[1] || (i <= len[0] && a[hp[i][0]] < a[hp[j][1]])) b[t] = hp[i++][0];
			        else b[t] = hp[j++][1];
			    }
				modify(id[x]);
				for (re signed i = id[x] + 1;i < id[y];i++) add[i] += z;
				len[0] = len[1] = 0;
				for (re signed i = st[id[y]];i <= ed[id[y]];i++)
				{
					if (b[i] <= y) hp[++len[1]][1] = b[i],a[b[i]] += add[id[y]] + z;
					else hp[++len[0]][0] = b[i],a[b[i]] += add[id[y]];
				}
				add[id[y]] = 0;
			    for (re signed i = 1,j = 1,t = st[id[y]];t <= ed[id[y]];t++)
			    {
			        if (j > len[1] || (i <= len[0] && a[hp[i][0]] < a[hp[j][1]])) b[t] = hp[i++][0];
			        else b[t] = hp[j++][1];
			    }
				modify(id[y]);
			}
		}
	}
	return 0;
}
说明

上述代码仅供参考,只是一个优化示例,直接提交该代码将无法 AC 本题,读者可在该代码的基础上继续优化。 附:最终 AC 记录