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

#include "algorithm"
#include "bitset"
#include "cstring"
#include "ctime"
#include "iomanip"
#include "iostream"
#include "map"
#include "math.h"
#include "numeric"
#include "queue"
#include "set"
#include "stack"
#include "string"
#include "vector"

#include "ext/pb_ds/assoc_container.hpp" // Common file
#include "ext/pb_ds/detail/standard_policies.hpp"
#include "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

#define f first
#define lgn 25
#define endl '\\n'
#define sc second
#define N (int)2e6 + 5
#define pb push_back
#define mod 1000000007
#define ld long double
#define vi vector<int>
#define eb emplace_back
#define vpii vector<pii>
#define mii map<int, int>
#define int long long
#define pii pair<int, int>
#define pq priority_queue
#define BLOCK (int)sqrt(N)
#define test(x) while (x--)
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define fo(i, a, n) for (int i = a; i < n; i++)
#define rfo(i, n, a) for (int i = n; i >= a; i--)
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define time()                                                                 \\
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\\n"
#define PI acos(-1.0)
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OS;

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;

int n, m, k, q;
string s;
vi adj[N];
int a[N], last[N], ans[N];

struct T {
  int r, l, id;
} Q[N];

vi BIT(N);

void update(int i, int v) {
  for (; i < N; i += (i & (-i)))
    BIT[i] += v;
}

int query(int i) {
  int sum = 0;
  for (; i > 0; i -= (i & (-i)))
    sum += BIT[i];

  return sum;
}

bool cmp(T a, T b) { return a.r < b.r; }

void go() {
  cin >> n >> q;

  fo(i, 1, n + 1) cin >> a[i];

  // cin >> q;

  fo(i, 1, q + 1) {
    int l, r;
    cin >> l >> r;

    Q[i] = {r, l, i};
  }

  sort(Q + 1, Q + 1 + q, cmp);

  int j = 1;

  fo(i, 1, n + 1) {
    if (last[a[i]]) {
      update(last[a[i]], -1);
    }
    last[a[i]] = i;
    update(i, 1);

    while (j <= q and Q[j].r == i) {
      ans[Q[j].id] = query(Q[j].r) - query(Q[j].l - 1);
      j++;
    }
  }

  fo(i, 1, q + 1) cout << ans[i] << endl;
}

int32_t main() {
  // INP();
  FAST;
  int t = 1;
  // cin>>t;
  test(t) go();
}


Error: -11

Fixed submission:

#include "algorithm"
#include "bitset"
#include "chrono"
#include "cstring"
#include "ctime"
#include "iomanip"
#include "iostream"
#include "map"
#include "math.h"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "vector"

#include "ext/pb_ds/assoc_container.hpp" // Common file
#include "ext/pb_ds/detail/standard_policies.hpp"
#include "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

#define f first
#define lgn 25
#define endl '\\n'
#define sc second
#define pb push_back
#define N (int)1e5
#define PI acos(-1.0)
#define int long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define eb emplace_back
#define mii map<int, int>
#define vpii vector<pii>
#define pii pair<int, int>
#define pq priority_queue
#define BLOCK (int)sqrt(N)
#define test(x) while (x--)
#define all(x) begin(x), end(x)
#define allr(x) x.rbegin(), x.rend()
#define fo(i, a, n) for (int i = a; i < n; i++)
#define rfo(i, n, a) for (int i = n; i >= a; i--)
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define time()                                                                 \\
  cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\\n"
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OS;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;

int n, m, k, q;
string s;
vi adj[N];
int vis[N], par[N], a[N];

bitset<100001> seg[N];

void build(int v = 1, int tl = 0, int tr = n - 1) {
  if (tl == tr) {
    seg[v].set(a[tl]);
    return;
  }

  int tm = (tl + tr) / 2;

  build(v << 1, tl, tm);
  build(v << 1 | 1, tm + 1, tr);

  seg[v] = seg[v << 1] | seg[v << 1 | 1];
}

bitset<100001> query(int l, int r, int v = 1, int tl = 0, int tr = n - 1) {
  if (l > tr or r < tl) {
    bitset<100001> b = 0;
    return b;
  }
  if (tl == l and tr == r)
    return seg[v];

  int tm = tl + tr >> 1;

  bitset<100001> aa = query(l, min(r, tm), v << 1, tl, tm);
  bitset<100001> bb = query(max(l, tm + 1), r, v << 1 | 1, tm + 1, tr);

  return aa | bb;
}
void go() {
  cin >> n >> q;

  fo(i, 0, n) cin >> a[i];
  build();

  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;

    cout << query(l, r).count() << endl;
  }
}

int32_t main() {
  FAST;
  int t = 1;
  // cin>>t;
  test(t) go();
}