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

Solution in C++11 (judge verdict: Accepted)


#include <vector>
using namespace std;


//Base class for Segment Trees
template <class T>
class segtree_base
{
protected:
	size_t width;
	vector<T> vec;
	T def;
	virtual T query_raw(int l_, int r_, int a_, int b_, int k_) = 0;
public:
	segtree_base(size_t size_, const T& def_ = T()) : def(def_)
	{
		width = 1;
		while (width < size_)width *= 2;
		vec.resize(width * 2 - 1, def);
	}
	T query(int left, int right)
	{
		return query_raw(left, right, 0, width, 0);
	}
	virtual ~segtree_base() {}
};

#include <algorithm>
//Range Minimum Tree
template <class T, class Comp>
class rmq_tree : public segtree_base<T>
{
public:
	rmq_tree(size_t size_) : segtree_base<T>(size_, Comp::defval)
	{}
	void update(int index, T val)
	{
		auto &vec = this->vec;
		index += (this->width - 1);
		vec[index] = val;
		while (index > 0)
		{
			index = (index - 1) / 2;
			vec[index] = min(vec[index * 2 + 1], vec[index * 2 + 2],Comp());
		}
	}
private:
	T query_raw(int l_, int r_, int a_, int b_, int k_)override
	{
		const auto &vec = this->vec;
		if (l_ <= a_ && b_ <= r_)
		{
			return vec[k_];
		}
		if (b_ <= l_ || r_ <= a_)
		{
			return this->def;
		}
		int m = (a_ + b_) / 2;
		return min(
			query_raw(l_, r_, a_, m, k_ + k_ + 1),
			query_raw(l_, r_, m, b_, k_ + k_ + 2),Comp());
	}
};


#include <algorithm>
//Range Sum + Range Add Tree
template <class T>
class rsaq_tree : public segtree_base<T>
{
protected:
	vector<T>lazy;
public:
	rsaq_tree(size_t size_, const T& def = 0) :segtree_base<T>(size_, def)
	{
		lazy.resize(this->vec.size());
	}
	void init(const vector<int>&base)
	{
		for (int i = 0; i < this->width; i++) this->vec[i + this->width - 1] = base[i];
		for (int i = this->width - 2; i >= 0; i--) this->vec[i] = this->vec[i * 2 + 1] + this->vec[i * 2 + 2];
	}
	void update(int left, int right, int val)
	{
		update_raw(left, right, val, 0, this->width, 0);
	}
private:
	void eval(int k_, int a_, int b_)
	{
		if (lazy[k_] != 0) {
			this->vec[k_] += lazy[k_];
			if (b_ - a_ > 1) {
				lazy[2 * k_ + 1] += lazy[k_] / 2;
				lazy[2 * k_ + 2] += lazy[k_] / 2;
			}
			lazy[k_] = 0;
		}
	}
	void update_raw(int l_, int r_, int x_, int a_, int b_, int k_)
	{
		eval(k_, a_, b_);
		if (r_ <= a_ || b_ <= l_) return;
		if (l_ <= a_ && b_ <= r_) {
			lazy[k_] += (b_ - a_) * x_;
			eval(k_, a_, b_);
		}
		else {
			update_raw(l_, r_, x_, a_, (a_ + b_) / 2, 2 * k_ + 1);
			update_raw(l_, r_, x_, (a_ + b_) / 2, b_, 2 * k_ + 2);
			this->vec[k_] = this->vec[2 * k_ + 1] + this->vec[2 * k_ + 2];
		}
	}
	T query_raw(int l_, int r_, int a_, int b_, int k_)override
	{
		eval(k_, a_, b_);
		if (r_ <= a_ || b_ <= l_) return this->def;
		if (l_ <= a_ && b_ <= r_) return this->vec[k_];
		T vl = query_raw(l_, r_, a_, (a_ + b_) / 2, 2 * k_ + 1);
		T vr = query_raw(l_, r_, (a_ + b_) / 2, b_, 2 * k_ + 2);
		return vl + vr;
	}
};


#include <functional>

//Functors for Segment Trees
template <class T>
struct my_less : std::less<T> 
{
	static constexpr T defval = std::numeric_limits<T>::max();
};
template <class T>
struct my_greater : std::greater<T>
{
	static constexpr T defval = std::numeric_limits<T>::min();
};

#include <iostream>
#include <climits>

int main()
{
	int n, q;
	cin >> n >> q;
	rmq_tree<int, my_less<int>>rmq(n);
	for (int i = 0; i < q; ++i)
	{
		int com, x, y;
		cin >> com >> x >> y;
		if (com)
		{
			cout << rmq.query(x, y + 1) << endl;
		}
		else
		{
			rmq.update(x, y);
		}
	}
	return 0;
}