UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212119#3816. 元素PanjunnanCompile Error//C++1.0kb2024-10-13 11:22:312024-10-13 12:24:31

answer

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 100005;
int n, q;
int a[MAXN];
int st[MAXN][20]; 

void buildST() {
    for (int i = 1; i <= n; ++i) {
        st[i][0] = a[i];
    }
    for (int j = 1; (1 << j) <= n; ++j) {
        for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
            st[i][j] = min(st[i][j-1], st[i+(1<<(j-1))][j-1]);
        }
    }
}

int query(int l, int r) {
    int k = log2(r - l + 1);
    return min(st[l][k], st[r-(1<<k)+1][k]);
}

int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    buildST();

    while (q--) {
        int l, r;
        cin >> l >> r;
        int min_val = query(l, r);
        int x = l, y = r;
        while (x <= y) {
            if (a[x] == min_val) break;
            x++;
        }
        while (y >= x) {
            if (a[y] == min_val) break;
            y--;
        }
        cout << y - x + 1 << endl;
    }

    return 0;
}

Details

answer.code: In function 'int query(int, int)':
answer.code:24:27: error: 'log2' was not declared in this scope
     int k = log2(r - l + 1);\x0d
                           ^