|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +#define __ ios_base::sync_with_stdio(0); cin.tie(0); |
| 4 | +#define endl '\n' |
| 5 | +#define foreach(it, x) for (__typeof (x).begin() it = (x).begin(); it != (x).end(); ++it) |
| 6 | +#define all(x) x.begin(),x.end() |
| 7 | +#define D(x) cout << #x " = " << (x) << endl; |
| 8 | + |
| 9 | +template <class T> string toStr(const T &x) |
| 10 | +{ stringstream s; s << x; return s.str(); } |
| 11 | + |
| 12 | +template <class T> int toInt(const T &x) |
| 13 | +{ stringstream s; s << x; int r; s >> r; return r; } |
| 14 | + |
| 15 | +int dx[8] = {-1,-1,-1,0,1,1, 1, 0}; |
| 16 | +int dy[8] = {-1, 0, 1,1,1,0,-1,-1}; |
| 17 | + |
| 18 | +const int N = 1000 * 100 + 10; |
| 19 | +vector<int> ans(N * 4, 0); |
| 20 | + |
| 21 | +int query (int x, int y, int id, int l, int r) { |
| 22 | + if (l >= y || x >= r) return INT_MAX; |
| 23 | + if (x <= l && r <= y) return ans[id]; |
| 24 | + |
| 25 | + int mid = (l + r) / 2; |
| 26 | + int a = query (x, y, 2 * id, l, mid); |
| 27 | + int b = query (x, y, 2 * id + 1, mid, r); |
| 28 | + |
| 29 | + return min(a, b); |
| 30 | +} |
| 31 | + |
| 32 | +void init (int id, int l, int r, vector<int> &v) { |
| 33 | + if (r - l < 2) { |
| 34 | + ans[id] = v[l]; |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + int mid = (r + l) / 2; |
| 39 | + init (2 * id, l, mid, v); |
| 40 | + init (2 * id + 1, mid, r, v); |
| 41 | + |
| 42 | + ans[id] = min(ans[2 * id], ans[2 * id + 1]); |
| 43 | +} |
| 44 | + |
| 45 | +int main() { |
| 46 | + int t; |
| 47 | + cin >> t; |
| 48 | + |
| 49 | + int TC = 1; |
| 50 | + while (t --> 0) { |
| 51 | + int n, q; |
| 52 | + scanf("%d %d", &n, &q); |
| 53 | + |
| 54 | + vector<int> v(n); |
| 55 | + for (int i = 0; i < n; ++i) cin >> v[i]; |
| 56 | + |
| 57 | + init (1, 0, n, v); |
| 58 | + |
| 59 | + printf("Case %d:\n", TC ++); |
| 60 | + for (int i = 0; i < q; ++i) { |
| 61 | + int l, r; |
| 62 | + scanf("%d %d", &l, &r); |
| 63 | + printf("%d\n", query (l - 1, r, 1, 0, n)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return 0; |
| 68 | +} |
0 commit comments