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



/*


SPOJ DQUERY offline updates using BIT


*/

// Hail god Yato

#include <bits/stdc++.h>
using namespace std;

#define hs                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 500005;
//
//
int tree[MAX];
int last[MAX];
struct queries {
  int l;
  int r;
  int index;
};
bool comparator(const queries &fst, const queries &snd) {
  if (fst.r == snd.r)
    return fst.l < snd.l;
  return fst.r < snd.r;
}
void update(int n, int idx, int val) {
  while (idx <= n) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}
int read(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}
void solve() {
  int n, q;
  cin >> n >> q;
  int arr[n];
  for (int i = 0; i < MAX; i++)
    tree[i] = 0;
  for (int i = 0; i < MAX; i++)
    last[i] = -1;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  queries quer[q];
  for (int i = 0; i < q; i++) {
    cin >> quer[i].l >> quer[i].r;
    quer[i].index = i;
  }
  sort(quer, quer + q, comparator);
  int idx = 0;
  map<int, int> store;
  for (int i = 0; i < n; i++) {
    update(n, i + 1, +1);
    if (last[arr[i]] != -1)
      update(n, last[arr[i]], -1);
    last[arr[i]] = i + 1;

    while (idx < q && quer[idx].r == i + 1) {
      int ans = read(quer[idx].r);
      ans -= read(quer[idx].l - 1);
      store[quer[idx].index] = ans;
      ++idx;
    }
    // ++idx;
  }
  for (auto &itr : store)
    cout << itr.second << "\\n";
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

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

#define int ll
// Each segment of the segment tree would be a set
// to maintain distinct elements
set<int> *segment;
vector<int> arr;

// 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) {

  // 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);
  build(1 + 2 * i, 1 + (s + e) / 2, e);

  // 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() << "\\n";
}

// Driver code
int32_t main() {

  ll n, q;
  cin >> n >> q;
  // int arr[n];
  arr.resize(4 * (n + 1));
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  // int arr[] = { 1, 1, 2, 1, 3 };
  // int n = sizeof(arr) / sizeof(arr[0]);

  init(n);

  // Bulid the segment tree
  build(1, 0, n - 1);
  while (q--) {
    int f, s;
    cin >> f >> s;
    --f, --s;
    // Query in range 0 to 4
    getDistinct(f, s, n);
  }

  return 0;
}