首先 \(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. cesium czml更新dataSource

    cesium czml更新dataSource:沙盒

  2. wpf 代码判断当前是否在设计模式,兼容没有UI线程的限制

    /// <summary> /// 当前是否处于设计模式 /// </summary> bool IsInDesignMode { get { return (bool)Des ...

  3. 倍增 & Tarjan 求解LCA

    什么是LCA? 假设我们有一棵树: 1 / \ 2 3 / \ / 4 5 6 对于 \(2\) 和 \(6\) 的LCA,就是最近公共祖先,即为距离 \(2\) 和 \(6\) 最近的两个节点公有的 ...

  4. 二、C语言基础知识

    声明 本文内容大多取自<高级语言程序设计一书>,为本人学习笔记记录,切勿用于商业用途. 第一节 C 语言发展和特点 C 语言是当今最流行的计算机语言之一,是一种结构化的高级语言. 一.C ...

  5. STM32F407——使用systick定时器裸机制作延时函数

    准备工作: 软件:keil5 硬件:STM32F407ZET6芯片,gec6818开发板,st-link调试器 文档:<开发板原理图>,<Cortex M3与M4权威指南>,& ...

  6. 基于Kubernetes可扩展的Selenium 并行自动化测试部署及搭建(1)——Win10环境下Docker部署

    前言: 最近逛swtestacademy时候发现一篇基于kubernetes搭建selenium并行自动化测试的帖子,主要描述的是通过k8s动态扩展selenium grid来实现自动化测试用例在不同 ...

  7. 使用C#构建一个同时问多个LLM并总结的小工具

    前言 在AI编程时代,如果自己能够知道一些可行的解决方案,那么描述清楚交给AI,可以有很大的帮助. 但是我们往往不知道真正可行的解决方案是什么? 我自己有过这样的经历,遇到一个需求,我不知道有哪些解决 ...

  8. web框架实例

    s5.py from wsgiref.simple_server import make_server def handle_index():     return [b'<h1>Hell ...

  9. wso2~对接外部认证系统keycloak

    在 WSO2 Identity Server 或 WSO2 API Manager 中,Identity Providers (身份提供者) 功能允许您将外部身份管理系统(如 Keycloak.Azu ...

  10. 代码随想录第三天 | 链表part01

    链表理论基础 建议:了解一下链表基础,以及链表和数组的区别 文章链接:https://programmercarl.com/链表理论基础.html 不是很了解链表和数组区别的可以先看看以上的文章. 2 ...