题解:洛谷 P7706「Wdsr-2.7」文文的摄影布置

题面+题目传送门

P7706 「Wdsr-2.7」文文的摄影布置

题目背景

作为幻想乡知名的记者射命丸文,文文常常需要为文文新闻采集相关的照片素材。

具体而言,文文会采集一大串的图片,用于为新的一期报纸提供图片。作为一份简短的快报,文文会从素材库中使用三张图片,第一张放在开头,第三张放在结尾,用于激发读者的阅读兴趣(毕竟,报纸的开头和结尾是最容易被看到的);第二张,则是为了帮助读者理解相关内容。

可是作为无双风神,文文收集的照片实在是太多了,以至于一时半会儿处理不过来。按照惯例,文文找到了在一旁吃瓜的你,希望你能帮她解决困难。

题目描述

尽管图片非常多,但幸运的是,文文已经将它们排成了一列,从左到右分别编号为 \(1 \sim n\),文文选取的三张图片,应该是一个长度为 \(\bf 3\) 的子序列。(不妨设选取的照片的序号为 \(i,j,k\) ,则必须要有 \(i<j<k\) )。

此外,文文给每张照片定了一个吸引度 \(A_i\) 与大小 \(B_i\) 。

因为报纸版面太大会降低读者的兴趣,于是选定两张照片 \(i,k\) 后,规定必须选择最小的 \(B_j\)。

形式化地说,规定 \(\psi(i,k) = A_i + A_k - \min(B_j)\),其中需要满足 \(i < j < k\)。

摸清了照片价值的计算,文文会告诉你共 \(m\) 个操作,可以分为以下三种:

  • \(\colorbox{f0f0f0}{\verb!1 x y!}\) :照片的吸引度发生变化。文文要将 \(A_x\) 修改为 \(y\) 。

  • \(\colorbox{f0f0f0}{\verb!2 x y!}\) :照片的大小发生变化。文文要将 \(B_x\) 修改为 \(y\) 。

  • \(\colorbox{f0f0f0}{\verb!3 l r!}\) :文文打算利用素材库的第 \(l\) 到第 \(r\) 张中的图片,你要告诉她 \(\psi(x,y)\) 的最大值( \(l\le x\le x+1<y \le r\) )。

输入格式

第一行两个整数 \(n,m\),分别表示照片数量和操作次数。

第二行 \(n\) 个整数,表示序列 \(A\),描述每张照片的吸引度。

第三行 \(n\) 个整数,表示序列 \(B\),描述每张照片的大小。

接下来 \(m\) 行,每行描述一个操作,格式如上所述。

输出格式

对于每个操作三,输出一行一个整数,表示答案。

输入输出样例 #1

输入 #1

6 6
1 4 2 3 5 6
5 3 4 1 6 7
3 2 5
3 1 6
1 2 3
3 1 6
2 6 1
3 1 6

输出 #1

8
9
8
8

说明/提示

数据范围及约定

\[\def\arraystretch{1.5} \begin{array}{|c|c|c|c|}\hline \textbf{Subtask} & \bm{n,m} & \textbf{特殊性质} & \textbf{分值}\cr\hline 1 & 1\le n,m\le 300 & \text{无} & 10\cr\hline 2 & 1\le n,m\le 5\times 10^3 & \text{无} & 20\cr\hline 3 & 1\le n,m\le 5\times 10^5 & \text{仅有操作 3} & 20\cr\hline 4 & 1\le n,m\le 10^5 & \text{无} & 20\cr\hline 5 & \text{无特殊限制} & \text{无} & 30\cr\hline \end{array} \]

  • 对于 \(100\%\) 的数据:

    \(1 \le n,m \le 5 \times 10^5\)。

    \(1 \leq A_i,B_i,y \leq 10^8\),\(1 \le x \le n\),\(1 \le l \le r \le n\)。

    保证 \(r-l+1 \geq 3\),即询问的区间长度大于等于 \(3\)


单点修改,区间查询,考虑线段树。

思考如何合并状态:

我们知道要的是 \(\text{aba}\),那么可以分割成 $$\text{\textbackslash + aba, a + ba, ab + a, aba + }$$
。

那么维护 \(\text{a, b, ab, ba, aba}\):

\[\text{a: a + \textbackslash,\textbackslash + a} \]

\[\text{b: b + \textbackslash,\textbackslash + b} \]

\[\text{ab: ab + \textbackslash,a+b,\textbackslash + ab} \]

\[\text{ba: ba + \textbackslash,b+a,\textbackslash + ba} \]

\[\text{aba: aba + \textbackslash, a+ba, ab+a, \textbackslash + aba} \]

。

时间复杂度 \(\Theta(n+q\log_2 n)\),空间复杂度 \(\Theta(n)\)。

代码
#include <iostream>
#include <algorithm>
using namespace std;
constexpr int N = 5e5 + 10, INF = 0x3f3f3f3f; pair<int, int> arr[N];
struct Node {
	int a, b, ab, ba, aba;
	Node operator + (const Node &R) const {
	    return {max(a, R.a), min(b, R.b),
	            max({a - R.b, ab, R.ab}), max({-b + R.a, ba, R.ba}),
                max({ab + R.a, a + R.ba, aba, R.aba})};
    }
} sgt[N << 2];
#define Ls (rt << 1)
#define Rs (rt << 1 | 1)
#define Mid (l + r >> 1)
void build(int rt, int l, int r) {
	l == r ? sgt[rt] = {arr[l].first, arr[l].second, -INF, -INF, -INF} :
	         (build(Ls, l, Mid), build(Rs, Mid + 1, r), sgt[rt] = sgt[Ls] + sgt[Rs]);
}
int op, x, y;
void modify(int rt, int l, int r) {
	l == r ? (op == 1 ? sgt[rt].a : sgt[rt].b) = y :
	         (x <= Mid ? modify(Ls, l, Mid) : modify(Rs, Mid + 1, r),
              sgt[rt] = sgt[Ls] + sgt[Rs], 0);
}
const Node query(int rt, int l, int r) {
	return x <= l && r <= y ? sgt[rt] :
	       x > Mid ? query(Rs, Mid + 1, r) : y <= Mid ? query(Ls, l, Mid) :
	       query(Ls, l, Mid) + query(Rs, Mid + 1, r);
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	int n, m; cin >> n >> m;
	for (register int i = 1; i <= n; ++i) cin >> arr[i].first;
	for (register int i = 1; i <= n; ++i) cin >> arr[i].second;
	build(1, 1, n);
	do
		cin >> op >> x >> y,
	    op == 3 ? cout << query(1, 1, n).aba << '\n', void() : modify(1, 1, n);
	while (--m); return 0;
}
posted @ 2025-12-16 20:57  linhanmo1  阅读(33)  评论(0)    收藏  举报