题目链接:http://codeforces.com/contest/862/problem/E

题解:水题显然利用前缀和考虑一下然后就是二分b的和与-ans_a最近的数(ans_a表示a的前缀和(奇加偶减))

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int M = 1e5 + ;
ll a[M] , b[M] , sum[M] , ans_b[M];
int main() {
int n , m , q;
scanf("%d%d%d" , &n , &m , &q);
for(int i = ; i <= n ; i++) scanf("%lld" , &a[i]);
for(int i = ; i <= m ; i++) scanf("%lld" , &b[i]);
sum[] = ;
for(int i = ; i <= m ; i++) {
if(i % ) {
sum[i] = sum[i - ] + b[i];
}
else {
sum[i] = sum[i - ] - b[i];
}
}
for(int i = ; i <= m - n + ; i++) {
if(!(i % )) {
ans_b[i - ] = sum[i + n - ] - sum[i - ];
}
else {
ans_b[i - ] = sum[i - ] - sum[i + n - ];
}
}
sort(ans_b , ans_b + m - n + );
ll ans_a = ;
for(int i = ; i <= n ; i++) {
if(i % ) {
ans_a += a[i];
}
else {
ans_a -= a[i];
}
}
int pos = ;
pos = lower_bound(ans_b , ans_b + m - n + , -ans_a) - ans_b;
pos = min(m - n , pos);
printf("%lld\n" , min(abs(ans_a + ans_b[max( , pos - )]) , min(abs(ans_a + ans_b[pos]) , abs(ans_a + ans_b[min(m - n , pos + )]))));
while(q--) {
int l , r;
ll x;
scanf("%d%d%lld" , &l , &r , &x);
if(l % ) {
if((r - l + ) % ) {
ans_a += x;
}
}
else {
if((r - l + ) % ) {
ans_a -= x;
}
}
pos = lower_bound(ans_b , ans_b + m - n + , -ans_a) - ans_b;
pos = min(m - n , pos);
printf("%lld\n" , min(abs(ans_a + ans_b[max( , pos - )]) , min(abs(ans_a + ans_b[pos]) , abs(ans_a + ans_b[min(m - n , pos + )]))));
}
return ;
}

codeforces E. Mahmoud and Ehab and the function(二分+思维)的更多相关文章

  1. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  2. 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 ...

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

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

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

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

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

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

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

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

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

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

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

    题目链接:http://codeforces.com/contest/862/submission/30696399 题解:这题一看操作数就知道是二分答案了.然后就是怎么个二分法,有两种思路第一种是找 ...

  9. [Codeforces]862F - Mahmoud and Ehab and the final stage

    题目大意:n个字符串,支持修改一个位置上的字符串和查询一个区间的子区间中长度乘LCP的最大值,输入字符数和询问数不超过10^5. 做法:求出相邻的LCP长度,区间LCP等于区间最小值,查询分几种情况考 ...

随机推荐

  1. Java内部类超详细总结(含代码示例)

    什么是内部类 什么是内部类? 顾名思义,就是将一个类的定义放在另一个类的内部. 概念很清楚,感觉很简单,其实关键在于这个内部类放置的位置,可以是一个类的作用域范围.一个方法的或是一个代码块的作用域范围 ...

  2. main方法中注入Spring bean

    在有些情况下需要使用main使用Spring bean,但是main方法启动并没有托管给Spring管理,会导致bean失败,报空指针异常. 可以使用 ClassPathXmlApplicationC ...

  3. 关于asp.net调用gemalto超级狗api的具体实现

    鉴于网上关于超级狗的具体操作并不详细,我把我所知道的写下来,希望能给有需求的网友做个参考.软件外壳保护我就不说了,没有什么难度,供应商也会提供文档,一步一步操作即可.我要说的是用于保护发给客户的程序, ...

  4. Activiti6系列(5)- 核心API

    前言 本来想把<疯狂工作流讲义-activiti6.0>这本书里面的实例拿过来,但是这本书我看完后,认为里面编写的activiti6的核心API代码片段不是很清晰,有不少需要雕琢的地方才好 ...

  5. 实现ssr服务端渲染demo

    最近在研究SSR服务器端渲染,自己写了的小demo. 项目布局 ├── build // 配置文件 │   │── webpack.base // 公共配置 │   │── webpack.clien ...

  6. UVA - 1152 --- 4 Values whose Sum is 0(二分)

    问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...

  7. 本地(任意)时间戳转化(转换)标准时间格式 js(eg:2019-05-07 17:49:12)

    <script> function getLocalTime(timestamp) { // 如果以秒为单位 // var dateObj = new Date(timestamp * 1 ...

  8. Codeforces 343D Water Tree

    题意简述 维护一棵树,支持以下操作: 0 v:将以v为跟的子树赋值为1 1 v:将v到根节点的路径赋值为0 2 v:询问v的值 题解思路 树剖+珂朵莉树 代码 #include <set> ...

  9. Tomcat源码分析 (六)----- Tomcat 启动过程(一)

    说到Tomcat的启动,我们都知道,我们每次需要运行tomcat/bin/startup.sh这个脚本,而这个脚本的内容到底是什么呢?我们来看看. 启动脚本 startup.sh 脚本 #!/bin/ ...

  10. Windows Server 2008利用NTFS管理数据

    今天我们学习关于NTFS管理数据 以下是学习的内容NTFS分区和FAT32分区的区别,如何将FAT32分区转化成NTFS分区,FAT 32 不支持大于4G ,NTFS权限设置 ,EFS加密 ,文件夹的 ...