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

带修改的区间最小值查询,线段树模板题。压了压常数,又打榜了。

 1 #include <cstdio>
 2
 3 inline int min(const int &a, const int &b) {
 4     return a < b ? a : b;
 5 }
 6
 7 #define siz 10000000
 8
 9 char buf[siz], *bit = buf;
10
11 inline int nextInt(void) {
12     register int ret = 0;
13     register int neg = false;
14
15     for (; *bit < ‘0‘; ++bit)
16         if (*bit == ‘-‘)neg ^= true;
17
18     for (; *bit >= ‘0‘; ++bit)
19         ret = ret * 10 + *bit - ‘0‘;
20
21     return neg ? -ret : ret;
22 }
23
24 #define inf 2147483647
25
26 int n, m, mini[400005];
27
28 int find(int t, int l, int r, int x, int y) {
29     if (x <= l && r <= y)
30         return mini[t];
31     int mid = (l + r) >> 1;
32     if (y <= mid)
33         return find(t << 1, l, mid, x, y);
34     if (x > mid)
35         return find(t << 1 | 1, mid + 1, r, x, y);
36     else
37         return min(
38             find(t << 1, l, mid, x, mid),
39             find(t << 1 | 1, mid + 1, r, mid + 1, y)
40         );
41 }
42
43 void update(int t, int l, int r, int x, int y) {
44     if (l == r)mini[t] = y;
45     else {
46         int mid = (l + r) >> 1;
47         if (x <= mid)
48             update(t << 1, l, mid, x, y);
49         else
50             update(t << 1 | 1, mid + 1, r, x, y);
51         mini[t] = min(mini[t << 1], mini[t << 1 | 1]);
52     }
53 }
54
55 signed main(void) {
56     fread(buf, 1, siz, stdin);
57
58     n = nextInt();
59     m = nextInt();
60
61     for (int i = 0; i <= (n << 2); ++i)mini[i] = inf;
62
63     for (int i = 1; i <= m; ++i) {
64         int c = nextInt();
65         int x = nextInt();
66         int y = nextInt();
67         if (c)    // find(x, y)
68             printf("%d\n", find(1, 1, n, x + 1, y + 1));
69         else    // update(x, y)
70             update(1, 1, n, x + 1, y);
71     }
72
73 //    system("pause");
74 }

@Author: YouSiki

时间: 2024-12-29 23:26:10

AOJ DSL_2_A Range Minimum Query (RMQ)的相关文章

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

使用线段树预处理,可以使得查询RMQ时间效率在O(lgn). 线段树是记录某范围内的最小值. 标准的线段树应用. Geeks上只有两道线段树的题目了,而且没有讲到pushUp和pushDown操作,只是线段树的入门了. 参考:http://www.geeksforgeeks.org/segment-tree-set-1-range-minimum-query/ 我修改了一下他的程序,使用pushUp操作,其实也是很简单的一个小函数.而且手动计算了下,觉得他的动态分配内存,计算需要的树的大小,这样

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 をxに変更する. find(i): ai の値を出力する. ただし.ai (i=0,1,...,n−1)は.231-1で初期化されているものとする. 入力 n q query1 query2 : queryq 1行目にAの要素数n, クエリの数qが与えられる.続くq行にi 番目のクエリ queryi

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を加算する. get(i): aiの値を出力する. ただし.ai (i=1,2,...,n)は.0 で初期化されているものとする. 入力 n q query1 query2 : queryq 1行目にAの要素数n, クエリの数qが与えられる.続くq行に i 番目のクエリ queryi が与えられる.queryi 

范围最小值问题(Range Minimum Query,RMQ)

问题描述 给定一个n个元素的序列{A1,A2,--,An},在要求的区间Query(L,R)内找到最小值:min{AL,AL+1,--,AR}.hiho16 算法描述 在这里介绍最常用的Tarjan的Sparse-Table算法,它的预处理时间复杂度为O(nlogn),而查询时间只需要O(1).令calc(i,j)表示从i开始的,长度为2j 的一段子序列的最小值,则使用循环的方式计算:calc[i][j] = min(calc[i][j-1],calc[i+2j-1)][j-1]),代码如下:

Segment Tree Range Minimum Query.

int rangeMinQuery(int segTree[], int qlow, int qhigh, int low, int high, int pos) { if (qlow <= low && qhigh >= high) return segTree[pos]; if (qlow > high || qhigh < low) return maxVal; int mid = (low + high) / 2; return min(rangeMinQu

2-D range sum query implementation(2-D segment tree)

Google interview question:一个二维数组,有两个方法,一个是update(x,y),更新一个cell的值,一个是query(x1,y1,x2,y2),查询(x1,y1,x2,y2)矩形内所有元素的和. Senario 1. update调用次数远大于query. Senario 2. query调用次数远大于update. Senario 3. 两种方法调用一样多. 对于senario 1,只需要用一个二维数组储存数据,update相应的cell,时间复杂度O(1),qu

[LeetCode] Range Sum Query 2D - Immutable

Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fact, if you work in computer vision, you may know a name for such an array --- Integral Image. To solve this problem, Stefan has already posted a very e

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

Range Sum Query 2D - Mutable &amp; Immutable

Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1,