\(Part\ 1:\)

我们发现每次修改动的是\(a\)串,所以对于这个答案的公式,\(b_{i+j}\)的部分是可以求出来的。所以我们可以把公式改成如下所示:

\(f(j)=|\sum_{i=1}^{n}\ (-1)^i*b_{i+j}+\sum_{i=1}^{n}\ (-1)^{i-1}*a_{i}|\),\(0\le j\le m-n\)。

第一部分可以预处理。

在预处理的过程中我们不管绝对值,也就是会出现负数,这一点很重要的。

但是呢,我们发现对于不同的\(j\),每一个\(i+j\)的奇偶性是不一样的,所以我们不能心安理得的算出来就完事了。

我们不妨设第一个\(j\)是偶数,我们用一个前缀和数组\(sum\),其中\(sum_i\)表示当\(j\)为偶数的时候\(b\)的前缀和。

计算方法\(:\)

for(int i=1;i<=m;i++) if(i&1) b[i]=-b[i];
for(int i=1;i<=m;i++) sum[i]=sum[i-1]+b[i];

然后我们计算第一轮的答案,也就是我们先把不经过更改的\(a\)数组的答案计算出来,然后得出第一轮的答案,先把第一轮整个\(ans\)数组求出来。

之后呢,因为题目问的是最小值,所以我们要找出距离\(0\)最近的\(f_j\),输出这个最小值即可。

\(Part\ 2:\)

前面我们说过了如何处理第一轮的答案。

接下来我们看如何应付修改。

最开始是想用线段树维护一下,毕竟区间修改嘛。但是后来发现维护绝对值的时候特别麻烦,会影响到正确性,所以我们重新考虑。

我们发现,这个每次对\(a\)数组的修改是全局性的,也就是说,如果当前这个\(a_i\)匹配到的是\(-1\)的奇数次方,那么就是\(-a_i\),每次加上\(d\)的话对答案的更新贡献就变成了\(-d\)。同理,如果匹配到的是\(-1\)的偶数次方,那么对答案的更新贡献就是\(+d\)。然后我们发现这个事情特别爽,因为相邻两个位是可以相互抵消的。但是呢,我们这里特别需要处理的就是\(l,r\)奇偶性相同的问题。

\(i.\)如果\(l\%2==r\%2==0\),那么两边的数字都是偶数,我们最后一定会多出来一个孤零零的\(-d\)。

\(ii.\)如果\(l\%2==r\%2==1\),那么两边的数字都是奇数,最后一定会多出来一个孤零零的\(+d\)。

然后我们再记录一个偏移量\(shift\),表示每次更新答案的情况,例如某次\(update\)多出来一个\(-d\),我们的\(shift-=d\)。这个\(shift\)记录的相当于一个前缀和,所以每次都把答案加上\(shift\)。

