[题目链接]

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. 牛客多校Round 5

    Solved:3 rank:195 F. take 官方题解:小 A 在打开第 i 个箱子后会交换手中的钻石和第 i 个箱子中的钻石 当且仅当第 i个箱子的钻石是前 i 个箱子打开后出现的所有钻石里最 ...

  2. [Algorithm] 3. Digit Counts

    Description Count the number of k's between 0 and n. k can be 0 - 9. Example if n = 12, k = 1 in [0, ...

  3. Java对象序列化为什么要使用SerialversionUID

    1.首先谈谈为什么要序列化对象 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通 ...

  4. Linux 安装 Tomcat 详解

    说明:安装的 tomcat 为解压版(即免安装版):apache-tomcat-8.5.15.tar.gz (1)使用 root 用户登录虚拟机,在根目录下的 opt 文件夹新建一个 software ...

  5. python正则表达式提取字符串

    用python正则表达式提取字符串 在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣 ...

  6. 【14】AngularJS 表单

    AngularJS 表单 AngularJS 表单是输入控件的集合. HTML 控件 以下 HTML input 元素被称为 HTML 控件: input 元素 select 元素 button 元素 ...

  7. 洛谷 P3203 BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊

    题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系 ...

  8. 文化之旅 2012年NOIP全国联赛普及组

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超 ...

  9. [bzoj3781]小B的询问_莫队

    小B的询问 bzoj-3781 题目大意:给定一个n个数的序列,m次询问.每次询问一段区间内数的种类的平方和. 注释:$1\le n\,m\le 5\cdot 10^4$. 想法:莫队练习题. 我们考 ...

  10. NOIP2014 提高组合集

    NOIP 2014 提高组 合集 D1 T1 生活大爆炸版石头剪刀布 首先,先将两个人的猜拳序列都变得不小于n.然后逐个模拟.胜败什么的看表就行了. #include <iostream> ...