[题目链接]

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. 梦想CAD控件 2019.01.20更新

    下载地址:http://www.mxdraw.com/ndetail_10120.html1. 修改CAD不等比例块保存问题2. 修改CAD捕捉时,Z值对捕捉不准的影响3. 修改图片对象选择后,自动跑 ...

  2. jQuery鼠标划入划出

    今天来简单的谈谈jQuery的一个划入划出的方法,.首先划入划出能想到的东西有哪些呢,. 1:hover 2:mouseenter/mouseleave 3:mouseover/mouseout. 一 ...

  3. 微服务网关从零搭建——(三)Ocelot网关 + identity4

    增加验证服务 1.创建名为AuthService 的core 空项目 2.修改startup文件 using System; using System.Collections.Generic; usi ...

  4. idea之查看类的上下级继承关系

  5. Getting start with dbus in systemd (01) - Interface, method, path

    Getting start with dbus in systemd (01) 基本概念 几个概念 dbus name: connetion: 如下,第一行,看到的就是 "dbus name ...

  6. 运行jar包的命令

    windows下使用java -jar xxx.jar运行,linux下使用nohup java -jar xxx.jar & 如果想停止jar运行,ps -ef查看进程(进程多的话也可以加上 ...

  7. [POJ1155]TELE(树形背包dp)

    看到这道题的第一眼我把题目看成了TLE 哦那不是重点 这道题是树形背包dp的经典例题 题目描述(大概的): 给你一棵树,每条边有一个cost,每个叶节点有一个earn 要求在earn的和大于等于cos ...

  8. springcloud(十四):搭建Zuul微服务网关

    springcloud(十四):搭建Zuul微服务网关 1. 2. 3. 4.

  9. idea 快捷键设置

    通过 点击放大镜然后按当前需要修改的快捷键找到需要修改的快捷键,更改成希望的快捷键

  10. 转载 - Struts2 拦截器详细配置过程

    出处:http://www.blogjava.net/zzzlyr/archive/2009/10/12/297998.html Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Inter ...