大意: 给定序列a, 单点更新, 询问是否存在a[i]等于s[i-1], s为a的前缀和, a非负

考虑到前缀和的单调性, 枚举从1开始前缀和, 找到第一个大于等于s[1]的a[i], 如果相等直接输出.

若不满足则只能在a[i]的右侧, 此时前缀和为s[i], 至少为s[1]的两倍, 故最多进行log次.

具体实现的话, 线段树维护a与s的差, 二分找第一个非负即可, 复杂度$O(nlog^2n)$.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (lc|1)
#define ls lc,l,mid
#define rs rc,mid+1,r
using namespace std;
typedef long long ll; const int N = 2e6+10;
int a[N], n, m;
ll b[N], v[N<<2], tag[N<<2]; ll build(int o, int l, int r) {
if (l==r) return v[o]=a[l]-b[l-1];
return v[o]=max(build(ls),build(rs));
} void pd(int o) {
if (tag[o]) {
tag[lc]+=tag[o],tag[rc]+=tag[o];
v[lc]+=tag[o],v[rc]+=tag[o];
tag[o]=0;
}
} void update(int o, int l, int r, int ql, int qr, int k) {
if (ql<=l&&r<=qr) return v[o]+=k,tag[o]+=k,void();
pd(o);
if (mid>=ql) update(ls,ql,qr,k);
if (mid<qr) update(rs,ql,qr,k);
v[o]=max(v[lc],v[rc]);
} int query(int o, int l, int r) {
if (v[o]<0) return -1;
if (l==r) return v[o]?-1:l;
pd(o);
int t = -1;
if (mid>=1) t=query(ls);
if (~t) return t;
if (mid<n) t=query(rs);
return t;
} int main() {
scanf("%d%d", &n, &m);
REP(i,1,n) scanf("%d",a+i),b[i]=b[i-1]+a[i];
build(1,1,n);
REP(i,1,m) {
int p, x, t;
scanf("%d%d", &p, &x);
t=x, x=x-a[p], a[p]=t;
update(1,1,n,p,p,x);
if (p!=n) update(1,1,n,p+1,n,-x);
printf("%d\n", query(1,1,n));
}
}

Nastya and King-Shamans CodeForces - 992E (线段树二分)的更多相关文章

  1. Codeforces 1136E - Nastya Hasn't Written a Legend - [线段树+二分]

    题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$, ...

  2. Blog Post Rating CodeForces - 806E (线段树二分)

    题目链接 题目大意: 有一个博客, 初始分数为$0$, 有$n$个人, 第$i$个人有一个期望值$a_i$, 如果第$i$个人浏览博客时,博客赞数高于$a_i$博客分数$-1$, 低于$+1$, 相等 ...

  3. Bash and a Tough Math Puzzle CodeForces - 914D (线段树二分)

    大意:给定序列, 单点修改, 区间询问$[l,r]$内修改至多一个数后$gcd$能否为$x$ 这题比较有意思了, 要注意到询问等价于$[l,r]$内最多有1个数不为$x$的倍数 可以用线段树维护gcd ...

  4. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  5. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  6. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  7. Educational Codeforces Round 64 (Rated for Div. 2) (线段树二分)

    题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n  n个数,然后求有多少个区间[l,r] 满足    a[l]+a[r]=max([l, ...

  8. hdu4614 线段树+二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  9. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

随机推荐

  1. 【百度统计】设置页面元素点击事件转化pv、uv

    html元素点击事件内添加代码:_hmt.push(['_trackEvent', category, action, opt_label, opt_value]); 1. '_trackEvent' ...

  2. 关于Context []startup failed due to previous errors

    文章转自:http://blog.sina.com.cn/s/blog_49b4a1f10100q93e.html 框架搭建好后,启动服务器出现如下的信息: log4j:WARN No appende ...

  3. Git本地仓库与远程github同步的时候提示fatal: remote origin already exists 错误解决办法

    Git本地仓库与远程github同步的时候提示fatal: remote origin already exists 错误解决办法 1.git在本地的电脑创建了仓库,要远程同步github的仓库.使用 ...

  4. 基于webview的Hybrid app和React Native及html5

    基于webview的Hybrid app和React Native及html5 React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iO ...

  5. [转载]ASP.NET中IsPostBack详解

    1.IsPostBack介绍Page.IsPostBack是一个标志:当前请求是否第一次打开. 调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者 ...

  6. Web安全学习笔记之DES算法实例详解

    转自http://www.hankcs.com/security/des-algorithm-illustrated.html 译自J. Orlin Grabbe的名作<DES Algorith ...

  7. 解决mysql的Too many connections

    解决: /etc/my.cnf vim编辑 添加 max_connections= wait_timeout= 然后执行code service mysqld reload service mysql ...

  8. 20165211 获奖感想及java课程总结

    20165211 获奖感想及java课程总结 理论脱离实践是最大的不幸.--达芬奇 这句话,是我在学习Java之前,假期内写的20165211 学习基础和C语言调查里的所引用的一句话,是当时我对Jav ...

  9. noip2008 真题练习 2017.2.25

    不是有很多可以说的,记住不能边算边取min Code #include<iostream> #include<fstream> #include<sstream> ...

  10. boot sector FAT