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

#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(Args &&...args) const {
      print(std::forward<Args>(args)...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(T &&head, Tail &&...tail) const {
      std::cout << head, print_rest(std::forward<Tail>(tail)...);
    }
    template <class T, class... Tail>
    void print_rest(T &&head, Tail &&...tail) const {
      std::cout << sep << head, print_rest(std::forward<Tail>(tail)...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\\n") const {
    return __Prints(sep, term);
  }
  template <class T>
  void print_vec(const std::vector<T> &vec, const char *sep = " ",
                 const char *term = "\\n") const {
    const size_t sz = vec.size();
    for (size_t i = 0; i < sz; i++)
      std::cout << vec[i] << (i == sz - 1 ? term : sep);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

template <typename T> class BIT {
private:
  int n;
  std::vector<T> vec;

public:
  BIT(int n) : n(n), vec(n + 1) {}
  // i 番目の要素に a を足す
  void add(int i, const T &a) {
    for (++i; i <= n; i += i & (-i)) {
      vec[i] += a;
    }
  }
  // 区間[0, i)に対するクエリを実行する
  T query(int i) const {
    T res = 0;
    for (; i >= 1; i -= i & (-i)) {
      res += vec[i];
    }
    return res;
  }
  // 区間[i, j)に対するクエリを実行する
  T query(int i, int j) const { return query(j) - query(i); }
  size_t size() const noexcept { return n; }
  void reset() noexcept {
    std::fill(vec.begin(), vec.end(), static_cast<T>(0));
  }
};

void solve() {
  ll N, Q;
  cin >> N >> Q;
  Vl C(N);
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
    --C[i];
  }
  using Tup = tuple<ll, ll, ll>;
  vector<Tup> queries(Q);
  for (ll i = 0; i < Q; i++) {
    ll a, b;
    cin >> a >> b;
    --a;
    queries[i] = {b, a, i}; // 左右逆
  }
  sort(queries.begin(), queries.end());
  BIT<ll> bb(N);
  Vl la(N, -1);
  ll cur = 0;
  Vl ans(Q);
  for (ll i = 0; i < Q; i++) {
    auto [right, left, idx] = queries[i];
    while (cur < right) {
      if (la[C[cur]] != -1) {
        bb.add(la[C[cur]], -1);
      }
      la[C[cur]] = cur;
      bb.add(cur, 1);
      ++cur;
    }
    ans[idx] = bb.query(left, right);
  }

  for (ll i = 0; i < Q; i++) {
    prints()(ans[i]);
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
  class __Prints {
  public:
    __Prints(const char *sep, const char *term) : sep(sep), term(term) {}
    template <class... Args> void operator()(Args &&...args) const {
      print(std::forward<Args>(args)...);
    }

  private:
    const char *sep, *term;
    void print() const { std::cout << term; }
    void print_rest() const { std::cout << term; }
    template <class T, class... Tail>
    void print(T &&head, Tail &&...tail) const {
      std::cout << head, print_rest(std::forward<Tail>(tail)...);
    }
    template <class T, class... Tail>
    void print_rest(T &&head, Tail &&...tail) const {
      std::cout << sep << head, print_rest(std::forward<Tail>(tail)...);
    }
  };

public:
  Prints() {}
  __Prints operator()(const char *sep = " ", const char *term = "\\n") const {
    return __Prints(sep, term);
  }
  template <class T>
  void print_vec(const std::vector<T> &vec, const char *sep = " ",
                 const char *term = "\\n") const {
    const size_t sz = vec.size();
    for (size_t i = 0; i < sz; i++)
      std::cout << vec[i] << (i == sz - 1 ? term : sep);
  }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

template <typename T> class BIT {
private:
  int n;
  std::vector<T> vec;

public:
  BIT(int n) : n(n), vec(n + 1) {}
  // i 番目の要素に a を足す
  void add(int i, const T &a) {
    for (++i; i <= n; i += i & (-i)) {
      vec[i] += a;
    }
  }
  // 区間[0, i)に対するクエリを実行する
  T query(int i) const {
    T res = 0;
    for (; i >= 1; i -= i & (-i)) {
      res += vec[i];
    }
    return res;
  }
  // 区間[i, j)に対するクエリを実行する
  T query(int i, int j) const { return query(j) - query(i); }
  size_t size() const noexcept { return n; }
  void reset() noexcept {
    std::fill(vec.begin(), vec.end(), static_cast<T>(0));
  }
};

void solve() {
  ll N, Q;
  cin >> N >> Q;
  Vl C(N);
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
    --C[i];
  }
  using Tup = tuple<ll, ll, ll>;
  vector<Tup> queries(Q);
  for (ll i = 0; i < Q; i++) {
    ll a, b;
    cin >> a >> b;
    --a;
    queries[i] = {b, a, i}; // 左右逆
  }
  sort(queries.begin(), queries.end());
  BIT<ll> bb(N);
  Vl la(N, -1);
  ll cur = 0;
  Vl ans(N);
  for (ll i = 0; i < Q; i++) {
    auto [right, left, idx] = queries[i];
    while (cur < right) {
      if (la[C[cur]] != -1) {
        bb.add(la[C[cur]], -1);
      }
      la[C[cur]] = cur;
      bb.add(cur, 1);
      ++cur;
    }
    ans[idx] = bb.query(left, right);
  }

  for (ll i = 0; i < Q; i++) {
    prints()(ans[i]);
  }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  cout << setprecision(18) << fixed;
  solve();
  cout << flush;
  return 0;
}