784 字
4 分钟
Replace-and-Sum

题面#

Today, KQ has an exam at the Grail Academy. A strict teacher gave a task that KQ could not solve. He was given two arrays aa and bb of length nn. KQ is allowed to perform the following operations on the arrays:

  • Choose an index ii (1i<n1\le i\lt n) and replace aia_i with ai+1a_{i+1}.
  • Choose an index ii (1in1\le i\le n) and replace aia_i with bib_i.

Now he has qq queries. Each query is described by two numbers ll and rr. His task is to find the maximum value of the sum (al+al+1+al+2++ar)(a_l + a_{l+1} + a_{l+2} + \dots + a_r) for each query, if he can perform any number of operations on any elements of the array. Since he is not skilled enough for this, he asks for your help.

Input

Each test consists of several test cases. The first line contains one integer tt (1t1041\le t\le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn, qq (1n,q2105)(1\le n, q\le 2 \cdot 10^5).

The second line of each test case contains nn integers a1,a2,...,ana_1, a_2,...,a_n (1ai104)(1\le a_i\le 10^4).

The third line of each test case contains nn integers b1,b2,...,bnb_1, b_2,...,b_n (1bi104)(1\le b_i\le 10^4).

The following qq lines contain two integers ll and rr (1lrn)(1\le l\le r\le n).

It is guaranteed that the sum of the values of nn and the sum of the values of qq across all test cases do not exceed 21052 \cdot 10^5.

Output

For each test case, output qq numbers separated by spaces — the maximum values of the sums (al+al+1+al+2++ar)(a_l + a_{l+1} + a_{l+2} + \dots + a_r).

思路#

理清题意,操作有两种类型:

  • 选择索引 ii,并赋值:ai=ai+1a_i = a_{i + 1}
  • 选择索引 ii,并赋值:ai=bia_i = b_i

而题目的要求是让KQ进行若干次上述操作,使得对于给出的查询 l,rl, r 能够使 al+al+1+al+2++ara_l + a_{l+1} + a_{l+2} + \dots + a_r 最大

可以注意到,对于一个固定位置 ii,它修改后的值来源于自己右边或者是 bb 中的同位置元素

因此我们可以定义答案数组 FF,并从右往左进行:

对于 FnF_n,它右边不可能存在数字,所以 Fn=max(an,bn)F_n = \max(a_n, b_n)

对于 Fi1inF_i 1 \leq i \le n,要考虑自己右边的情况,所以 Fi=max(ai,bi,Fi+1)F_i = \max(a_i, b_i, F_{i + 1})

所以最终我们所求的那个最大值就是

i=lrFi\sum_{i = l}^r F_i

为了降低时间复杂度,我们使用前缀和进行预处理,将查询的时间复杂度降低到 O(1)O(1)

NOTE

为什么要降低时间复杂度?

注意到本题的数据范围很大,而如果对每个查询都循环区间,时间复杂度就是 O(rl+1)O(r - l + 1)

总的复杂度会变成 O(nq)O(nq),有很大概率发生 TLE

所以我们需要一个时间复杂度更低的方法拿到结果

所以定义前缀和数组 PP,有如下:

P0=0,Pi=Pi1+FiP_0 = 0, P_i = P_{i - 1} + F_i

所以:

i=lrFi=PrPl1\sum_{i = l}^r F_i = P_r - P_{l - 1}

代码#

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t --) {
int n, q;
cin >> n >> q;
vector<int> a(n), b(n);
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < n; i ++) cin >> b[i];
vector<long long> F(n), P(n + 1, 0);
F[n - 1] = max(a[n - 1], b[n - 1]);
for (int i = n - 2; i >= 0; i --) {
F[i] = max((long long)max(a[i], b[i]), F[i + 1]);
}
for (int i = 0; i < n; i ++) {
P[i + 1] = P[i] + F[i];
}
for (int i = 0; i < q; i ++) {
int l, r;
cin >> l >> r;
l --, r --;
cout << P[r + 1] - P[l] << " \n"[i == q - 1];
}
}
}
Replace-and-Sum
https://github.com/posts/replace-and-sum/
作者
FZSGBall
发布于
2026-04-14
许可协议
CC BY-NC-SA 4.0