### #1077 : RMQ问题再临-线段树

#### 输出

• 样例输入

• ```10
3655 5246 8991 5933 7474 7603 6098 6654 2414 884
6
0 4 9
0 2 10
1 4 7009
0 5 6
1 3 7949
1 3 1227```
• 样例输出

• ```2414
884
7474```

AC代码：

#include<iostream>

#include<cstdlib>

#include<algorithm>

#include<vector>

#include<list>

#include<iterator>

#include<string>

#include<stack>

using namespace std;

#define INF 0x3fffffff

const int MAX = 100000100;

struct NODE {

int value, left, right;

}node[MAX];

void BuildTree(int n, int left, int right) {

node[n].left = left;

node[n].right = right;

if (left == right)

{

scanf("%d",&node[n].value);

return;

}

int mid = (left + right) >> 1;

BuildTree(n << 1, left, mid);

BuildTree((n << 1) + 1, mid + 1, right);

node[n].value = min(node[n << 1].value, node[(n << 1) + 1].value);

}

int FindTree(int n, int begin, int end) {

int p1 = INF, p2 = INF;

if (node[n].left >= begin&&node[n].right <= end)

return node[n].value;

if (begin <= node[n << 1].right)

p1 = FindTree(n << 1, begin, end);

if (end >= node[(n << 1) + 1].left)

p2 = FindTree((n << 1) + 1, begin, end);

return min(p1, p2);

}

void UpdateTree(int n, int ind, int val) {

if (node[n].left == node[n].right)

{

node[n].value = val;

}

else

{

if (ind <= node[n << 1].right)

UpdateTree(n << 1, ind, val);

if (ind >= node[(n << 1) + 1].left)

UpdateTree((n << 1) + 1, ind, val);

node[n].value = min(node[n << 1].value, node[(n << 1) + 1].value);

}

}

int main()

{

int N;

int m;

int s, l, r;

while (~scanf("%d",&N))

{

BuildTree(1, 0, N - 1);

scanf("%d",&m);

for (int i = 0; i < m; i++)

{

scanf("%d %d %d", &s, &l, &r);

if (s == 0)

{

printf("%d\n",FindTree(1, l - 1, r - 1));

}

if (s == 1)

{

UpdateTree(1, l - 1, r);

}

}

}

return 0;

}

```scanf是格式化输入，printf是格式化输出。
cin是输入流，cout是输出流。效率稍低，但书写简便。```