Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

#define endl "\\n"
#define forn(i, x, n) for (int i = x; i < n; i++)
#define forit(it, m)                                                           \\
  for (decltype(m)::iterator it = m.begin(); it != m.end(); ++it)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void zeus() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
const int MAX = 5000001;

// structure to store queries
struct Query {
  int l, r, idx;
};

// cmp function to sort queries according to r
bool cmp(Query x, Query y) { return x.r < y.r; }

// updating the bit array
void update(int idx, int val, int bit[], int n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

// querying the bit array
int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(int arr[], int n, Query queries[], int q) {
  // initialising bit array
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  // holds the rightmost index of any number
  // as numbers of a[i] are less than or equal to 10^6
  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));

  // answer for each query
  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    // If last visit is not -1 update -1 at the
    // idx equal to last_visit[arr[i]]
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    // Setting last_visit[arr[i]] as i and updating
    // the bit array accordingly
    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    // If i is equal to r of any query  store answer
    // for that query in ans[]
    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }
  // cout << "hello" << endl;
  //  print answer for each query
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}
int32_t main() {

  zeus();

  int n, q;
  cin >> n >> q;

  int a[n];
  forn(i, 0, n) { cin >> a[i]; }

  Query queries[q];

  forn(i, 0, q) {
    int left, right;
    cin >> left >> right;
    queries[i].l = left - 1;
    queries[i].r = right - 1;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(a, n, queries, q);
  // cout << "hello" << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "\\n"
#define eps 0.00000001
LL pow(LL a, LL b, LL m) {
  LL x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > m)
        x %= m;
    }
    y = (y * y);
    if (y > m)
      y %= m;
    b /= 2;
  }
  return x % m;
}
LL gcd(LL a, LL b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
LL gen(LL start, LL end) {
  LL diff = end - start;
  LL temp = rand() % start;
  return temp + diff;
}
using namespace std;
int a[500001];
// int b[100001];
bitset<5001> tree1[2000001];
void build(int node, int i, int j) {
  if (i > j)
    return;
  if (i == j) {
    tree1[node].set(a[i]);
    // tree2[node].set(b[i]);
    return;
  }
  int mid = (i + j) / 2;
  build(2 * node, i, mid);
  build(2 * node + 1, mid + 1, j);
  tree1[node] = tree1[2 * node] | tree1[2 * node + 1];
  // tree2[node] = tree2[2 * node] | tree2[2 * node + 1];
}
bitset<5001> query1(int node, int i, int j, int l, int r) {
  if (i > j || i > r || j < l)
    return 0;
  if (i >= l && j <= r)
    return tree1[node];
  int mid = (i + j) / 2;
  return query1(2 * node, i, mid, l, r) |
         query1(2 * node + 1, mid + 1, j, l, r);
}
// bitset<5001>query2(int node, int i, int j, int l, int r) {
//     if (i > j || i > r || j < l)
//         return 0;
//     if (i >= l && j <= r)
//         return tree2[node];
//     int mid = (i + j) / 2;
//     return query2(2 * node , i , mid , l , r) | query2(2 * node + 1 , mid + 1
//     , j , l , r);
// }
void zeus() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int main() {
  zeus(); // ios_base::sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    // b[i] = a[i];
  }
  build(1, 1, n);
  while (q--) {
    int a, b;
    cin >> a >> b;
    cout << (query1(1, 1, n, a, b)).count() << endl;
  }
}