Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans(线段树)
题意
给出一个长度为 \(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(线段树)的更多相关文章
- Codeforces Round #489 (Div. 2) E - Nastya and King-Shamans
E - Nastya and King-Shamans 题目大意:有n个数,每一次操作更改一个数,每次操作之后问你是否有一个数等于其前面所有数的和. 思路:好题,想了很久没想出来,看了题解,主要思想就 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- 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 ...
- 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 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- 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 ...
- 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, ...
- 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 ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
随机推荐
- Linux常用软件启动、停止、重启命令
一.PHP 启动命令: /usr/local/php5/sbin/php-fpm 停止命令: pkill php-fpm 二.MySQL 启动命令: /etc/init.d/mysqld start ...
- php微信公众号开发入门小教程
1.配置相关服务器 (1) 如下,把自己的服务器ip白名单配置上: (2) 开始配置令牌,配置令牌时先需要把现成的代码放到自己的服务器上面,代码里面包含自己的设置的令牌号码,这样才可以配置成功. 注意 ...
- 实用小技巧(一):UIScrollView中上下左右滚动方向的判断
https://www.jianshu.com/p/93e8459b6dae 2017.06.01 01:13* 字数 674 阅读 1201评论 0喜欢 1 2017.06.01 01:13* 字数 ...
- Python_动态参数、名称空间、作用域、作用域链、加载顺序、函数的嵌套、global、nonlocal
1.动态参数 当实参数量与形参数量相等时,参数传递正常. def func1(a, b, c): pass func1(1, 2, 3) 当实参数量与形参数量不相等时,则会报错. def func1( ...
- Swagger UI 用法
Swagger - 简书https://www.jianshu.com/p/4115f2b53983 Swagger简介 - Ghost Stories - CSDN博客https://blog.cs ...
- js数据放入缓存,需要再调用
再贴代码之前先描述下,这个技术应用的场景:一个页面的http请求次数能少点就少,这样大大提高用户体验.所以再一个页面发起一个请求,把所有数据都拿到后储存在缓存里面,你想用的时候再调用出来,这个是非常好 ...
- 判断String类型字符串是否为空的方法
在项目中经常遇到要判断String类型的字段是否为空操作 我们可以用Apache提供的StringUtils这个工具类,不用自己去判断,也不用自己封装判断空的方法 它有两个版本,一个是org.apac ...
- 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...
- php-memcached详解
一.memcached 简介 在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东.这里简单介绍一下,memcached 是高 ...
- rsync: chgrp "/.hosts.NBCxBB" (in test) failed: Operation not permitted (1)
#记一次rsync出现的错误(网上基本都是说权限问题) #这并不是权限的问题,应为实际的文件已经传过去了,但是rsync就是会报这个错误,(虽然使用是正常的,但是看着就是不爽) [root@local ...