Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 3;
- const int LOG = 18;
- int n, q, a[N], mxp[N], p[LOG], dp[LOG][N];
- void preCalc() {
- p[0] = 1;
- for (int i = 1; i < LOG; ++i) {
- p[i] = p[i - 1] * 2;
- }
- for (int i = 1, curP = 0, curValue = 1; i < N; ++i) {
- while (curValue * 2 <= i) {
- curValue *= 2;
- curP++;
- }
- mxp[i] = curP;
- }
- }
- void build() {
- for (int i = 1; i <= n; ++i) {
- dp[0][i] = a[i];
- }
- int Log = mxp[n];
- for (int k = 1; k <= Log; ++k) {
- for (int i = 1; i <= n; ++i) {
- dp[k][i] = min(dp[k - 1][i], dp[k - 1][i + p[k - 1]]);
- }
- }
- }
- int getMin(int L, int R) {
- int k = mxp[R - L + 1];
- return min(dp[k][L], dp[k][R - p[k] + 1]);
- }
- void solve(int testId) {
- cin >> n >> q;
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- }
- build();
- cout << "Case " << testId << ":" << endl;
- while (q--) {
- int i, j;
- cin >> i >> j;
- cout << getMin(i, j) << endl;
- }
- }
- int main() {
- ios_base::sync_with_stdio(NULL);
- cin.tie(0); cout.tie(0);
- preCalc();
- int t;
- cin >> t;
- for (int testId = 1; testId <= t; ++testId) {
- solve(testId);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement