Codeforces 992 E. Nastya and King-Shamans
\(>Codeforces\space992 E. Nastya and King-Shamans<\)
题目大意 : 给你一个长度为 \(n\) 的序列,有 \(q\) 次操作,每一次操作将一个数 \(A_i\) 改为另外一个数。每一次操作结束时,你需要找出一个位置 \(x\) 满足 \(A_x = sum_{x-1}\) 其中 \(sum\) 表示前缀和
$n , q \leq 2 \times 10^5 \ 0 \leq A_i \leq 10^9 $
解题思路 :
博主亲测分块加均摊分析的做法会因为常数太大 \(TLE\) 掉,在此就不多讨论了
问题要求出满足 \(A_x = sum_{x-1}\) 的位置,这个可以转化为 \(sum_x = 2 \times sum_{x-1}\)
我们考虑从 \(A_{p=1}\) 开始跳,每一次跳到其后面一个最小的 \(k - 1\) ,满足\(sum_k \geq 2 \times sum_p\)
可以证明如果有答案且 \(sum_{ans} > 0\),那么答案一定在所有的 \(k\) 之中产生
不妨用反证法来证明,假设当且跳到点 \(k\) ,接下来选取的点是 \(k' \ (k < k')\) ,对于 \(k < i < k' - 1\)
如果说 \(i\) 是答案的话,设 \(y\) 为 第一个满足 $ sum_y \geq 2 \times sum_i$ 的点。
因为\(sum_y \geq sumk\) 所以必然有 $ y \geq k' $ ,如果 \(i < k' - 1\) 那么 $ y - i > 1$ , \(i\) 不是答案
所以证明了这样跳,如果有答案的话答案必然在跳到的点上
所以可以用树状数组维护前缀和,每一次暴力二分跳,跳 \(log\) 次就能跳完,总复杂度是\(O(nlog^3n)\)
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
if(f) x = -x;
}
#define N (300005)
#define int ll
ll c[N], a[N], n, q;
inline void add(int x, ll y){ for(int i = x; i <= n; i += i & -i) c[i] += y; }
inline ll sum(int x){ ll ans = 0; for(int i = x; i; i -= i & -i) ans += c[i]; return ans; }
inline void solve(){
int x = 1;
if(sum(1) == 0) return (void)( puts("1") );
while(x < n){
int l = x + 1, r = n, k = x, now = 2 * sum(x);
if(sum(x + 1) == now) return (void) (printf("%d\n", x + 1));
while(l <= r){
int mid = l + r >> 1;
if(sum(mid) < now) k = mid, l = mid + 1; else r = mid - 1;
}
if(k + 1 > n) break;
x = (k == x) ? k + 1 : k;
}
puts("-1");
}
main(){
read(n), read(q);
for(int i = 1; i <= n; i++) read(a[i]), add(i, a[i]);
for(int i = 1; i <= q; i++){
int x, y; read(x), read(y);
add(x, y - a[x]), a[x] = y, solve();
}
return 0;
}
Codeforces 992 E. Nastya and King-Shamans的更多相关文章
- codeforces A. Rook, Bishop and King 解题报告
题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...
- Codeforces 3A-Shortest path of the king(BFS打印路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- CodeForces 370A Rook, Bishop and King
此题看似很简单,但实际上有不少细节,WA点不少.分情况处理即可. #include<cmath> #include<cstdio> #include<string> ...
- codeforces#1136 C. Nastya Is Transposing Matrices(找规律)
题意:给出两个n*m的矩阵,每次操作可以让一个正方形矩阵行列交换.问,在无限次操作下,第一个矩阵能否变成第二个矩阵 分析:先把操作限定在2*2的矩阵中.这样对角线上的元素就可以随意交换.也就是说,如果 ...
- 【Codeforces 992B】Nastya Studies Informatics
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 因为gcd(a,b)=x 所以设a = nx b = mx 又有ab/gcd(a,b)=lcm(a,b)=y 则nmx = y 即n(m*x) ...
- Codeforces 992 范围内GCD,LCM要求找一对数 衣柜裙子期望
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- Codeforces Beta Round #3 A. Shortest path of the king 水题
A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...
随机推荐
- [BZOJ1911][BZOJ1912][BZOJ1913]APIO2010解题报告
特别行动队 Description 这个好像斜率优化不是一般地明显了啊...只不过要分a的正负两种情况考虑是维护上凸还是下凸 /********************************** ...
- mysql查询日期相关的
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...
- img图片居中
关键词:clear: both; display: block; margin:auto; 图片居左,居右,居中: /* Alignment */ .alignleft { display ...
- .net APIHelper client获取数据
using Newtonsoft.Json; using System.Net.Http.Headers; public static class APIHepler { public static ...
- 通过cordova将vue项目打包为webapp
准备工作:需要之前配置好vue-cli脚架构,安装好cordova环境.下面开始对vue.js项目进行打包,打包环境为Android. 可以看下我的github:https://github.com/ ...
- 图片异或(xor)getflag
题目地址:https://files.cnblogs.com/files/nul1/flag_enc.png.tar 这题是源于:网鼎杯minified 经过测试隧道红色最低通道异常.其余均正常.所以 ...
- 《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!【转】
转自:http://www.cnblogs.com/bitzhuwei/p/OS-in-30-days-01-hello-bitzhuwei-OS.html 阅读目录(Content) 最初的OS代码 ...
- 15个你不得不知道的Chrome dev tools的小技巧
转载自:https://www.imooc.com/article/2559 谷歌浏览器如今是Web开发者们所使用的最流行的网页浏览器.伴随每六个星期一次的发布周期和不断扩大的强大的开发功能,Chro ...
- 省市区ajax联动
function setCity1(){ var areaId1 = $('#areaId1').val(); var cityId1 = $('#cityId1'); var cityOpt = $ ...
- WPF之模拟打开或关闭Windows功能
用WPF模拟打开或关闭Windows功能的实现方法其实很简单,主要用递归判断当前节点的子节点和父节点的选中状态就行了. 一.效果演示 先看看效果图: 二.部分代码 xaml代码: <TreeVi ...