\(>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的更多相关文章

  1. codeforces A. Rook, Bishop and King 解题报告

    题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...

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

  3. CodeForces 370A Rook, Bishop and King

    此题看似很简单,但实际上有不少细节,WA点不少.分情况处理即可. #include<cmath> #include<cstdio> #include<string> ...

  4. codeforces#1136 C. Nastya Is Transposing Matrices(找规律)

    题意:给出两个n*m的矩阵,每次操作可以让一个正方形矩阵行列交换.问,在无限次操作下,第一个矩阵能否变成第二个矩阵 分析:先把操作限定在2*2的矩阵中.这样对角线上的元素就可以随意交换.也就是说,如果 ...

  5. 【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) ...

  6. Codeforces 992 范围内GCD,LCM要求找一对数 衣柜裙子期望

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std ...

  7. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

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

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

随机推荐

  1. String类的一些常用操作方法

    package com.liveyc.framework.util; import java.io.UnsupportedEncodingException; import java.net.URLD ...

  2. javac -cp java -cp

    ///////////////////////////////////////////////////////////////////////////////////// 编译java文件的命令都知道 ...

  3. Coursera在线学习---第一节.梯度下降法与正规方程法求解模型参数比较

    一.梯度下降法 优点:即使特征变量的维度n很大,该方法依然很有效 缺点:1)需要选择学习速率α 2)需要多次迭代 二.正规方程法(Normal Equation) 该方法可以一次性求解参数Θ 优点:1 ...

  4. Python排序算法之插入排序

    # 插入排序的工作原理是,对于每个未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.## 步骤:## 从第一个元素开始,该元素可以认为已经被排序# 取出下一个元素,在已经排序的元素序列中从后 ...

  5. 关于"轉淚點"与"轉捩點"

    经常看台湾偶像剧或台湾综艺节目的人,一定听过"转泪点"这个词,虽然我一直不知道这三个字具体是怎么写, 但其意思很容易明白,就是"转折点"的意思.今天无聊在看凤凰 ...

  6. 使用jolokia api监控ActiveMQ

    jolokia api提供了一种通过HTTP访问JMX获得AMQ后台数据的一种方式,即Restful Api #!/usr/bin/env python # -*- coding:utf-8 -*- ...

  7. MemCached缓存操作

    Web项目在运行时,通常需要从数据库中进行读写.随着操作数据量的增大,以及访问量的集中,数据库的负载增加,数据库响应变慢,网站访问速度变慢的情况.Memcached就是用来解决这些问题的. Memca ...

  8. acm专题---键树

    题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...

  9. 转:PHP环境搭建 - Linux

    本文PHP环境采用,nginx + PHP7 + mysql 5.6 一.安装mysql 5.6 参见:http://www.cnblogs.com/rslai/p/7853465.html 二.Ng ...

  10. IndexWriterConfig的各个配置项说明(转)

    1.Analyzer:分析器 2.matchVersion:所用Lucene的版本 3.ramBufferSizeMB:随机内存 默认为16M. 用于控制buffer索引文档的内存上限,如果buffe ...