题意

给出一个长度为 \(n\) 的序列 \(\{a_i\}\) , 现在会进行 \(m\) 次操作 , 每次操作会修改某个 \(a_i\) 的值 ,

在每次操作完后你需要判断是否存在一个位置 \(i\), 满足 \(\displaystyle a_i = \sum_{j=1}^{i - 1}a_j\) 并求任意一个 \(i\) .

\(n, m ≤ {10}^5 , 0 ≤ a_i ≤ {10}^9\)

题解

考虑一个暴力,不妨首先从 \(i = 1\) 开始判断是否合法,由于 \(a_i\) 是单调不降的,符合条件的 \(a_i\) 一定大于等于当前前缀和。

每次用线段树找到大于等于当前前缀和的最左边的 \(a_i\) 并判断是否合法。(这个位置一定要在前缀和的右边)

这样为什么是对的呢,因为你下一次的起点不可能存在与现在的位置到下次位置之间的任意位置。

(因为每个值都不会 \(\ge\) 当前的前缀和)

这样做就是对的了,因为如果新的 \(a_i​\) 不合法,那么当前前缀和至少会达到上一次的两倍。

于是这样子查找的次数就是 \(O(\log w)\) 的 , 总复杂度是 \(O(m \log n \log w)\) .

总结

考虑一类题对于权值翻倍时候的复杂度是 \(O(\log w)\) 的,例如 ZJOI2018 历史

代码

#include <bits/stdc++.h>

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__) using namespace std; template<typename T> inline bool chkmin(T &a, T b) {return b < a ? a = b, 1 : 0;}
template<typename T> inline bool chkmax(T &a, T b) {return b > a ? a = b, 1 : 0;} inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
} void File() {
#ifdef zjp_shadow
freopen ("E.in", "r", stdin);
freopen ("E.out", "w", stdout);
#endif
} const int N = 2e5 + 1e3; typedef long long ll; #define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r int a[N]; template<int Maxn>
struct Segment_Tree { int maxv[Maxn]; ll sumv[Maxn]; inline void Push_Up(int o) {
sumv[o] = sumv[o << 1] + sumv[o << 1 | 1];
maxv[o] = max(maxv[o << 1], maxv[o << 1 | 1]);
} void Build(int o, int l, int r) {
if (l == r) return (void)(maxv[o] = sumv[o] = a[l]);
int mid = (l + r) >> 1; Build(lson); Build(rson); Push_Up(o);
} void Update(int o, int l, int r, int up, int uv) {
if (l == r) return (void)(sumv[o] = maxv[o] = uv);
int mid = (l + r) >> 1;
if (up <= mid) Update(lson, up, uv);
else Update(rson, up, uv); Push_Up(o);
} ll Sum(int o, int l, int r, int qr) {
if (r <= qr) return sumv[o];
int mid = (l + r) >> 1;
return Sum(lson, qr) + (qr > mid ? Sum(rson, qr) : 0);
} int Find(int o, int l, int r, int ql, ll val) {
if (ql > r || maxv[o] < val) return 0;
if (l == r) return l; int mid = (l + r) >> 1;
int pos = Find(lson, ql, val);
return pos ? pos : Find(rson, ql, val);
} }; Segment_Tree<N << 2> T; int n, q; inline int Solve() {
if (a[1] == 0) return 1;
int cur = 1; ll sum = a[1];
for (;;) {
int pos = T.Find(1, 1, n, cur + 1, sum);
if (!pos) return -1;
sum = T.Sum(1, 1, n, (cur = pos) - 1);
if (sum == a[pos]) return pos; sum += a[pos];
}
} int main () { File(); n = read(), q = read(); For (i, 1, n) a[i] = read();
T.Build(1, 1, n); For (i, 1, q) {
int pos = read(), val = read();
T.Update(1, 1, n, pos, a[pos] = val);
printf ("%d\n", Solve());
} return 0; }

Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans(线段树)的更多相关文章

  1. Codeforces Round #489 (Div. 2) E - Nastya and King-Shamans

    E - Nastya and King-Shamans 题目大意:有n个数,每一次操作更改一个数,每次操作之后问你是否有一个数等于其前面所有数的和. 思路:好题,想了很久没想出来,看了题解,主要思想就 ...

  2. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  3. Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树

    C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...

  4. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  5. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  6. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

  7. Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

    题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...

  8. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

    B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...

  9. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

随机推荐

  1. 出题人的女装(牛客练习赛38题B) (概率+分式运算)

    链接:https://ac.nowcoder.com/acm/contest/358/B来源:牛客网 出题人的女装 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  2. PHP的内存回收(GC)

    php官方对gc的介绍:http://php.net/manual/zh/features.gc.php

  3. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in range(128)

    python报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-25: ordinal not in ...

  4. CMMI摘要

    CMMI_百度百科https://baike.baidu.com/item/CMMI CMMI分为哪几个等级?CMMI等级介绍_百度经验https://jingyan.baidu.com/articl ...

  5. webdriver问题汇总

    如果你的selenium是3.X版本的,火狐浏览器需要geckodriver这个组件的支持,而谷歌浏览器需要chromedriver的支持,selenium是2.X版本则不需要. 使用selenium ...

  6. 1244. Minimum Genetic Mutation

    描述 A gene string can be represented by an 8-character long string, with choices from "A", ...

  7. jQuery EasyUI 折叠面板accordion的使用实例

    1.对折叠面板区域 div 设置 class=”easyui-accordion” 2.在区域添加多个 div, 每个 div 就是一个面板 (每个面板一定要设置 title 属性). 3.设置面板属 ...

  8. mybatis数据源与连接池

    1.概念介绍1.1 数据源:顾名思义,数据的来源,它包含了数据库类型信息,位置和数据等信息,一个数据源对应一个数据库. 1.2 连接池:在做持久化操作时,需要通过数据库连接对象来连接数据库,而连接池就 ...

  9. 区分Python中的可变对象和不可变对象

    参考: https://www.cnblogs.com/sun-haiyu/p/7096918.html """不过注意函数传参既不是传值也不是传引用,正确的叫法是传对象 ...

  10. myisam和innodb的区别对比

    1.MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法.不 ...