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

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2")                                        // Enable AVX

using namespace std;

#define F first
#define S second

#ifdef __debug_leywar
#include <debug/debugger.h>
#else
#define db(...) ;
#endif

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, q;

  cin >> n >> q;

  vector<int> ar(n + 1), freq(n + 1, n + 1), acc(n + 1);

  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }

  for (int i = n; i > 0; i--) {
    acc[i] = freq[ar[i]];
    freq[ar[i]] = i;
  }

  vector<pair<pair<int, int>, int>> querys(q);

  int cont = 1;
  for (auto &i : querys) {
    cin >> i.F.F >> i.F.S;
    i.S = cont++;
  }

  sort(querys.begin(), querys.end(),
       [](pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
         return a.F.F < b.F.F;
       });

  vector<int> bit(n + 2);

  function<void(int)> add = [&](int pos) {
    for (; pos > 0; pos -= (pos & -pos))
      bit[pos]++;
  };

  function<int(int)> query = [&](int pos) {
    int Qans = 0;
    for (; pos <= n + 1; pos += (pos & -pos))
      Qans += bit[pos];
    return Qans;
  };

  vector<int> ans(q);

  int pos = 0;

  for (int i = 0; i < q; i++) {
    while ((pos + 1) < querys[i].F.F)
      add(acc[++pos]);
    ans[querys[i].S - 1] = -query(querys[i].F.S + 1);
  }

  for (int i = 1; i <= n + 1; i++)
    bit[i] = 0;
  pos = 0;

  sort(querys.begin(), querys.end(),
       [](pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
         return a.F.S < b.F.S;
       });

  for (int i = 0; i < q; i++) {
    while (pos < querys[i].F.S)
      add(acc[++pos]);
    ans[querys[i].S - 1] += query(querys[i].F.S + 1);
  }

  for (auto i : ans)
    cout << i << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC option("arch=native", "tune=native", "no-zero-upper") // Enable AVX
#pragma GCC target("avx2")                                        // Enable AVX

using namespace std;

#define F first
#define S second

#ifdef __debug_leywar
#include <debug/debugger.h>
#else
#define db(...) ;
#endif

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, q;
  cin >> n >> q;

  int root = sqrt(n);

  vector<int> ar(n + 1);

  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }

  vector<pair<pair<int, int>, int>> querys(q);

  int cont = 0;
  for (auto &i : querys) {
    cin >> i.F.F >> i.F.S;

    i.S = ++cont;
  }

  sort(querys.begin(), querys.end(),
       [&](pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
         if ((a.F.F / root) != (b.F.S / root))
           return (a.F.F / root) < (b.F.S / root);

         if ((a.F.F / root) % 2 == 1)
           return a.F.S < b.F.S;
         else
           return a.F.S > b.F.S;
       });

  vector<pair<int, int>> ans;
  vector<int> flag(n + 3);
  int x, xend, sc;
  x = xend = sc = 0;

  ar[0] = 0;

  for (auto i : querys) {
    function<void(int)> Add = [&](int nod) {
      sc += (!flag[ar[nod]]);
      flag[ar[nod]]++;
    };

    function<void(int)> Del = [&](int nod) {
      flag[ar[nod]]--;
      sc -= (!flag[ar[nod]]);
    };

    while (xend < i.F.S) {
      xend++;
      Add(xend);
    }
    while (xend > i.F.S) {
      Del(xend);
      xend--;
    }
    while ((x + 1) < i.F.F) {
      x++;
      Del(x);
    }
    while (x > i.F.F - 1) {
      Add(x);
      x--;
    }

    ans.push_back({i.S, sc});
  }

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

  for (auto i : ans)
    cout << i.S << endl;

  return 0;
}