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

#include <bits/stdc++.h>

using namespace std;
const int MAX = 600005;
const int MAX1 = 1e6 + 5;
int tree[MAX], p[MAX1], ans[MAX], a[MAX];
pair<int, pair<int, int>> pr[MAX];

void update(int idx, int val) {
  while (idx < MAX) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}

int read(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}

int main() {
  // ifstream cin("input.txt");
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q, x;
  cin >> n >> q; // scanf("%d", &n);
  // scanf("%d", &q);
  for (int i = 1; i <= n; ++i)
    cin >> a[i]; // scanf("%d", &a[i]);
  for (int i = 0; i < q; ++i) {
    cin >> pr[i].second.first >>
        pr[i].first; // pr[i].second.first[scanf("%d %d", &pr[i].second.first,
                     // &pr[i].first);
    pr[i].second.second = i;
  }
  sort(pr, pr + q);
  memset(p, -1, sizeof(p));
  memset(tree, 0, sizeof(tree));
  x = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);
    while (x < q and pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);
      x++;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << "\\n"; // printf("%d\\n", ans[i]);
  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]
int getDistinct(int l, int r, int n) {
  // Query for the range set
  set<int> ans = query(1, 0, n - 1, l, r);

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

// Driver code
int main() {

  // int arr[] = { 1, 1, 2, 1, 3 };

  int nn, q;
  cin >> nn >> q;

  int arr[nn];

  for (int i = 0; i < nn; i++) {
    int a;
    cin >> a;
    arr[i] = a;
  }

  int n = sizeof(arr) / sizeof(arr[0]);

  init(n);

  // Bulid the segment tree
  build(1, 0, n - 1, arr);

  // Query in range 0 to 4
  // getDistinct(0, 4, n);

  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;

    cout << getDistinct(l, r, n) << "\\n";
  }

  return 0;
}