Range Minimum Query (RMQ)

Write a program to manipulate a sequence A = {a0, a1, ..., an-1} with the following operations:
- find(s, t): report the minimum 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 2^31-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 < 2^31-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

# Buggy version:
import math

def update(index,n):
    targ[index] = n
    index = (index - 1) // 2
    while True:
        targ[index] = min(targ[index * 2],targ[index * 2 + 1])
        if index <= 0:
            break
        index = (index - 1) // 2

def find(i,e,node,inode,enode):
    #print(i,e,node,inode,enode)
    if inode > e or enode < i:
        return float("inf")
    elif (i <= inode and enode <= e) or inode==enode:
        return targ[node]

    else:
        irange = find(i,e,2*node + 1,inode,(inode+enode)// 2 )
        erange = find(i,e,2*node + 2,(inode+enode)//2 + 1,enode)
        return min(irange,erange)

num,query = (int(n) for n in input().split(' '))
initial = 2147483647
ln = 2 ** math.ceil(math.log2(num)) - 1
global targ
targ = [initial] * (ln + num)
length = num - 1

for q in range(query):
    #print(targ)
    temp = [int(n) for n in input().split(' ') ]
    if temp[0] == 0:
        update(temp[1] + ln,temp[2])
    else:
        print(find(temp[1],temp[2],0,0,length))

# Fixed version:
import math

def update(index,n):
    targ[index] = n
    index = (index - 1) // 2
    while True:
        targ[index] = min(targ[index * 2 + 1],targ[index * 2 + 2])
        if index <= 0:
            break
        index = (index - 1) // 2

def find(i,e,node,inode,enode):
    #print(i,e,node,inode,enode)
    if inode > e or enode < i:
        return float("inf")
    elif (i <= inode and enode <= e) or inode==enode:
        return targ[node]

    else:
        irange = find(i,e,2*node + 1,inode,(inode+enode)// 2 )
        erange = find(i,e,2*node + 2,(inode+enode)//2 + 1,enode)
        return min(irange,erange)

num,query = (int(n) for n in input().split(' '))
initial = 2147483647
ln = 2 ** math.ceil(math.log2(num)) - 1
if num == 1:
    tail = 2
else:
    tail = 2 ** math.ceil(math.log2(num)) - num
global targ
targ = [initial] * (ln + num + tail)

for q in range(query):
    #print(targ)
    temp = [int(n) for n in input().split(' ') ]
    if temp[0] == 0:
        update(temp[1] + ln,temp[2])
    else:
        print(find(temp[1],temp[2],0,0,ln))

# Buggy version:
class RMQ:
    def __init__(self, length, ini_num = float("inf")):
        self.length = 1
        while self.length < length:
            self.length <<= 1
        self.segtree = [ini_num] * (2 * self.length - 1)

    def update(self, index, n):
        leaf_index = index + self.length - 1
        self.segtree[leaf_index] = n
        while leaf_index > 0:
            leaf_index = (leaf_index - 1) // 2
            self.segtree[leaf_index] = min(self.segtree[leaf_index * 2 + 1], self.segtree[leaf_index * 2 + 2])

    def find(self, a, b, index, l, r):
        if a == b:
            return self.segtree[self.length - 1 + a] 
        elif a <= l <= r <= b:
            return self.segtree[index]
        elif b <= l or r <= a:
            return float("inf")
        else:
            return min(self.find(a, b, index * 2 + 1, l, (l + r) // 2), self.find(a, b, index * 2 + 2, (l + r) // 2, r))

v_num, query = (int(n) for n in input().split(" "))
rmq = RMQ(v_num, 2147483647)
for _ in range(query):
    com, i, n = (int(n) for n in input().split(" ") )
    if com == 0:
        rmq.update(i, n)
    else:
        print(rmq.find(i, n, 0, 0, rmq.length - 1))

# Fixed version:
class RMQ:
    def __init__(self, length, ini_num = float("inf")):
        self.length = 1
        self.ini_num = ini_num
        while self.length < length:
            self.length <<= 1
        self.segtree = [ini_num] * (2 * self.length - 1)

    def update(self, index, n):
        leaf_index = index + self.length - 1
        self.segtree[leaf_index] = n
        while leaf_index > 0:
            leaf_index = (leaf_index - 1) // 2
            self.segtree[leaf_index] = min(self.segtree[leaf_index * 2 + 1], self.segtree[leaf_index * 2 + 2])

    def find(self, a, b, index, l, r):
        if a <= l <= r <= b:
            return self.segtree[index]
        elif b < l or r < a:
            return self.ini_num
        else:
            return min(self.find(a, b, index * 2 + 1, l, (l + r) // 2), self.find(a, b, index * 2 + 2, (l + r) // 2 + 1, r))

v_num, query = map(int, input().split(" "))
rmq = RMQ(v_num, 2147483647)
for _ in [0] * query:
    com, i, n = map(int, input().split(" "))
    if com == 0:
        rmq.update(i, n)
    else:
        print(rmq.find(i, n, 0, 0, rmq.length - 1))

# Buggy version:
import sys;

def init(n_):
    length = 1;
    while(length < n_):
        length*=2
    element=[0]*(2*length-1)
    for i in range(2*length-1):
        element[i] = initial
    return element

def update(k,a,element):
    k+=n
    element[k]=a
    while(k > 0):
        k = (int)((k-1)/2)
        element[k] = min(element[k*2+1],element[k*2+2])

def findmin(a, b):
    return findquery(a,b+1,0,0,n)

def findquery(a, b, k, l, r):
    if(r <= a or b <= l):
        return initial
    if(a <= l and r <= b):
        return element[k]

    vl = findquery(a,b,k*2+1,l,(int)((l+r)/2+0.5))
    vr = findquery(a,b,k*2+2,(int)((l+r)/2),r)
    return min(vl,vr)

n,q = map(int, input().split());

initial = pow(2,31)-1
element = init(n)

for i in range(q):
    query,key,value = map(int, input().split());
    if(query == 0):
        update(key,value,element)
    else :
        print(findmin(key,value))


# Fixed version:
import sys;

def init(n_):
    length = 1;
    while(length < n_):
        length*=2
    element=[0]*(2*length-1)
    for i in range(2*length-1):
        element[i] = initial
    return element,length

def update(k,a,element):
    k+=n-1
    element[k]=a
    while(k > 0):
        k = (int)((k-1)/2)
        element[k] = min(element[k*2+1],element[k*2+2])

def findmin(a, b):
    return findquery(a,b+1,0,0,n)

def findquery(a, b, k, l, r):
    if(r <= a or b <= l):
        return initial
    if(a <= l and r <= b):
        return element[k]

    vl = findquery(a,b,k*2+1,l,(int)((l+r)/2+0.5))
    vr = findquery(a,b,k*2+2,(int)((l+r)/2),r)
    return min(vl,vr)

n,q = map(int, input().split());

initial = pow(2,31)-1
element,n = init(n)

for i in range(q):
    query,key,value = map(int, input().split());
    if(query == 0):
        update(key,value,element)
    else :
        print(findmin(key,value))

# Buggy version:
import sys
sys.setrecursionlimit(10000)


def initRMQ(n, D):
    n_ = 1

    while n_ < n:
        n_ *= 2

    for i in range(2*n_ - 1):
        D.append(2147483647)

    return n_


def update(k, a):
    k += n_ - 1
    D[k] = a
    while k > 0:
        k = int((k - 1) / 2)
        D[k] = min(D[k*2 + 1], D[k*2 + 2])


def findMin(a, b):
    return query(a, b + 1, 0, 0, n)


def query(a, b, k, l, r):
    #print('q', a, b, k, l, r)
    if r <= a or b <= l:
        return 2147483647

    if a <= l and r <= b:
        return D[k]

    vl = query(a, b, k*2 + 1, l, int((l + r)/2))
    vr = query(a, b, k*2 + 2, int((l + r)/2), r)

    return min(vl, vr)


if __name__ == '__main__':
    n, q = map(int, input().split())

    D = []
    n_ = initRMQ(n, D)

    for _ in range(q):
        #print(D)
        c, x, y = map(int, input().split())

        if c == 0:
            update(x, y)

        else :
            print(findMin(x, y))



# Fixed version:
import sys
sys.setrecursionlimit(10000)


def initRMQ(n, D):
    n_ = 1

    while n_ < n:
        n_ *= 2

    for i in range(2*n_ - 1):
        D.append(2147483647)

    return n_


def update(k, a):
    k += n_ - 1
    D[k] = a
    while k > 0:
        k = int((k - 1) / 2)
        D[k] = min(D[k*2 + 1], D[k*2 + 2])


def findMin(a, b):
    return query(a, b + 1, 0, 0, n_)


def query(a, b, k, l, r):
    #print('q', a, b, k, l, r)
    if r <= a or b <= l:
        return 2147483647

    if a <= l and r <= b:
        return D[k]

    vl = query(a, b, k*2 + 1, l, int((l + r)/2))
    vr = query(a, b, k*2 + 2, int((l + r)/2), r)

    return min(vl, vr)


if __name__ == '__main__':
    n, q = map(int, input().split())

    D = []
    n_ = initRMQ(n, D)

    for _ in range(q):
        #print(D)
        c, x, y = map(int, input().split())

        if c == 0:
            update(x, y)

        else :
            print(findMin(x, y))

# Buggy version:
import bisect
import collections
import heapq
import itertools
import operator
import sys


def main():
    int_max = 2 ** 31 - 1
    n, q = map(int, sys.stdin.readline().split())
    dat = [int_max] * 131072 * 2

    def update(i, x):
        i += n - 1
        dat[i] = x
        while i > 0:
            i = (i - 1) // 2
            dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2])

    def query(a, b, k, l, r):
        if r <= a or b <= l:
            return int_max
        if a <= l and r <= b:
            return dat[k]
        else:
            vl = query(a, b, k * 2 + 1, l, (l + r) // 2)
            vr = query(a, b, k * 2 + 2, (l + r) // 2, r)
            return min(vl, vr)

    for _ in range(q):
        com, x, y = map(int, sys.stdin.readline().split())
        if com == 0:
            update(x, y)
        elif com == 1:
            print(query(x, y + 1, 0, 0, n))


if __name__ == "__main__":
    main()



# Fixed version:
import bisect
import collections
import heapq
import itertools
import operator
import sys


def main():
    int_max = 2 ** 31 - 1
    n, q = map(int, sys.stdin.readline().split())
    n = 1 << (n - 1).bit_length()
    dat = [int_max] * 131072 * 2

    def update(i, x):
        i += n - 1
        dat[i] = x
        while i > 0:
            i = (i - 1) // 2
            dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2])

    def query(a, b, k, l, r):
        if r <= a or b <= l:
            return int_max
        if a <= l and r <= b:
            return dat[k]
        else:
            vl = query(a, b, k * 2 + 1, l, (l + r) // 2)
            vr = query(a, b, k * 2 + 2, (l + r) // 2, r)
            return min(vl, vr)

    for _ in range(q):
        com, x, y = map(int, sys.stdin.readline().split())
        if com == 0:
            update(x, y)
        elif com == 1:
            print(query(x, y + 1, 0, 0, n))


if __name__ == "__main__":
    main()