\(Code:\)

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,q,ans;
int a[N],b[N],sum[N],f[N];
int read()
{
int x=0,f=1;
char c=getchar();
while (c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while (c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
int higher(int x)
{
int l=0,r=m-n,mid,cnt=0x3f3f3f3f3f3f3f3f;
while (l<=r)
{
mid=(l+r)>>1;
if(x<f[mid]+ans) cnt=mid,r=mid-1;
else l=mid+1;
}
if(cnt==0x3f3f3f3f3f3f3f3f) return cnt;
return f[cnt];
}
int lower(int x)
{
int l=0,r=m-n,mid,cnt=0x3f3f3f3f3f3f3f3f;
while (l<=r)
{
mid=(l+r)>>1;
if(x>=f[mid]+ans) cnt=mid,l=mid+1;
else r=mid-1;
}
if(cnt==0x3f3f3f3f3f3f3f3f) return cnt;
return f[cnt];
}
signed main()
{
n=read(),m=read(),q=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=m;i++) b[i]=read();
for(int i=1;i<=m;i++) if(i&1) b[i]=-b[i];
for(int i=1;i<=m;i++) sum[i]=sum[i-1]+b[i];
for(int i=1;i<=n;i++) {if(i%2) ans+=a[i];else ans-=a[i];}
for(int i=0;i<=m-n;i++)
{
if(!(i%2)) f[i]=ans+sum[n+i]-sum[i];
else f[i]=ans+sum[i]-sum[n+i];
}
sort(f,f+m-n+1);
ans=0;
cout<<min(abs(higher(0)),abs(lower(0)))<<endl;
while (q--)
{
int l=read(),r=read(),d=read();
if((l&1) && (r&1)) ans+=d;
if(!(l&1) && !(r&1)) ans-=d;
cout<<min(abs(higher(0)+ans),abs(lower(0)+ans))<<endl;
}
return 0;
}

CodeForces CF862E题解的更多相关文章

  1. codeforces#536题解

    CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and ...

  2. codeforces 1093 题解

    12.18 update:补充了 $ F $ 题的题解 A 题: 题目保证一定有解,就可以考虑用 $ 2 $ 和 $ 3 $ 来凑出这个数 $ n $ 如果 $ n $ 是偶数,我们用 $ n / 2 ...

  3. Codeforces Numbers 题解

    这题只需要会10转P进制就行了. PS:答案需要约分,可以直接用c++自带函数__gcd(x,y). 洛谷网址 Codeforces网址 Code(C++): #include<bits/std ...

  4. Codeforces 691E题解 DP+矩阵快速幂

    题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...

  5. Codeforces 833B 题解(DP+线段树)

    题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...

  6. Codeforces 840C 题解(DP+组合数学)

    题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...

  7. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

  8. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

  9. CodeForces CF875C题解

    题解 非常有意思的\(2-SAT\)的题. 听学长讲完之后感觉确实容易想到\(2-SAT\),顺理成章. 显然,对于两个串,对咱们来说有意义的显然是两个串中第一个不同的数字.那么,我们假设两个串分别是 ...

随机推荐

  1. VSCode & disable telemetry reporting

    VSCode & disable telemetry reporting https://code.visualstudio.com/docs/supporting/faq#_how-to-d ...

  2. Typescript & React & optional parameters & default parameters

    Typescript & React & optional parameters & default parameters Typescript & optional ...

  3. scrimba & interactive free online tutorials

    scrimba & interactive free online tutorials https://github.com/scrimba/community/blob/master/FAQ ...

  4. Nodejs 使用 TypeScript

    安装依赖 λ yarn add typescript types/node concurrently nodemon wait-on -D 初始化一个 tsconfig.json λ ./node_m ...

  5. Flutter: debounce 避免高频率事件

    原文 函数 import 'dart:async'; Function debounce(Function fn, [int t = 30]) { Timer _debounce; return () ...

  6. Puppeteer: 虚拟键盘

    文档 main.js const pptr = require('puppeteer'); const gotoUrl = 'http://127.0.0.1:5500/index.html'; (a ...

  7. sql if else 用法

    语法: case when 条件1 then 结果1 when 条件2 then 结果2 else 结果N end 可以有任意多个条件,如果没有默认的结果,最后的else也可以不写, select c ...

  8. SpringBoot 整合 Shiro 密码登录与邮件验证码登录(多 Realm 认证)

    导入依赖(pom.xml)  <!--整合Shiro安全框架--> <dependency> <groupId>org.apache.shiro</group ...

  9. Linux解压缩相关命令

    Linux解压缩相关命令 运行级别: 0:关机 1:单用户 2:多用户无网络连接 3:多用户有网络连接 4:系统保留 5:图形界面 6:系统重启 通过init[0123456]来切换不同的运行级别 g ...

  10. Vue中Jsx的使用

    什么是JSX? JSX就是Javascript和XML结合的一种格式.React发明了JSX,利用HTML语法来创建虚拟DOM.当遇到<,JSX就当HTML解析,遇到{就当JavaScript解 ...