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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
// typedef long long int ll;
using namespace std;
struct node {
  int left, right, index;
};
int compare(node a, node b) { return (a.right < b.right); }
void update(int tree[], int root, int l, int r, int x, int plus) {
  if (l == r && l == x) {
    tree[root] += plus;
    return;
  }
  int m = (l + r) / 2;
  if (x <= m && l <= x)
    update(tree, root * 2, l, m, x, plus);
  else
    update(tree, root * 2 + 1, m + 1, r, x, plus);
  tree[root] = tree[root * 2] + tree[root * 2 + 1];
  return;
}
int query(int tree[], int root, int l, int r, int lq, int rq) {
  if (l >= lq && r <= rq)
    return tree[root];
  if (r < lq || l > rq)
    return 0;
  int m = (l + r) / 2;
  // if(lq>m)return s+=query(tree,root*2+1,l,r,lq,rq,s);
  // if(rq<m)return s+query(tree,root*2,l,r,lq,rq,s);
  return query(tree, root * 2, l, m, lq, rq) +
         query(tree, root * 2 + 1, m + 1, r, lq, rq);
}
int main() {
  // your code goes here
  int n, i, c, q, d;
  cin >> n >> q;
  vector<int> a;
  a.push_back(0);
  for (i = 1; i <= n; i++) {
    cin >> c;
    a.push_back(c);
  }

  //	vector<int, pair<int,int>> v;
  node v[q + 1];
  for (i = 1; i <= q; i++) {
    cin >> v[i].left >> v[i].right;
    v[i].index = i;
  }
  sort(v + 1, v + q + 1, compare);
  int b[1000001] = {0};
  int tree[4 * n + 4] = {0};
  int j, r = 1;
  int ord[q + 1];
  for (i = 1; i <= q; i++) {
    while (r <= v[i].right) {
      if (b[a[r]] == 0) {
        b[a[r]] = r;
        update(tree, 1, 1, n, r, 1);
      } else {
        update(tree, 1, 1, n, b[a[r]], -1);
        b[a[r]] = r;
        update(tree, 1, 1, n, r, 1);
      }
      r++;
      // for(j=1;j<=10;j++)cout<<tree[j]<<" "; cout<<endl;
    }

    ord[v[i].index] = query(tree, 1, 1, n, v[i].left, v[i].right);
  }

  for (i = 1; i <= q; i++)
    printf("%d\\n", ord[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]
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, i;
  cin >> n >> q;
  int arr[n];
  for (i = 0; i < n; i++)
    cin >> arr[i];

  init(n);

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

  // Query in range 0 to 4
  int u, v;
  for (i = 0; i < q; i++) {
    cin >> u >> v;
    u--;
    v--;
    getDistinct(u, v, n);
  }
  return 0;
}