codeforces 862E (二分)

本文介绍了一个涉及数组操作和数学函数的复杂算法问题。任务要求计算一个特定数学函数的最小值,并在数组更新后重新计算该值。通过预处理和排序技巧,文章提供了一种高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题题面

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 - n0jmn. Suppose, ci = ai − bi + jc_i = a_i - b_{i + j}ci=aibi+j. Then f(j) = ∣c1 − c2 + c3 − c4...cn∣f(j) = |c_1 - c_2 + c_3 - c_4... c_n|f(j)=c1c2+c3c4...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)i1(aibi+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)(1nm105,1q105) — 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.(109ai109) — 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.(109bi109) — 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(1lirin,109x109) — 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-thith of them should contain the minimum value of the function fff after performing the i−thi-thith 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)i1(aibi+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)i1ai(1)i1bi+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)i1aii=1n(1)i1bi+j
我们记prefixA=∑i=1n(−1)i−1∗aiprefixA=\sum_{i=1}^{n}{(-1)^{i-1}*a_i}prefixA=i=1n(1)i1ai,
sumB=∑i=1n(−1)i−1∗bisumB=\sum_{i=1}^{n}{(-1)^{i-1}*b_i}sumB=i=1n(1)i1bi,
考虑到∑i=1n(−1)i−1∗bi+j\sum_{i=1}^{n}{(-1)^{i-1}*b_{i+j}}i=1n(1)i1bi+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)i1sum[i+n1]sum[i1]
那问题就转化为:
∣prefixA−prefixB∣|prefixA-prefixB|prefixAprefixB的最小值。
我们算出所有的prefixBprefixBprefixB进行排序,二分查找−prefixA-prefixAprefixA即可。
最后计算时要考虑相邻两个数字的值,即prefixA−prefix[pos−1/pos/pos+1]prefixA-prefix[pos-1/pos/pos+1]prefixAprefix[pos1/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值