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

#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[2000001];
int n, m, a[1000001], ans[2000001];
int st[5 * 1000001], used[10000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  // scanf("%d", &m);
  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);

  return 0;
}

Error: 0

Fixed submission:

// C++ implementation of above approach
/************************************************************LET US BREAK
 * BAD!****************************************************************/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvi vector<vector<int>>
#define vlp vector<pair<ll, ll>>
#define vllp vector<pair<ll, ll>, ll>
#define mll map<ll, ll>
#define rep(i, a) for (ll i = 0; i < a; i++)
#define rep1(i, a) for (ll i = 1; i < a; i++)
#define foi(i, a, b) for (ll i = a; i < b; i++)
#define fod(i, a, b) for (ll i = a; i >= b; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
// #define MOD 998244353
#define endl "\\n"
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

using namespace std;
using namespace __gnu_pbds;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll fexp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = ans * a % MOD;
    b /= 2;
    a = a * a % MOD;
  }
  return ans;
}
ll inverse(ll a, ll p) { return fexp(a, p - 2); }
void ingraph(vvl &graph, ll m) {
  ll x, y;
  rep(i, m) {
    cin >> x >> y;
    x--, y--;
    graph[x].pb(y);
    graph[y].pb(x);
  }
}
ll modify(ll n) {
  ll res = n;
  res %= MOD;
  res += MOD;
  res %= MOD;
  return res;
}

// int main(){
// 	//#ifndef ONLINE_JUDGE
// 	//    freopen("../input.txt", "r", stdin);
// 	//    freopen("../output.txt", "w", stdout);
// 	//#endif
// 	ios::sync_with_stdio(0);
// 	cin.tie(0); cout.tie(0);
// 	ll t;
// 	cin>>t;
// 	// t = 1;
// 	while(t--){

// 	}
// return 0;
// }
// 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 arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  init(n);

  // Bulid the segment tree
  build(1, 0, n - 1, arr);
  int l, r;
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    getDistinct(l - 1, r - 1, n);
  }
  // Query in range 0 to 4
  // getDistinct(0, 4, n);

  return 0;
}