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

#include <bits/stdc++.h>
#define ll long long
#define db double
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
using namespace std;

ll mod = 1e9 + 7;
const ll M = 500006;
ll f[M], inv[M];
ll expo(ll base, ll exponent) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}
void compute() {
  f[0] = 1;
  rep(i, 1, M - 1) { f[i] = (1LL * i * f[i - 1]) % mod; }
  inv[M - 1] = expo(f[M - 1], mod - 2);
  for (ll i = M - 2; i >= 0; i--) {
    inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod;
  }
}
ll C(ll n, ll r) {
  return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod;
}

vector<bool> prime(1000002, true);
void Sieve() {

  for (int p = 2; p * p <= 1000001; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      for (int i = p * p; i <= 1000001; i += p)
        prime[i] = false;
    }
  }
}

ll bit[2000000] = {0};
void update(ll id, ll val, ll n) {
  for (; id < n; id |= id + 1)
    bit[id] += val;
}
ll query(ll id, ll n) {
  ll res = 0;
  for (; id >= 0; id = (id & (id + 1)) - 1) {
    res += bit[id];
  }
  return res;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  // freopen("alchemy_input.txt", "r", stdin);
  //    // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  //    #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // compute();
  ll tests = 1, t = 1;
  // cin>>tests;
  while (tests--) {
    ll i, j, n, q;
    cin >> n >> q;
    vector<ll> c(n);
    rep(i, 0, n - 1) cin >> c[i];
    map<ll, ll> mp;
    i = 0;
    j = 0;
    vector<pair<ll, ll>> v, v2;
    while (q--) {
      set<ll> st;
      ll l, r;
      cin >> l >> r;
      l--;
      r--;
      v.pb({r, l});
    }
    v2 = v;
    map<pair<ll, ll>, ll> ans;
    sort(all(v));
    vector<ll> last_visit(n + 1, -1);
    ll cntr = 0;
    for (i = 0; i < n; i++) {
      if (last_visit[c[i]] != -1)
        update(last_visit[c[i]], -1, n);
      last_visit[c[i]] = i;
      update(i, 1, n);
      while (cntr < v.size() && v[cntr].first == i) {
        ll l = v[cntr].second, r = v[cntr].first;
        ans[{r, l}] = query(r, n) - query(l - 1, n);
        cntr++;
      }
    }

    for (auto x : v2) {
      cout << ans[x] << "\\n";
    }
  }

  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 i, j, n, q;
  cin >> n >> q;
  int arr[n];
  for (i = 0; i < n; i++)
    cin >> arr[i];
  // 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--;
    getDistinct(l, r, n);
  }

  return 0;
}