AOJ DSL_2_A Range Minimum Query (RMQ)
Range Minimum Query (RMQ)
Write a program which manipulates a sequence A = {a0,a1,...,an−1} with the following operations:
- find(s,t): report the mimimum element in as,as+1,...,at.
- update(i,x): change ai to x.
Note that the initial values of ai (i=0,1,...,n−1) are 231-1.
Input
n q
com0 x0 y0
com1 x1 y1
...
comq−1 xq−1 yq−1
In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes update(xi,yi) and '1' denotes find(xi,yi).
Output
For each find operation, print the minimum element.
Constraints
- 1≤n≤100000
- 1≤q≤100000
- If comi is 0, then 0≤xi<n, 0≤yi<231−1.
- If comi is 1, then 0≤xi<n, 0≤yi<n.
Sample Input 1
3 5
0 0 1
0 1 2
0 2 3
1 0 2
1 1 2
Sample Output 1
1
2
Sample Input 2
1 3
1 0 0
0 0 5
1 0 0
Sample Output 2
2147483647
5
带修改的区间最小值查询,线段树模板题。压了压常数,又打榜了。
#include <cstdio>
inline int min(const int &a, const int &b) {
return a < b ? a : b;
}
#define siz 10000000
char buf[siz], *bit = buf;
inline int nextInt(void) {
register int ret = ;
register int neg = false;
for (; *bit < ''; ++bit)
if (*bit == '-')neg ^= true;
for (; *bit >= ''; ++bit)
ret = ret * + *bit - '';
return neg ? -ret : ret;
}
#define inf 2147483647
int n, m, mini[];
int find(int t, int l, int r, int x, int y) {
if (x <= l && r <= y)
return mini[t];
int mid = (l + r) >> ;
if (y <= mid)
return find(t << , l, mid, x, y);
if (x > mid)
return find(t << | , mid + , r, x, y);
else
return min(
find(t << , l, mid, x, mid),
find(t << | , mid + , r, mid + , y)
);
}
void update(int t, int l, int r, int x, int y) {
if (l == r)mini[t] = y;
else {
int mid = (l + r) >> ;
if (x <= mid)
update(t << , l, mid, x, y);
else
update(t << | , mid + , r, x, y);
mini[t] = min(mini[t << ], mini[t << | ]);
}
}
signed main(void) {
fread(buf, , siz, stdin);
n = nextInt();
m = nextInt();
for (int i = ; i <= (n << ); ++i)mini[i] = inf;
for (int i = ; i <= m; ++i) {
int c = nextInt();
int x = nextInt();
int y = nextInt();
if (c) // find(x, y)
printf("%d\n", find(, , n, x + , y + ));
else // update(x, y)
update(, , n, x + , y);
}
// system("pause");
}
@Author: YouSiki
AOJ DSL_2_A Range Minimum Query (RMQ)的更多相关文章
- Geeks - Range Minimum Query RMQ范围最小值查询
使用线段树预处理.能够使得查询RMQ时间效率在O(lgn). 线段树是记录某范围内的最小值. 标准的线段树应用. Geeks上仅仅有两道线段树的题目了.并且没有讲到pushUp和pushDown操作. ...
- Range Minimum Query and Lowest Common Ancestor
作者:danielp 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAnc ...
- AOJ DSL_2_E Range Add Query (RAQ)
Range Add Query 数列 A = {a1,a2,...,an} に対し.次の2つの操作を行うプログラムを作成せよ. add(s,t,x): as,as+1,...,at にxを加算する. ...
- AOJ DSL_2_D Range Update Query (RUQ)
Range Update Query 数列 A = {a0,a1 ,...,an−1} に対し.次の2つの操作を行うプログラムを作成せよ. update(s,t,x): as,as+1,...,at ...
- RMQ(Range Minimum Query)问题(转)
问题描述 RMQ问题是求给定区间中的最值问题.对于长度为n的数列A,回答若干查询RMQ(A, i, j).返回数组A中下标在[i,j]里的最小值的下标. 比如数列 5,8,1,3,6,4,9,5,7 ...
- Segment Tree Range Minimum Query.
int rangeMinQuery(int segTree[], int qlow, int qhigh, int low, int high, int pos) { if (qlow <= l ...
- RMQ (Range Minimal Query) 问题 ,稀疏表 ST
RMQ ( 范围最小值查询 ) 问题是一种动态查询问题,它不需要修改元素,但要及时回答出数组 A 在区间 [l, r] 中最小的元素值. RMQ(Range Minimum/Maximum Query ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
随机推荐
- IRIS数据集的分析-数据挖掘和python入门-零门槛
所有内容都在python源码和注释里,可运行! ########################### #说明: # 撰写本文的原因是,笔者在研究博文“http://python.jobbole.co ...
- Linux系统查看系统是32位还是64位方法总结
这篇博客是总结.归纳查看Linux系统是32位还是64位的一些方法,很多内容来自网上网友的博客.本篇只是整理.梳理这方面的知识,方便自己忘记的时候随时查看. 方法1:getconf LONG_BIT ...
- Nagios学习实践系列——产品介绍篇
Nagios介绍 Nagios是一款功能强大.优秀的开源监控系统,它能够让你发现和解决IT架构中存在的问题,避免这些问题影响到关键业务流程. Nagios最早于1999年发布,它在开源社区的影响力是相 ...
- SQL Server里面如何检查没有释放的游标
一直以来对SQL SERVER的游标都不怎么感冒,也很少使用SQL Server里面的游标,前几天有一位网友问如何检查数据库里面没有释放的游标,觉得有点意思,就测试验证了一下,顺便整理于此. 会话1: ...
- linux 安装jdk
1.Linux安装JDK步骤1. 先从网上下载jdk(jdk-1_5_0_02-linux-i586.rpm) ,推荐SUN的官方网站www.sun.com,下载后放在/home目录中,当然其它地方也 ...
- shell中各种括号的作用()、(())、[]、[[]]、{}
一.小括号,圆括号() 1.单小括号 () ①命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有 ...
- [WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]
What is the difference between these 2 bindings: <ControlTemplate TargetType="{x:Type Button ...
- ECharts 初体验
缘起 ECharts,缩写来自Enterprise Charts,商业级数据图表,它最初是为了满足公司商业体系里各种业务系统(如凤巢.广告管家等等)的报表需求.以前这些系统的图表需求我们都是使用fla ...
- HDU2767Proving Equivalences[强连通分量 缩点]
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Java中单例
Java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍两种:懒汉式单例.饿汉式单例 单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己创建自己的唯一实例. 3. ...