CF862E Mahmoud and Ehab and the function
首先 \(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的更多相关文章
- E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound #include &l ...
- codeforces E. Mahmoud and Ehab and the function(二分+思维)
题目链接:http://codeforces.com/contest/862/problem/E 题解:水题显然利用前缀和考虑一下然后就是二分b的和与-ans_a最近的数(ans_a表示a的前缀和(奇 ...
- 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 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- 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 ...
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
- 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 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
随机推荐
- robotframework:运用JavaScript进行定位元素以及页面操作
在ui自动化时,有些特殊情况需要用到js操作,在进行js操作前要先进行js元素定位.一.js元素定位 1.id定位 document.getElementById("id") 2. ...
- 前端自动打包工具webpack的安装和使用
一.准备 要使用webpack工具,最好了解一些基础的文件目录操作的命令行, win all里的一些常用的命令行 http://blog.csdn.net/qq_36110571/article/de ...
- EFCore(五)——多个DBContext的Code First指定对应的DBContext更新
此环境为ASP.NET Core的项目 1.在需要更新的DBContext里添加空的构造函数 2.打开Nuget命令行选择对应的目录位置 3.带参数-Context指定对应的DBContext 1. ...
- Nacos源码—1.Nacos服务注册发现分析一
大纲 1.客户端如何发起服务注册 + 发送服务心跳 2.服务端如何处理客户端的服务注册请求 3.注册服务-如何实现高并发支撑上百万服务注册 4.内存注册表-如何处理注册表的高并发读写冲突 1.客户端如 ...
- ESP32S3 BLE_HID的编程实现
ESP32S3 BLE_HID的编程实现 BLE是低功耗蓝牙,HID是Human Interface Device,也就是人机接口设备. 主要用于无线连接并传输用户输入数据(如按键.触控.手势等). ...
- linux vim增强使用
目录 删除 编辑 删除 删除当前行 dd 删除当前行后面的所有行 dG 编辑 恢复为未修改前的状态 uu
- Rolldown:下一代JavaScript/TypeScript打包工具,基于Rust的JS打包工具-速度贼快
Vue 团队已正式开源 Rolldown 项目,这是一款基于 Rust 的 JavaScript 打包工具. 项目介绍 Rolldown 是使用 Rust 开发的 Rollup.js 编译工具的替代品 ...
- 【最新】MySQL 5.6 保姆级安装详细教程
MySQL5.6简介 MySQL 5.6 是 MySQL 数据库管理系统的一个重要版本,以其稳定性.性能优化和功能扩充受到广泛关注与使用.该版本在数据库领域中提供了更加高效的数据处理能力.增强的复制功 ...
- 聊聊@Autowired注解的Field injection is not recommended提示问题
1. 前言 在我接触过的大部分Java项目中,经常看到使用@Autowired注解进行字段注入: import org.springframework.beans.factory.annotation ...
- 2. LangChain4j-AIServices,原来调用AI这么简单?
1. 简介 上一章节我们讲了如何使用LangChain4J的底层组件来进行AI的交互,如 ChatLanguageModel.ChatMessage.ChatMemory 等. 在这个层面上工作非常灵 ...