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

#include <cstdio>
#include <map>

using std::map;

const int MAXN = 500005, MAXLGN = 40;

int N, Q;
map<int, int> pos;
int tree[MAXN];
int cant;
struct node {
  int val, L, R, size;
} buff[2 * MAXN * MAXLGN];

int build(int lo, int hi) {
  if (lo > hi)
    return 0;

  int idx = ++cant;

  int mid = (lo + hi) / 2;
  buff[idx] = (node){mid, build(lo, mid - 1), build(mid + 1, hi), 0};
  return idx;
}

int update(int x, int val, int amount) {

  if (x == 0)
    return 0;

  int idx = ++cant;

  int L = buff[x].L;
  int R = buff[x].R;
  if (val < buff[x].val)
    L = update(L, val, amount);
  if (val > buff[x].val)
    R = update(R, val, amount);

  buff[idx] = (node){buff[x].val, L, R, buff[x].size + amount};

  return idx;
}

int query(int x, int val) {
  if (val < buff[x].val)
    return query(buff[x].L, val) + buff[x].size - buff[buff[x].L].size;

  if (val > buff[x].val)
    return query(buff[x].R, val);

  return buff[x].size - buff[buff[x].L].size;
}

int main() {

  scanf("%d", &N);
  scanf("%d", &Q);
  tree[0] = build(1, N);
  for (int i = 1; i <= N; i++) {
    int x, posx;
    scanf("%d", &x);
    posx = pos[x];
    if (posx != 0)
      tree[i] = update(update(tree[i - 1], posx, -1), i, +1);
    else
      tree[i] = update(tree[i - 1], i, +1);
    pos[x] = i;
  }
  while (Q--) {
    int lo, hi;
    scanf("%d %d", &lo, &hi);
    printf("%d\\n", query(tree[hi], lo));
  }

  return 0;
}


Error: 0

Fixed submission:

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;

// Each segment of the segment tree would be a set
// to maintain distinct elements
set<int> *segment;

// Build the segment tree
// i denotes current node, s denotes start and
// e denotes the end of range for current node
void build(int i, int s, int e, int arr[]) {

  // If start is equal to end then
  // insert the array element
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  // Else divide the range into two halves
  // (start to mid) and (mid+1 to end)
  // first half will be the left node
  // and the second half will be the right node
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);

  // Insert the sets of right and left
  // node of the segment tree
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

// Query in an range a to b
set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  // If the range is out of the bounds
  // of this segment
  if (b < l || a > r)
    return result;

  // If the range lies in this segment
  if (a <= l && r <= b)
    return segment[node];

  // Else query for the right and left
  // leaf node of this subtree
  // and insert them into the set
  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());

  // Return the result
  return result;
}

// Initialize the segment tree
void init(int n) {
  // Get the height of the segment tree
  int h = (int)ceil(log2(n));
  h = (2 * (pow(2, h))) - 1;

  // Initialize the segment tree
  segment = new set<int>[h];
}

// Function to get the result for the
// subarray from arr[l] to arr[r]
void getDistinct(int l, int r, int n) {
  // Query for the range set
  set<int> ans = query(1, 0, n - 1, l, r);

  cout << ans.size() << endl;
}

// Driver code
int main() {
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  init(n);
  build(1, 0, n - 1, a);
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    getDistinct(a, b, n);
  }

  return 0;
}