CF992E Nastya and King-Shamans_线段树
Code:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 200000 + 5;
int n,q;
struct Segment_Tree
{
# define lson (o << 1)
# define rson (o << 1) | 1
long long sumv[maxn << 2], maxv[maxn << 2];
inline void maintain(int o){
sumv[o] = sumv[lson] + sumv[rson];
maxv[o] = max(maxv[lson], maxv[rson]);
}
void update(int l,int r,int pos, long long val, int o) {
if(l > r || l > pos || r < pos) return ;
if(l == r) { maxv[o] = sumv[o] = val; return ; }
int mid = (l + r) >> 1;
update(l, mid, pos, val, lson);
update(mid + 1, r, pos, val, rson);
maintain(o);
}
inline long long query(int l,int r,int L,int R,int o)
{
if(l > r || l > R || r < L) return 0;
if(l >= L && r <= R) return sumv[o];
int mid = (l + r) >> 1;
return query(l, mid, L, R, lson) + query(mid + 1, r, L, R, rson);
}
inline int dfs(int l,int r,long long val, int o)
{
if(l == r) return l;
int mid = (l + r) >> 1;
if(maxv[lson] >= val) return dfs(l, mid, val, lson);
else return dfs(mid + 1, r, val, rson);
}
int get(int l,int r, int L, int R, long long val,int o)
{
if(l > r || l > R || r < L) return -1;
if(l >= L && r <= R)
{
if(maxv[o] < val) return -1;
return dfs(l, r, val, o);
}
int mid = (l + r) >> 1, h;
h = get(l, mid, L, R, val, lson); if(h != -1) return h;
h = get(mid + 1, r, L, R, val, rson); if(h != -1) return h;
return -1;
}
}T;
inline void solve()
{
int l = 1;
for(;;) {
long long sumv = T.query(1, n, 1, l - 1, 1);
int pos = T.get(1, n, l, n, sumv, 1);
if(pos == -1) { printf("-1\n"); return ; }
else{
if(sumv + T.query(1, n, l, pos - 1, 1) == T.query(1, n, pos, pos, 1)) { printf("%d\n", pos); return ;}
l = pos + 1;
if(l > n) { printf("-1\n"); return ;}
}
}
}
int main()
{ scanf("%d%d",&n,&q);
for(int i = 1;i <= n; ++i) { long long a; scanf("%I64d",&a); T.update(1, n, i, a, 1); }
while(q--){
int pos;
long long h;
scanf("%d%I64d",&pos, &h);
T.update(1, n, pos, h, 1);
solve();
}
return 0;
}
CF992E Nastya and King-Shamans_线段树的更多相关文章
- CF992E Nastya and King-Shamans(线段树二分+思维)
这是一道卡常好题 从160s卡到36s qwq 由于题目设计到原数组的单点修改,那么就对应着前缀和数组上的区间加. 很显然能想到用线段树来维护这么个东西. 那么该如果求题目要求的位置呢 我们来看这个题 ...
- Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans(线段树)
题意 给出一个长度为 \(n\) 的序列 \(\{a_i\}\) , 现在会进行 \(m\) 次操作 , 每次操作会修改某个 \(a_i\) 的值 , 在每次操作完后你需要判断是否存在一个位置 \(i ...
- HPU组队赛J:Ball King(线段树)
时间限制 1 Second 内存限制 512 Mb 题目描述 HPU601球王争霸赛即将举行,ACMER纷纷参加. 现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜. 为保证比赛公 ...
- 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 ...
- hdu5643 King's Game(约瑟夫环+线段树)
Problem Description In order to remember history, King plans to play losephus problem in the parade ...
- codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)
题目链接: http://codeforces.com/contest/1136/problem/E 题意: 初始有a数组和k数组 有两种操作,一,求l到r的区间和,二,$a_i\pm x$ 并且会有 ...
- cf1136E. Nastya Hasn't Written a Legend(二分 线段树)
题意 题目链接 Sol yy出了一个暴躁线段树的做法. 因为题目保证了 \(a_i + k_i <= a_{i+1}\) 那么我们每次修改时只需要考虑取max就行了. 显然从一个位置开始能影响到 ...
- Codeforces 1136E Nastya Hasn't Written a Legend 线段树
vp的时候没码出来.. 我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并, 可以发现我们最多合并O(n)次. 然后写个线段树就没了. #in ...
- Codeforces 1136E - Nastya Hasn't Written a Legend - [线段树+二分]
题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$, ...
- Nastya and King-Shamans CodeForces - 992E (线段树二分)
大意: 给定序列a, 单点更新, 询问是否存在a[i]等于s[i-1], s为a的前缀和, a非负 考虑到前缀和的单调性, 枚举从1开始前缀和, 找到第一个大于等于s[1]的a[i], 如果相等直接输 ...
随机推荐
- Javase 简单练习
public class Test10 { public static void main(String[] args) { System.out.println("------------ ...
- 使用javadoc 报错:编码GBK的不可映射字符
运行命令:javadoc Test1.java 报错:编码GBK的不可映射字符 问题原因:类文件中带有非GBK字符 解决办法:javadoc -encoding utf-8 Test1.java
- 【JavaScript框架封装】实现一个类似于JQuery的内容框架的封装
// 内容框架 (function (xframe) { // 需要参与链式访问的(必须使用prototype的方式来给对象扩充方法) xframe.extend({ /** * .html()用为读 ...
- php strtotime 同样的函数为何在不同的地方输出的结果不同?
方法1:调用函数 date_default_timezone_set('Asia/Shanghai'); // 如果是中国的话 方法2:设置php.ini 中data.timezone [Date] ...
- git-osc自己定义控件之:CircleImageView
git-osc自己定义控件之:CircleImageView 一.CircleImageView的使用 在项目中能够发现,用户的头像都是圆形的.感觉非常好奇,昨天最终发现了,原来是自定了一个Image ...
- Mybatis分页插件2.0版本号公布
项目地址:http://git.oschina.net/free/Mybatis_PageHelper 软件介绍:http://www.oschina.net/p/mybatis_pagehelper ...
- MySql 基础学习笔记 1——概述与基本数据类型: 整型: 1)TINYINT 2)SMALLINT 3) MEDIUMINT 4)INT 5)BIGINT 主要是大小的差别 图 浮点型:命令
一.CMD中经常使用mysql相关命令 mysql -D, --database=name //打开数据库 --delimiter=name //指定分隔符 -h, --host=name // ...
- android AChartEnginee解说之源代码框架解读
从上周把android ACHartEnginee的源代码check out出来后就一直在看这个东西是怎样使用的,以及底层是怎样实现的,把近期一周对这个东西的了解先发上来,即是给自己做一个总结,也希望 ...
- virtual table(有180个评论)
To implement virtual functions, C++ uses a special form of late binding known as the virtual table. ...
- UESTC--1272--Final Pan's prime numbers(水题)
Final Pan's prime numbers Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & % ...