1 / 3
Jan 2023

What’s wrong with my code?:

#include <iostream>
using namespace std;

const int MAXINT = 2147483647;
const int MAXN = 1e5+7;
const int BASE = 128*1024;
int numbers[MAXN];
int segTree[2*BASE+7];

int inti(int n){
for (int i=1; i<=n; ++i)
    segTree[BASE+i] = numbers[i];
for (int i=BASE-1; i>=1; --i)
    segTree[i] = min(segTree[i*2], segTree[i*2+1]);
}
 int get_min(int i, int j, int v, int l, int r){
    if ( l>j || r <i)
        return MAXINT;
    if (l>=i && r<=j)
        return segTree[v];
    int mid = (l+r)/2;
    int res_l = get_min(i, j, v*2, l, mid);
    int res_r = get_min(i, j, v*2+1, mid+1, r);
    return min(res_l, res_r);
}

int main()
{
    int n;
    cin >> n;
    for (int i=1; i<=n; ++i)
        cin >> numbers[i];
    inti(n);
    int q;
    cin >> q;
    for (int i=1; i<=q; ++i){
        int querry_i, querry_j;
    cin >> querry_i >> querry_j;
        cout << get_min(querry_i+1, querry_j+1, 1, 0, BASE-1) << endl;
}
    return 0;
}
  • created

    Jan '23
  • last reply

    Feb '23
  • 2

    replies

  • 520

    views

  • 3

    users

  • 1

    like

What does the inti method return? Try changing it to void.

1 month later

I think the size of segment tree should be 4*MAXN, Your current size is 131072 which isn’t enough.
Also I am not sure if your inti() is correct or not.