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<n0≤yi<231−1.
  • If comi is 1, then 0≤xi<n0≤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)的更多相关文章

  1. Geeks - Range Minimum Query RMQ范围最小值查询

    使用线段树预处理.能够使得查询RMQ时间效率在O(lgn). 线段树是记录某范围内的最小值. 标准的线段树应用. Geeks上仅仅有两道线段树的题目了.并且没有讲到pushUp和pushDown操作. ...

  2. Range Minimum Query and Lowest Common Ancestor

    作者:danielp 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAnc ...

  3. 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を加算する. ...

  4. 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  ...

  5. RMQ(Range Minimum Query)问题(转)

    问题描述 RMQ问题是求给定区间中的最值问题.对于长度为n的数列A,回答若干查询RMQ(A, i, j).返回数组A中下标在[i,j]里的最小值的下标. 比如数列 5,8,1,3,6,4,9,5,7 ...

  6. Segment Tree Range Minimum Query.

    int rangeMinQuery(int segTree[], int qlow, int qhigh, int low, int high, int pos) { if (qlow <= l ...

  7. RMQ (Range Minimal Query) 问题 ,稀疏表 ST

    RMQ ( 范围最小值查询 ) 问题是一种动态查询问题,它不需要修改元素,但要及时回答出数组 A 在区间 [l, r] 中最小的元素值. RMQ(Range Minimum/Maximum Query ...

  8. [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 ...

  9. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

随机推荐

  1. C++算法实源码分析

    includes: // TEMPLATE FUNCTION includes WITH PRED template<class _InIt1, class _InIt2, class _Pr& ...

  2. crontab -e 每天定时备份mysql

    contab -e 00 03 * * * mysqldump -u juandx --password=wenbin -d 'juandx$blog' -h host > /home/juan ...

  3. sql monitor生成不了报告& FFS hint不生效两个问题思考

    事情的发生就是这么偶然,一步步的深入才能汲取到更深入的知识~~ -------------------START------------------------------------------- ...

  4. Linux系统中 安装Vmware Toolst工具

    前提: 安装虚拟机.可以参考:在Windows上安装虚拟机详细图文教程 安装Linux.可以参考:在VMware Workstation里的Linux操作系统的安装——红旗桌面7.0 本文作者:sou ...

  5. 廖雪峰python教程的第一个疑问

    函数的参数一节中提到: def add_end(L = []); L.append('END') return L 正常调用add_end时(也就是有参数传入时): >>> add_ ...

  6. android 解决ListView点击与滑动事件冲突

    如果你的ListView的Item有滑动功能,但又点击Item跳转到其它activity,这样若是在Adapter里面写点击事件是会导致滑动事件获取不到焦点而失效: 解决方法:不要在adapter里面 ...

  7. 【2016-11-3】【坚持学习】【Day18】【我认识的ORM】

    我学过或者用过的ORM有几种 EF NHibernate DevExpress  xpo (第一家公司用这个,也是很省力的orm,现在已经不记得了.) 今天晚上想找一下有没有轻量级的orm: flue ...

  8. js面向对象与原型

    创建对象 var box = new Object();//创建对象 box.name = 'Lee'; //添加属性 box.age = 100; box.run = function(){ ret ...

  9. [转载]python:open/文件操作

    open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 如:' ...

  10. NYOJ 205

    大数取模算法: 这个又不同于幂取模算法,对于几百万位的数字取模,直接的方法是行不通的.最好利用数论的知识 (a*b)%c = ((a%c)*b)%c ;利用这个公式我们只要从n的一次方开始不断计算,乘 ...