[题目链接]

http://codeforces.com/contest/992/problem/E

[算法]

线段树 + 二分

时间复杂度 : O(NlogN^2)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
typedef long long ll; struct Node
{
int l,r;
ll mx,sum;
} Tree[MAXN << ]; int i,n,q,x,y,cur,tmp,ans;
ll value[MAXN];
ll pre; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar())
{
if (c == '-') f = -f;
}
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void update(int index)
{
Tree[index].mx = max(Tree[index << ].mx,Tree[index << | ].mx);
Tree[index].sum = Tree[index << ].sum + Tree[index << | ].sum;
}
inline void build(int index,int l,int r)
{
int mid;
Tree[index].l = l;
Tree[index].r = r;
if (l == r)
{
Tree[index].mx = value[l];
Tree[index].sum = value[l];
return;
}
mid = (l + r) >> ;
build(index << ,l,mid);
build(index << | ,mid + ,r);
update(index);
}
inline void modify(int index,int pos,int val)
{
int mid;
if (Tree[index].l == Tree[index].r)
{
Tree[index].mx = Tree[index].sum = val;
return;
}
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= pos) modify(index << ,pos,val);
else modify(index << | ,pos,val);
update(index);
}
inline int query(int index,int l,int r,ll val)
{
int mid,tmp;
if (Tree[index].l == l && Tree[index].r == r)
{
if (Tree[index].mx < val) return -;
if (l == r) return l;
mid = (Tree[index].l + Tree[index].r) >> ;
if (Tree[index << ].mx >= val) return query(index << ,l,mid,val);
else return query(index << | ,mid + ,r,val);
}
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) tmp = query(index << ,l,r,val);
else if (mid + <= l) tmp = query(index << | ,l,r,val);
else
{
tmp = query(index << ,l,mid,val);
if (tmp != -) return tmp;
return query(index << | ,mid + ,r,val);
}
return tmp;
}
inline ll query_sum(int index,int l,int r)
{
int mid;
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].sum;
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query_sum(index << ,l,r);
else if (mid + <= l) return query_sum(index << | ,l,r);
else return query_sum(index << ,l,mid) + query_sum(index << | ,mid + ,r);
}
int main()
{ read(n); read(q);
for (i = ; i <= n; i++) read(value[i]);
build(,,n);
while (q--)
{
read(x); read(y);
value[x] = y;
modify(,x,y);
if (value[] == )
{
printf("1\n");
continue;
}
cur = pre = tmp = ; ans = -;
while (cur < n)
{
tmp = query(,cur + ,n,pre);
if (tmp == -) break;
cur = tmp;
pre = query_sum(,,tmp);
if (pre - value[cur] == value[cur])
{
ans = tmp;
break;
}
}
printf("%d\n",ans);
} return ;
}

[Codeforces 489E] Nastya and King-Shamans的更多相关文章

  1. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

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

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

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

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

  4. Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]

    题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...

  5. Codeforces 1136C - Nastya Is Transposing Matrices

    题目链接:https://codeforces.com/problemset/problem/1136/C 题意: 给出 $n \times m$ 的矩阵 $A,B$,你可以对其中任意某个 $k \t ...

  6. Codeforces Gym 100851 K King's Inspection ( 哈密顿回路 && 模拟 )

    题目链接 题意 : 给出 N 个点(最多 1e6 )和 M 条边 (最多 N + 20 条 )要你输出一条从 1 开始回到 1 的哈密顿回路路径,不存在则输出 " There is no r ...

  7. CodeForces 992C Nastya and a Wardrobe(规律、快速幂)

    http://codeforces.com/problemset/problem/992/C 题意: 给你两个数x,k,k代表有k+1个月,x每个月可以增长一倍,增长后的下一个月开始时x有50%几率减 ...

  8. CodeForces 992B Nastya Studies Informatics + Hankson的趣味题(gcd、lcm)

    http://codeforces.com/problemset/problem/992/B  题意: 给你区间[l,r]和x,y 问你区间中有多少个数对 (a,b) 使得 gcd(a,b)=x lc ...

  9. CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)

    Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...

随机推荐

  1. js统计图表插件 Echarts

    Echarts 用于制作数据统计图表,一个纯 Javascript 的图表库,快捷简便的生成统计图表. 官网:https://www.echartsjs.com/ 效果 html <!DOCTY ...

  2. c# 常用 Common

    /// <summary> /// md5加密字符串 /// </summary> /// <param name="message">< ...

  3. 用 Systemtap 统计 TCP 连接

    转自: https://mp.weixin.qq.com/s?__biz=MzIxMjAzMDA1MQ==&mid=2648946009&idx=1&sn=3a0be2fe4f ...

  4. X shell 6下载安装和简单使用

    ①前言:昨天已经上线了的智能家居项目出现了一个BUG,需要重新写个html发布到服务器上,由于公司大佬都在忙别的项目,时间比较紧张,这种小事就落到了我这个小喽啰身上.其实,写个html我还是可以接受的 ...

  5. 杂文Python

    2.文件操作 文件操作的过程:打开文件获得句柄——>操作文件行(遍历等)——>关闭文件 打开文件获得句柄 比较low的方法: f = open("file_path", ...

  6. 简述Centos系统启动流程

    1. Centos5 POST开机自检 运行CMOS中的BIOS程序,加载第一个启动磁盘的Bootloader 由Bootloader读取kernel 通过挂载临时根目录initramfs加载核心模块 ...

  7. flex多列布局遇到的问题,和解决方案

    flex布局无疑是简单.易用的,他让我我们的布局更加简单和快速,但是在使用flex进行多列布局的时候,我相信很多人会遇到下面的情况: 这种情况是因为我们使用了justify-content: spac ...

  8. 洛谷 2922 BZOJ 1590 [USACO08DEC]秘密消息Secret Message

    [题意概述] 给出n个01串组成的字典和m个询问,每次询问某个01串和多少个字典中的串有相同的前缀.(前缀长度是两串中较小的部分) [题解] 直接上Trie树即可.树上每个节点记录两个信息:这个节点有 ...

  9. Eclipse中使用JRebel实现项目热部署(Maven插件版)

    JRebel实现项目热部署(Maven插件版) 热部署,就是在应用运行过程中不进行重启,可直接进行软件升级. 在开发过程中,热部署就是在项目运行过程中变更代码,无需重启服务器即可使代码生效. tomc ...

  10. 【13】AngularJS 模块

    AngularJS 模块 模块定义了一个应用程序.(魔芋:也就是说一个ng-app代表一个应用程序,也就是一个模块,module) 模块是应用程序中不同部分的容器. 模块是应用控制器的容器. 控制器通 ...