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_线段树的更多相关文章

  1. CF992E Nastya and King-Shamans(线段树二分+思维)

    这是一道卡常好题 从160s卡到36s qwq 由于题目设计到原数组的单点修改,那么就对应着前缀和数组上的区间加. 很显然能想到用线段树来维护这么个东西. 那么该如果求题目要求的位置呢 我们来看这个题 ...

  2. Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans(线段树)

    题意 给出一个长度为 \(n\) 的序列 \(\{a_i\}\) , 现在会进行 \(m\) 次操作 , 每次操作会修改某个 \(a_i\) 的值 , 在每次操作完后你需要判断是否存在一个位置 \(i ...

  3. HPU组队赛J:Ball King(线段树)

    时间限制 1 Second  内存限制 512 Mb 题目描述 HPU601球王争霸赛即将举行,ACMER纷纷参加. 现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜. 为保证比赛公 ...

  4. 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 ...

  5. hdu5643 King's Game(约瑟夫环+线段树)

    Problem Description In order to remember history, King plans to play losephus problem in the parade ...

  6. codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)

    题目链接: http://codeforces.com/contest/1136/problem/E 题意: 初始有a数组和k数组 有两种操作,一,求l到r的区间和,二,$a_i\pm x$ 并且会有 ...

  7. cf1136E. Nastya Hasn't Written a Legend(二分 线段树)

    题意 题目链接 Sol yy出了一个暴躁线段树的做法. 因为题目保证了 \(a_i + k_i <= a_{i+1}\) 那么我们每次修改时只需要考虑取max就行了. 显然从一个位置开始能影响到 ...

  8. Codeforces 1136E Nastya Hasn't Written a Legend 线段树

    vp的时候没码出来.. 我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并, 可以发现我们最多合并O(n)次. 然后写个线段树就没了. #in ...

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

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

  10. Nastya and King-Shamans CodeForces - 992E (线段树二分)

    大意: 给定序列a, 单点更新, 询问是否存在a[i]等于s[i-1], s为a的前缀和, a非负 考虑到前缀和的单调性, 枚举从1开始前缀和, 找到第一个大于等于s[1]的a[i], 如果相等直接输 ...

随机推荐

  1. JS 封装一个求数组最大值的函数

    var aa = [1,2,3,4,9,2,5]; z(aa); function z(attr){ var b = 0 for(var i =1;i<aa.length;i++){ if(aa ...

  2. 路飞学城Python-Day115

    个人博客搭建 from django.db import models from django.contrib.auth.models import User, AbstractUser # Crea ...

  3. 网络流入门——EK算法

    转载:https://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html 网络流的相关定义: 源点:有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点. ...

  4. Javascript解析URL

    举个栗子,一个网页的URL为https://i.cnblogs.com/EditPosts.aspx?opt=1,要分离出通信协议.host.port.path.query.hash等值.这时候我们应 ...

  5. XSS Chanllenges 16-19

    Stage #16 同样为DOM 型XSS ,document.write() 方法 插入代码 \x3cscript\x3ealert(document.domain)\x3c/script\x3e ...

  6. (1)spring boot起步之Hello World【从零开始学Spring Boot】

    Spring Boot交流平台 1.1 介绍 自从structs2出现上次的漏洞以后,对spring的关注度开始越来越浓. 以前spring开发需要配置一大堆的xml,后台spring加入了annot ...

  7. Could not publish server configuration for Tomcat v7.0 Server at localhost. Multiple Contexts have a path of "/ezoutdoor".

    Could not publish server configuration for Tomcat v7.0 Server at localhost. Multiple Contexts have a ...

  8. idea常用方便的快捷键

    Ctrl+D 复制行Ctrl+F 查找文本Ctrl+G 定位到某行Ctrl+H 显示类结构图(类的继承层次)Ctrl+I 实现方法ctrl+J 显示所有快捷键模板ctrl+k 提交代码到SVNCrtl ...

  9. Android网络编程(十)Retrofit2后篇[注解]

    G相关文章 Android网络编程(一)HTTP协议原理 Android网络编程(二)HttpClient与HttpURLConnection Android网络编程(三)Volley用法全解析 An ...

  10. 【分享】GEARS of DRAGOON 1+2【日文硬盘版】[带全CG存档&amp;攻略+SSG改动+打开存档补丁]

    冒险者们哟.寻找龙秘玉吧--! ninetail的最新作,是使用丰富多彩的技能·道具探索迷宫的3D迷宫RPG! 存在着骑士和神官的架空世界常见的职业为首的13种职业.超过数百种的道具的登场! 和伙伴一 ...