Write a program which manipulates a sequence A={a0,a1,…,an−1} by the following operations: min(b,e): report the minimum element in ab,ab+1,…,ae−1 max(b,e): report the maximum element in ab,ab+1,…,ae−1
输入
The input is given in the following format. n a0 a1,…,an−1 q com1 b1 e1 com2 b2 e2 : comq bq eq In the first line, n (the number of elements in A) is given. In the second line, ai (each element in A) are given. In the third line, the number of queries q is given and each query is given in the following q lines. comi denotes a type of query. 0 and 1 represents min(b,e) and max(b,e) respectively
输出
For each query, print the minimum element or the maximum element in a line.
样例输入
7
8 3 7 1 9 1 4
3
0 0 3
0 1 5
1 0 7
样例输出
3
1
9
题解(代码流程)
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, q;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++)cin >> A[i];
cin >> q;
while (q--) {
int op, left, right;
cin >> op >> left >> right;
if (op == 0) {
cout << *min_element(A.begin() + left, A.begin() + right) << endl;
} else if (op == 1) {
cout << *max_element(A.begin() + left, A.begin() + right) << endl;
}
}
return 0;
}