原题题面
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array aaa of length nnn and array bbb of length mmm. He introduced a function f(j)f(j)f(j) which is defined for integers jjj, which satisfy 0 ≤j ≤ m − n0 \leq j \leq m - n0 ≤j ≤ m − n. Suppose, ci = ai − bi + jc_i = a_i - b_{i + j}ci = ai − bi + j. Then f(j) = ∣c1 − c2 + c3 − c4...cn∣f(j) = |c_1 - c_2 + c_3 - c_4... c_n|f(j) = ∣c1 − c2 + c3 − c4...cn∣. More formally,f(j)=∣∑i=1n(−1)i−1∗(ai−bi+j)∣f(j)=|\sum_{i=1}^{n}{(-1)^{i-1}*(a_i-b_{i+j})}|f(j)=∣∑i=1n(−1)i−1∗(ai−bi+j)∣.
Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid jjj. They found it a bit easy, so Dr. Evil made their task harder. He will give them qqq update queries. During each update they should add an integer xix_ixi to all elements in a in range [li;ri][l_i;r_i][li;ri] i.e. they should add xix_ixi to ali, ali + 1, ..., aria_{l_i}, a_{l_{i + 1}}, ... , a_{r_i}ali, ali + 1, ..., ari and then they should calculate the minimum value of f(j)f(j)f(j) for all valid jjj.
Please help Mahmoud and Ehab.
输入格式
The first line contains three integers nnn, mmm and qqq (1 ≤ n ≤ m ≤ 105,1 ≤ q ≤ 105)(1 \leq n \leq m \leq 10^5, 1 \leq q \leq 10^5)(1 ≤ n ≤ m ≤ 105,1 ≤ q ≤ 105) — number of elements in aaa, number of elements in bbb and number of queries, respectively.
The second line contains nnn integers a1, a2, ..., an.( −109 ≤ ai ≤ 109)a_1, a_2, ..., a_n. ( -10^9 \leq a_i \leq 10^9)a1, a2, ..., an.( −109 ≤ ai ≤ 109) — elements of aaa.
The third line contains mmm integers b1, b2, ..., bm.( −109 ≤ bi ≤ 109)b_1, b_2, ..., b_m. ( -10^9 \leq b_i \leq 10^9)b1, b2, ..., bm.( −109 ≤ bi ≤ 109) — elements of bbb.
Then qqq lines follow describing the queries. Each of them contains three integers li,ri,xi(1 ≤ li ≤ ri ≤ n, −109 ≤ x ≤ 109)l_i,r_i,x_i (1 \leq l_i \leq r_i \leq n, -10^9 \leq x \leq 10^9)li,ri,xi(1 ≤ li ≤ ri ≤ n, −109 ≤ x ≤ 109) — range to be updated and added value.
输出格式
The first line should contain the minimum value of the function fff before any update.
Then output qqq lines, the i−thi-thi−th of them should contain the minimum value of the function fff after performing the i−thi-thi−th update .
输入样例
5 6 3
1 2 3 4 5
1 2 3 4 5 6
1 1 10
1 1 -9
1 5 -1
输出样例
0
9
0
0
题面分析
∣∑i=1n(−1)i−1∗(ai−bi+j)∣|\sum_{i=1}^{n}{(-1)^{i-1}*(a_i-b_{i+j})}|∣∑i=1n(−1)i−1∗(ai−bi+j)∣
=∣∑i=1n(−1)i−1∗ai−(−1)i−1∗bi+j∣|\sum_{i=1}^{n}{(-1)^{i-1}*a_i-(-1)^{i-1}*b_{i+j}}|∣∑i=1n(−1)i−1∗ai−(−1)i−1∗bi+j∣
=∣∑i=1n(−1)i−1∗ai−∑i=1n(−1)i−1∗bi+j∣|\sum_{i=1}^{n}{(-1)^{i-1}*a_i}-\sum_{i=1}^{n}{(-1)^{i-1}*b_{i+j}}|∣∑i=1n(−1)i−1∗ai−∑i=1n(−1)i−1∗bi+j∣
我们记prefixA=∑i=1n(−1)i−1∗aiprefixA=\sum_{i=1}^{n}{(-1)^{i-1}*a_i}prefixA=∑i=1n(−1)i−1∗ai,
sumB=∑i=1n(−1)i−1∗bisumB=\sum_{i=1}^{n}{(-1)^{i-1}*b_i}sumB=∑i=1n(−1)i−1∗bi,
考虑到∑i=1n(−1)i−1∗bi+j\sum_{i=1}^{n}{(-1)^{i-1}*b_{i+j}}∑i=1n(−1)i−1∗bi+j的首项符号会有正负,因此我们再定义
prefixB[i]=(−1)i−1sum[i+n−1]−sum[i−1]prefixB[i]=(-1)^{i-1} sum[i+n-1]-sum[i-1]prefixB[i]=(−1)i−1sum[i+n−1]−sum[i−1]
那问题就转化为:
求∣prefixA−prefixB∣|prefixA-prefixB|∣prefixA−prefixB∣的最小值。
我们算出所有的prefixBprefixBprefixB进行排序,二分查找−prefixA-prefixA−prefixA即可。
最后计算时要考虑相邻两个数字的值,即prefixA−prefix[pos−1/pos/pos+1]prefixA-prefix[pos-1/pos/pos+1]prefixA−prefix[pos−1/pos/pos+1]三者的最小值。
AC代码(171ms)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=5e5;
ll prefixA=0;
ll prefixB[maxn+10];
ll sumB[maxn+10];
ll b[maxn+10];
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for(int i=1; i<=n; i++) {
ll x;
scanf("%lld", &x);
if (i&1) {
prefixA+=x;
}
else
prefixA-=x;
}
sumB[0]=0;
for(int i=1; i<=m; i++) {
scanf("%lld", &b[i]);
if (i&1) {
sumB[i]=sumB[i-1]+b[i];
}
else {
sumB[i]=sumB[i-1]-b[i];
}
}
for(int i=1; i<=m-n+1; i++) {
if (!(i&1)) {
prefixB[i-1]=sumB[i-1+n]-sumB[i-1];//[i,i+n-1]
}
else {
prefixB[i-1]=sumB[i-1]-sumB[i-1+n];//-[i,i+n-1]
}
}
sort(prefixB, prefixB+m-n+1);
int pos=lower_bound(prefixB, prefixB+m-n+1, -prefixA)-prefixB;
pos=min(pos, m-n);
printf("%lld\n", min(
min(abs(prefixA+prefixB[max(pos-1, 0)]),
abs(prefixA+prefixB[pos])),
abs(prefixA+prefixB[min(pos+1, m-n)])));
while(q--) {
ll l, r, x;
scanf("%lld%lld%lld", &l, &r, &x);
if ((r-l+1)&1) {
if (l&1) {
prefixA+=x;
}
else {
prefixA-=x;
}
}
pos=lower_bound(prefixB, prefixB+m-n+1, -prefixA)-prefixB;
pos=min(pos, m-n);
printf("%lld\n", min(
min(abs(prefixA+prefixB[max(pos-1, 0)]),
abs(prefixA+prefixB[pos])),
abs(prefixA+prefixB[min(pos+1, m-n)])));
}
}
后记
思路五分钟,调参两小时,好耶。
DrGilbert 2020.12.10