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,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
随机推荐
- git的用法步骤讲解
1.创建全局的本地用户名 git config --global user.name "teamemory" git config --global user.email &quo ...
- docker vm 性能优劣
Docker容器与虚拟机区别 - unixfbi.com - 博客园 http://www.cnblogs.com/pangguoping/articles/5515286.html docker与虚 ...
- Centos6.x升级内核方法支持Docker
Centos6升级内核方法_百度经验https://jingyan.baidu.com/article/7e4409531bda252fc1e2ef4c.html
- php之IP
常用的获取客户端的IP地址的方法: 1) function getRemoteIp(){ if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $ip = $_SE ...
- oracle创建表空间、创建用户、授权角色和导入导出用户数据
使用数据库管理员身份登录 -- log as sysdba sqlplus / as sysdba; 创建临时表空间 -- create temporary tablespace create tem ...
- class用法
自 PHP 5.5 起,关键词 class 也可用于类名的解析.使用 ClassName::class 你可以获取一个字符串,包含了类 ClassName 的完全限定名称.这对使用了 命名空间 的类尤 ...
- MyBatis映射文件3(参数处理Map)
参数命名 POJO 如果多个参数,正好是业务逻辑的数据模型,那么我们就可以直接传入POJO,这样#{}中就可以直接使用属性名 Map 如果多个参数不是业务逻辑的数据模型,没有对应的POJO,为了方便, ...
- CodeForces 113B Petr#
题目链接:http://codeforces.com/problemset/problem/113/B 题目大意: 多组数据每组给定3个字符串T,Sbeg,Sed,求字符串T中有多少子串是以Sbeg开 ...
- PhpStorm本地断点调试
一.断点调试php环境搭建 1.检测本地php环境是否安装了Xdebug 在本地输出phpinfo():搜索Xdebug;如下图 如果没有安装,安装操作Xdebug如下: 将phpinfo();的信 ...
- shell expr用法
expr 计算整数变量值 使用方法如下: linux-zpycfm:/home/test/shell # s=+ -bash: +: command not found linux-zpycfm:/h ...