[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 ...
随机推荐
- DWG转PDF
DWG转PDF DWG转换PDF有两种方法,一种是利用PDF打印机,一种是利用专业软件: 利用PDF打印机最直接,但是不能批量打印,下面讲一下利用专业软件如何进行批量转换,在这里以梦想CAD软件(Mx ...
- 出生年 (15 分) C解法
出生年 以上是新浪微博中一奇葩贴:"我出生于1988年,直到25岁才遇到4个数字都不相同的年份."也就是说,直到2013年才达到"4个数字都不相同"的要求.本题 ...
- 右键快捷打开Git Bash here失败
右键快捷打开Git Bash here失败,提示: Error: Could not fork child process: Resource temporarily unavailable (-1) ...
- Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)
有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...
- MySQL数据导入sql文件过程中出错
错误类型: ERROR 1231 (42000): Variable 'time_zone' can't be set to the value of 'NULL' ERROR 1231 (42000 ...
- 使用 MyBatis 对表执行 CRUD 操作
说明: 1.CRUD: C -- create R -- read U -- update D -- delete 2.Mybatis 的 SQL 核心配置文件中 SQL 语句的参数的传 ...
- 如何防止XshellPortable、putty、SecureCRT等断网造成Linux命令中断
在使用XshellPortable.putty.SecureCRT等工具远程连接Linux系统时,如果我们执行了一大堆命令,在命令尚未执行完毕,客户端突然断网或者XshellPortable.putt ...
- python正则表达式提取字符串
用python正则表达式提取字符串 在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣 ...
- FJoi2017 1月20日模拟赛 交错和(等差数列+rmq)
[题目描述] 无限循环数字串S由长度为n的循环节s构成.设s为12345(n=5),则数字串S为123451234512345… 设Si为S的第i位数字,在上面的例子中,S1=1,S2=2,S6=1. ...
- Leetcode 95.不同的二叉搜索树II
不同的二叉搜索树2 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null ...