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

// 高知能系Vtuberの高井茅乃です。
// Twitter: https://twitter.com/takaichino
// YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i, n) for (int i = 0; i < n; i++)
#define REP1(i, n) for (int i = 1; i <= n; i++)
#define MODA 1000000007

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  for (T &x : vec) {
    is >> x;
  }
  return is;
}

// ei1333さんのGitHubから https://ei1333.github.io/luzhiled/
template <typename T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};
// ここまで

int main() {
  int N, Q;
  cin >> N;
  cin >> Q;
  vector<int> A(N);
  cin >> A;
  BinaryIndexedTree<int> bit(N);
  int colr[N + 1] = {};
  REP(i, N + 1) colr[i] = -1;
  vector<tuple<int, int, int>> qs;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    qs.push_back(make_tuple(l, r, i));
  }
  sort(qs.begin(), qs.end(),
       [](const tuple<int, int, int> &x, const tuple<int, int, int> &y) {
         return get<1>(x) < get<1>(y);
       });
  int anss[Q] = {};
  int nowi = 0, nowq = 0;
  while (nowi < N || nowq < Q) {
    while (nowi < get<1>(qs[nowq])) {
      if (colr[A[nowi]] >= 0)
        bit.add(colr[A[nowi]], -1);
      bit.add(nowi, 1);
      colr[A[nowi]] = nowi;
      nowi++;
    }
    int l, r, ind;
    tie(l, r, ind) = qs[nowq];
    anss[ind] = bit.sum(r - 1) - bit.sum(l - 2);
    /*
    cout << ind << " " << l << " " << r << " " ;
    REP(i, N) cout << bit.sum(i) - bit.sum(i-1) << " ";
    cout << bit.sum(l-2) << " " << bit.sum(r-1) << endl;
    */
    nowq++;
  }
  for (int i = 0; i < Q; i++) {
    cout << anss[i] << "\\n";
  }
}

Error: 0

Fixed submission:

// 高知能系Vtuberの高井茅乃です。
// Twitter: https://twitter.com/takaichino
// YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i, n) for (int i = 0; i < n; i++)
#define REP1(i, n) for (int i = 1; i <= n; i++)
#define MODA 1000000007

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  for (T &x : vec) {
    is >> x;
  }
  return is;
}

// ei1333さんのGitHubから https://ei1333.github.io/luzhiled/
struct Mo {
  using ADD = function<void(int)>;
  using DEL = function<void(int)>;
  using REM = function<void(int)>;

  int width;
  vector<int> left, right, order;
  vector<bool> v;

  Mo(int N, int Q) : width((int)sqrt(N)), order(Q), v(N) {
    iota(begin(order), end(order), 0);
  }

  void add(int l, int r) { /* [l, r) */
    left.emplace_back(l);
    right.emplace_back(r);
  }

  int run(const ADD &add, const DEL &del, const REM &rem) {
    assert(left.size() == order.size());
    sort(begin(order), end(order), [&](int a, int b) {
      int ablock = left[a] / width, bblock = left[b] / width;
      if (ablock != bblock)
        return ablock < bblock;
      if (ablock & 1)
        return right[a] < right[b];
      return right[a] > right[b];
    });
    int nl = 0, nr = 0;
    auto push = [&](int idx) {
      v[idx].flip();
      if (v[idx])
        add(idx);
      else
        del(idx);
    };
    for (auto idx : order) {
      while (nl > left[idx])
        push(--nl);
      while (nr < right[idx])
        push(nr++);
      while (nl < left[idx])
        push(nl++);
      while (nr > right[idx])
        push(--nr);
      rem(idx);
    }
  }
};
// ここまで

// 検証用コードもei1333さんのGitHubから戴きました。
// https://ei1333.github.io/luzhiled/snippets/other/mo.html
int main() {
  int N, Q;
  cin >> N;
  cin >> Q;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<int> f(1000001);
  Mo mo(N, Q);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    mo.add(--l, r);
  }
  int ret = 0;
  auto add = [&](int idx) {
    if (f[A[idx]]++ == 0)
      ret++;
  };
  auto del = [&](int idx) {
    if (f[A[idx]]-- == 1)
      --ret;
  };
  vector<int> ans(Q);
  auto rem = [&](int idx) { ans[idx] = ret; };
  mo.run(add, del, rem);
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << "\\n";
  }
}