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

// C++ code to find number of distinct numbers
// in a subarray
#include <bits/stdc++.h>
using namespace std;

const int MAX = 1000001;

// 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++;
    }
  }

  // print answer for each query
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

// driver code
int main() {
  int n;
  cin >> n;
  int q;
  cin >> q;
  int i;
  int a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  Query queries[q];
  for (i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    y--;
    x--;
    queries[i].l = x;
    queries[i].r = y;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  answeringQueries(a, n, queries, q);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mid(L, R) ((L + R) / 2)
struct Node {
  int cnt, L, R;
  Node() {
    cnt = 0;
    L = R = -1;
  }
  Node(int x, int y, int z) {
    L = x;
    R = y;
    cnt = z;
  }
} tree[10000001];

int fastscan() {
  register int n = 0;
  char ch = getchar_unlocked();
  for (; (ch < 48 || ch > 57); ch = getchar_unlocked())
    ;
  for (; (ch > 47 && ch < 58); ch = getchar_unlocked())
    n = (n << 3) + (n << 1) + ch - 48;
  return n;
}
int G[5000001];
int A[5000001], root[5000001], RM[5000001], B[5000001], V[5000001];
int gc = 0, N;

int build(int L, int R) {
  ++gc;
  if (L == R)
    return gc;
  int x = gc;
  tree[x] = Node(build(L, mid(L, R)), build(mid(L, R) + 1, R), 0);
  return x;
}

int update(int L, int R, int root, int idx, int val) {
  if (L > idx || R < idx)
    return root;
  ++gc;
  if (L == idx && R == idx) {
    tree[gc] = Node(-1, -1, tree[root].cnt + val);
    return gc;
  }
  int x = gc;
  tree[x] = Node(update(L, mid(L, R), tree[root].L, idx, val),
                 update(mid(L, R) + 1, R, tree[root].R, idx, val),
                 tree[root].cnt + val);
  return x;
}

int query(int L, int R, int root, int qe, int qr) {
  if (qr < L || qe > R)
    return 0;
  if (qe <= L && R <= qr)
    return tree[root].cnt;
  return query(L, mid(L, R), tree[root].L, qe, qr) +
         query(mid(L, R) + 1, R, tree[root].R, qe, qr);
}

int main() {
  int M, i, x, y, z;
  N = fastscan();
  M = fastscan();
  memset(G, -1, sizeof(G));
  for (i = 1; i <= N; ++i) {
    A[i] = fastscan();
  }
  root[0] = build(1, N);
  for (i = 1; i <= N; ++i) {
    int p = root[i - 1];
    if (G[A[i]] != -1)
      p = update(1, N, p, G[A[i]], -1);
    root[i] = update(1, N, p, i, 1);
    G[A[i]] = i;
  }

  for (; M; --M) {
    x = fastscan();
    y = fastscan();
    printf("%d\\n", query(1, N, root[y], x, y));
  }

  return 0;
}