[Codeforces 489E] Nastya and King-Shamans
[题目链接]
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的更多相关文章
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)
题目链接: http://codeforces.com/contest/1136/problem/E 题意: 初始有a数组和k数组 有两种操作,一,求l到r的区间和,二,$a_i\pm x$ 并且会有 ...
- Codeforces 1136E - Nastya Hasn't Written a Legend - [线段树+二分]
题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$, ...
- Codeforces 1136D - Nastya Is Buying Lunch - [贪心+链表+map]
题目链接:https://codeforces.com/problemset/problem/1136/D 题意: 给出 $1 \sim n$ 的某个排列 $p$,再给出若干 $(x,y)$ 表示当序 ...
- Codeforces 1136C - Nastya Is Transposing Matrices
题目链接:https://codeforces.com/problemset/problem/1136/C 题意: 给出 $n \times m$ 的矩阵 $A,B$,你可以对其中任意某个 $k \t ...
- Codeforces Gym 100851 K King's Inspection ( 哈密顿回路 && 模拟 )
题目链接 题意 : 给出 N 个点(最多 1e6 )和 M 条边 (最多 N + 20 条 )要你输出一条从 1 开始回到 1 的哈密顿回路路径,不存在则输出 " There is no r ...
- CodeForces 992C Nastya and a Wardrobe(规律、快速幂)
http://codeforces.com/problemset/problem/992/C 题意: 给你两个数x,k,k代表有k+1个月,x每个月可以增长一倍,增长后的下一个月开始时x有50%几率减 ...
- 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 ...
- 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 ...
随机推荐
- nginx+tomcat+memcache
tomcat1和tomcat2都需要安装以下配置[root@tomcat-1 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel[ro ...
- ios xmpp 发送语音图片解决方案
ios xmpp 发送语音,图片解决方案,有需要的朋友可以参考下. 目前做IM多是用的xmpp. 因为项目需求需要实现语音和图片的发送. 发送语音图片有三种方法. 1,xmpp smack.文件传输方 ...
- ie7下设置z-index无效如何解决?
ie7下z-index无效的问题之前做练习的时候遇到过,百度解决掉之后就丢脑后了.今天项目中又发现这个bug,无奈又去百度,这次还是记下来,节省了百度的时间还能小装一把... 需求是这样的: 页面中的 ...
- 判断机型是否为iphoneX
判断机型是否为iphoneX isIPhoneX() { var u = navigator.userAgent; var isIOS ...
- POJ3616 Milking Time【dp】
Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...
- 2.Linux文件IO编程
2.1Linux文件IO概述 2.1.0POSIX规范 POSIX:(Portable Operating System Interface)可移植操作系统接口规范. 由IEEE制定,是为了提高UNI ...
- Java基础学习总结(75)——Java反射机制及应用场景
什么是Java反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能称为 ...
- spring mvc参数校验
一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...
- Ubuntu18.04卸载lnmp
1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...
- Spring mvc+Easyui遇到的几个问题
简单的一个数据表的增删查改的总体界面的展示效果例如以下图: 1.datagrid数据载入问题 datagrid通过url请求后台数据,总记录数和数据行的属性是固定死的.数据行是rows,总记录数为to ...