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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#define Boost()                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

using namespace std;

typedef long long ll;

typedef struct node {
  int s, e, val, pos;
  bool operator<(const node &n) const {
    if (e != n.e)
      return e < n.e;
    return val > n.val;
  }
  node(){};
  node(int ss, int ee, int v, int p) { s = ss, e = ee, val = v, pos = p; }
} node;

int occs[(int)1e6 + 5], tree[500100], n, ans[500100];

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

int query(int x) {
  int ret = 0;
  while (x > 0) {
    ret += tree[x];
    x -= x & -x;
  }
  return ret;
}

int main() {
  Boost();

  int q, s, e;
  vector<node> v;
  scanf("%d", &n);
  scanf("%d", &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s);
    v.push_back(node(i, i, s, -1));
  }

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &e);
    v.push_back(node(s, e, 0, i));
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < v.size(); i++) {
    if (v[i].val == 0) {
      ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
    } else {
      if (occs[v[i].val] != 0)
        update(occs[v[i].val], -1);
      update(v[i].s, 1);
      occs[v[i].val] = v[i].s;
    }
  }

  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}


Error: -11

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
#define kill                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define rev(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(c) c.begin(), c.end()
#define go(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define fi first
#define se second
#define MOD 998244353
#define Boost()                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define MOD1 1000000007
ll power(ll a, ll b) {
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
#define oo 100000000000000000
const ll dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const ll dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
unordered_set<int> *segment;
vector<int> arr;
void build(int i, int s, int e) {

  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  build(2 * i, s, (s + e) / 2);
  build(1 + 2 * i, 1 + (s + e) / 2, e);

  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

unordered_set<int> query(int node, int l, int r, int a, int b) {
  unordered_set<int> left, right, result;

  if (b < l || a > r)
    return result;

  // If the range lies in this segment
  if (a <= l && r <= b)
    return segment[node];

  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 unordered_set<int>[h];
}

void getDistinct(int l, int r, int n) {
  // Query for the range set
  unordered_set<int> ans = query(1, 0, n - 1, l, r);

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

int main() {
  Boost();
  ll T = 1;
  int n, q;
  cin >> n >> q;
  arr.resize(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  init(n);
  build(1, 0, n - 1);
  while (q--) {
    int s, e;
    cin >> s >> e;
    s--;
    e--;
    getDistinct(s, e, n);
  }
  return 0;
}