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 ...
随机推荐
- 用体渲染的方法在Unity中渲染云(18/4/4更新)
github: https://github.com/yangrc1234/VolumeCloud 更新的内容在底部 最近在知乎上看到一篇文章讲云层的渲染(https://zhuanlan.zhihu ...
- CSS Sprite笔记
1. 什么是CSS Sprite 将一些碎小的图片拼接为一张大点的图片来使用,目的是为了减少浏览器的http请求次数以提高网页的加载速度.每次请求图片都需要跟服务器建立一次连接,并且浏览器的最大并发请 ...
- VC连接access
(1)首先拷贝 c:\program files\common files\system\ado\ 目录中的 msado15.dll 文件到项目中. (2)在VC中加入DLL,具体方法如下: (3)创 ...
- 【C++自我精讲】基础系列六 PIMPL模式
[C++自我精讲]基础系列六 PIMPL模式 0 前言 很实用的一种基础模式. 1 PIMPL解释 PIMPL(Private Implementation 或 Pointer to Implemen ...
- Wireshark抓包保存文件(图片,视频,音频等)
1.首先选择一个图片的分组 如图的9801 就是JPG 2.对下面的窗口里面选中JPEG File Interchange Format 右键选择 导出分组字节流 3.文件输入XXX.jpg,注意保存 ...
- SwitchSharp代理插件的安装和使用
参考链接: http://bbs.feng.com/read-htm-tid-8227283.html 安装参考链接: http://jingyan.baidu.com/article/380abd0 ...
- 基于TCP协议的聊天室控制台版
我之前写过一篇博客,主要是基于TCP协议实现的聊天室swing版,在此再写一个基于TCP协议实现的聊天室控制台版,便于学习和比较. package 聊天室console版.utils; import ...
- leetcode Two Sum II - Input array is sorted <面试常考题>
题目描述 //二分查找的变形 用头尾两个指针进行 面试考察题 class Solution { public: vector<int> twoSum(vector<int> ...
- C# 中DateTime的各种使用
获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = DateTime ...
- jupyter notebook,弄起来