首先 \(f(j)=\displaystyle|\sum_{i=1}^{n}(-1)^{i-1}a_i+(-1)^ib_{i+j}|=|\sum_{i=1}^{n}(-1)^{i-1}a_i+\sum_{i=1}^{n}(-1)^{i}b_{i+j}|\)

我们先处理出 suma,表示 \(\displaystyle\sum_{i=1}^{n}(-1)^{i-1}a_i\) 的值。

然后处理出 sumb[i],这个是等于 \(-(sumb[i-1]+b_i)+b_{i+n}* n\) 是否为奇数,为奇数就乘 \(-1\),否则乘 \(1\)。

然后我们发现对于每次的更新,只有 \(r-l+1\) 为奇数时,且 \(l\) 也为奇数时,suma 才会发生变化,所以对于每次更新就判断一下就行了。

那么对于 \(f(j)\) 的最小值,我们二分一下,在 sumb 中找最接近 suma 的就行了。

代码:

#include <bits/stdc++.h>
#define reg register
#define ll long long
#define int long long
#define ull unsigned long long
#define db double
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vpi vector<pi>
#define vpl vector<pl>
#define pb push_back
#define er erase
#define SZ(x) (int) x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mkp make_pair
using namespace std;
char ch, B[1 << 20], *S = B, *T = B;
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 20, stdin), S == T) ? 0 : *S++)
#define isd(c) (c >= '0' && c <= '9')
int aa, bb;
int rd() {
while(ch = getc(), !isd(ch) && ch != '-');
ch == '-' ? aa = bb = 0 : (aa = ch - '0', bb = 1);
while(ch = getc(), isd(ch))
aa = aa * 10 + ch - '0';
return bb ? aa : -aa;
}
const int MAXN = 1e5 + 10;
int n, m, q, a[MAXN], b[MAXN], suma, sumb[MAXN];
int ask(int l, int r, int x, int delta) {
if((r - l) % 2 == 0) {
if(l % 2 == 1)
suma += x;
else
suma -= x;
}
int *pos = lb(sumb, sumb + delta, -suma);
int ans = 1e18 + 10;
if(pos != sumb + delta)
ans = min(ans, abs(suma + *pos));
if(pos != sumb) {
--pos;
ans = min(ans, abs(suma + *pos));
}
return ans;
}
inline void work() {
scanf("%lld%lld%lld", &n, &m, &q);
suma = 0;
for(reg int i = 1, mul = 1; i <= n; ++i, mul *= -1) {
scanf("%lld", &a[i]);
suma += a[i] * mul;
// printf("%d ", suma);
}
// puts("");
int delta = m - n + 1;
sumb[0] = 0;
for(reg int i = 1, mul = -1; i <= m; ++i, mul *= -1) {
scanf("%lld", &b[i]);
if(i <= n)
sumb[0] += b[i] * mul;
}
for(reg int i = 1; i + n <= m; ++i)
sumb[i] = -(sumb[i - 1] + b[i]) + b[i + n] * ((n & 1) ? -1 : 1);
// for(reg int i = 1; i <= m; ++i)
// printf("%d ", &b[i]);
// puts("");
sort(sumb, sumb + delta);
printf("%lld\n", ask(1, 1, 0, delta));
for(reg int i = 1; i <= q; ++i) {
int l, r, x;
scanf("%lld%lld%lld", &l, &r, &x);
printf("%lld\n", ask(l, r, x, delta));
}
}
signed main() {
// freopen("input.txt", "r", stdin);
work();
return 0;
}

CF862E Mahmoud and Ehab and the function的更多相关文章

  1. E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound #include &l ...

  2. codeforces E. Mahmoud and Ehab and the function(二分+思维)

    题目链接:http://codeforces.com/contest/862/problem/E 题解:水题显然利用前缀和考虑一下然后就是二分b的和与-ans_a最近的数(ans_a表示a的前缀和(奇 ...

  3. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

  4. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  5. E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...

  6. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  7. Codeforces 862C - Mahmoud and Ehab and the xor

    862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...

  8. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

  9. CF 959 E. Mahmoud and Ehab and the xor-MST

    E. Mahmoud and Ehab and the xor-MST https://codeforces.com/contest/959/problem/E 分析: 每个点x应该和x ^ lowb ...

  10. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

随机推荐

  1. fiddler的自动响应器

    1.点击autoresponder,勾选enable rules和unmatched requests passthrough 2.替换步骤 (1)把要替换的会话拉取到空白处,或者选中要替换的内容点击 ...

  2. MacOS v15.X安装HP旧款打印机驱动(P1606dn为例)

    一.下载官方驱动 先去官网下载一下HP提供的Mac下的驱动合集(图1),可惜的只支持15.0以下版本安装. https://support.hp.com/cn-zh/drivers/hp-laserj ...

  3. Windows查看端口占用、相应进程、杀死进程等[netstat]

    Windows 通过cmd或powerShell查看端口占用.相应进程.杀死进程等的命令 由于一般开发环境是在windows上,相应的一些测试必然涉及到一些端口的监听与使用.当开发使用的端口被占用后, ...

  4. Nim 语言新的性能测试

    今天将 性能测试网站: benchmarks game 上一个关于 n-body 的题目改成 nim 1.6.4 语言来编写. 注意,我是基于 java 的版本来写的,没有像  c++ 那样的版本使用 ...

  5. mysql免密登录

    开启mysql免密登录, vi /etc/my.cnf [mysqld]下添加 skip-grant-tables , 保存后重启mysql服务:service mysqld restart

  6. 四十种AI编程工具,让你码字如飞

    用过AI编程工具的都知道,这玩意儿虽说还有待完善,但是确实能提高效率啊,真香啊. 是这样的,最近一个同学离职,我临时接手了他的BI数据后台,我一个不会前端的人,使用AI编程工具,发送简单的指令,竟然使 ...

  7. Multisim14.0安装包免费获取,超详细中文安装步骤助你快速上手!

    Multisim14.0简介 Multisim14.0是由美国国家仪器公司(NI)推出的专业电子设计自动化](EDA)工具,广泛应用于电路设计.仿真验证.教学实验及科研开发领域.其核心功能是通过虚拟仿 ...

  8. 开源的java内网穿透 - 维基代理(wiki-proxy)

    1.简介 维基代理(wiki-proxy).开源的java内网穿透项目. 技术栈:cdkjFramework(维基框架).JPA.Netty 遵循MIT许可,因此您可以对它进行复制.修改.传播并用于任 ...

  9. Allure2+Maven+Testng部署及使用详细教程

    AllureReport部署 前言:最近做自动化测试要用到AllureReport生成测试报告,网上逛了一下,发现有很多帖子,但是大家描述的都模棱两可,没有详细的步骤,因此编写此贴对部署方式进行记录; ...

  10. 如何用IDEA 生成JavaDoc新版

    用IDEA生成JavaDoc 左上角四横杠点击,唤出工具栏(快捷键 alt+\) 左键单击工具选项 下拉框选 生成Javadoc 在弹出框选好参数立刻导出啦 1.JavaDoc 作用域: 选要导出的文 ...