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

// #define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void prec(const int &DIG) {
  cout << fixed << setprecision(DIG);
  cerr << fixed << setprecision(DIG);
}
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
#define dbl(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << "\\n"
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
  CERR(FIRST);
  cerr << ", ";
  CERR(REST...);
}
#define dbs(...)                                                               \\
  cerr << "(" << #__VA_ARGS__ << "): (";                                       \\
  CERR(__VA_ARGS__);                                                           \\
  cerr << ")\\n"
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
template <typename T> void CERR(const vector<T> &VEC) {
  cerr << "{ ";
  itrep(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\\n"
const ll INF = 1e18; // LONG_LONG_MAX;
// const int INF = INT_MAX;
const ll MOD = 1000000007; // 998244353;

// Fenwick Tree
namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral =
    typename std::conditional<std::is_integral<T>::value ||
                                  is_signed_int128<T>::value ||
                                  is_unsigned_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_signed_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_signed<T>::value) ||
                                  is_signed_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value, make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using to_unsigned =
    typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>,
                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

} // namespace internal

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
  using U = internal::to_unsigned_t<T>;

public:
  fenwick_tree() : _n(0) {}
  fenwick_tree(int n) : _n(n), data(n) {}

  void add(int p, T x) {
    assert(0 <= p && p < _n);
    p++;
    while (p <= _n) {
      data[p - 1] += U(x);
      p += p & -p;
    }
  }

  T sum(int l, int r) {
    assert(0 <= l && l <= r && r <= _n);
    return sum(r) - sum(l);
  }

private:
  int _n;
  std::vector<U> data;

  U sum(int r) {
    U s = 0;
    while (r > 0) {
      s += data[r - 1];
      r -= r & -r;
    }
    return s;
  }
};

} // namespace atcoder

using namespace atcoder;

struct Query {
  int l, r, i;
};
bool comp_r(Query &x, Query &y) { return x.r < y.r; }
// bool comp_i(static Query &x, static Query &y) { return x.i < y.i; }

int main() {
  // 色は1-indのまま，位置は0-indに直す
  int N, Q;
  cin >> N >> Q;
  vi c(N);
  rep(i, N) cin >> c[i];
  vector<Query> q;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    q.push_back(Query{l - 1, r - 1, i});
  }
  sort(all(q), comp_r);
  vi ans(Q);

  map<int, int> last; // last[i]:最も右にある色iの玉の座標

  fenwick_tree<int> fw(N);

  int cur = 0, i = 0;
  while (cur < N and i < Q) {
    if (cur < q[i].r) {
      if (last.count(c[cur]))
        fw.add(last[c[cur]], -1);
      fw.add(cur, 1);
      last[c[cur]] = cur;
      cur++;
    } else if (cur == q[i].r) {
      if (last.count(c[cur]))
        fw.add(last[c[cur]], -1);
      fw.add(cur, 1);
      last[c[cur]] = cur;
      ans[q[i].i] = fw.sum(q[i].l, q[i].r + 1);
      i++;
    } else if (cur > q[i].r) {
      i++;
    }
  }
  rep(i, Q) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep0(TMS) for (int CNT = 0; CNT < (int)(TMS); CNT++)
#define rep(CNT, GOAL) for (int CNT = 0; CNT < (int)(GOAL); CNT++)
#define rep2(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT < (int)(GOAL); CNT++)
#define rep3(CNT, START, GOAL)                                                 \\
  for (int CNT = (int)(START); CNT > (int)(GOAL); CNT--)
#define all(CONT) begin(CONT), end(CONT)
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void prec(const int &DIG) {
  cout << fixed << setprecision(DIG);
  cerr << fixed << setprecision(DIG);
}
template <typename T> void CERR(const T &ELEM) { cerr << ELEM; }
#define dbl(OBJ)                                                               \\
  cerr << #OBJ << ": ";                                                        \\
  CERR(OBJ);                                                                   \\
  cerr << "\\n"
template <typename T, typename... Ts>
void CERR(const T &FIRST, const Ts &...REST) {
  CERR(FIRST);
  cerr << ", ";
  CERR(REST...);
}
#define dbs(...)                                                               \\
  cerr << "(" << #__VA_ARGS__ << "): (";                                       \\
  CERR(__VA_ARGS__);                                                           \\
  cerr << ")\\n"
#define itrep(ITR, CONT) for (auto ITR = begin(CONT); ITR != end(CONT); ITR++)
template <typename T> void CERR(const vector<T> &VEC) {
  cerr << "{ ";
  itrep(ITR, VEC) {
    CERR(*ITR);
    cerr << ", ";
  }
  cerr << "}";
}
#define YN(FLG) cout << (FLG ? "YES" : "NO") << "\\n"
#define Yn(FLG) cout << (FLG ? "Yes" : "No") << "\\n"
#define yn(FLG) cout << (FLG ? "yes" : "no") << "\\n"
const ll INF = 1e18; // LONG_LONG_MAX;
// const int INF = INT_MAX;
const ll MOD = 1000000007; // 998244353;

// Fenwick Tree
namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral =
    typename std::conditional<std::is_integral<T>::value ||
                                  is_signed_int128<T>::value ||
                                  is_unsigned_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_signed_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_signed<T>::value) ||
                                  is_signed_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value, make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type, std::false_type>::type;

template <class T>
using to_unsigned =
    typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>,
                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

} // namespace internal

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
  using U = internal::to_unsigned_t<T>;

public:
  fenwick_tree() : _n(0) {}
  fenwick_tree(int n) : _n(n), data(n) {}

  void add(int p, T x) {
    assert(0 <= p && p < _n);
    p++;
    while (p <= _n) {
      data[p - 1] += U(x);
      p += p & -p;
    }
  }

  T sum(int l, int r) {
    assert(0 <= l && l <= r && r <= _n);
    return sum(r) - sum(l);
  }

private:
  int _n;
  std::vector<U> data;

  U sum(int r) {
    U s = 0;
    while (r > 0) {
      s += data[r - 1];
      r -= r & -r;
    }
    return s;
  }
};

} // namespace atcoder

using namespace atcoder;

struct Query {
  int l, r, i;
};
bool comp_r(Query &x, Query &y) { return x.r < y.r; }
// bool comp_i(static Query &x, static Query &y) { return x.i < y.i; }

int main() {
  // 色は1-indのまま，位置は0-indに直す
  int N, Q;
  cin >> N >> Q;
  vi c(N);
  rep(i, N) cin >> c[i];
  vector<Query> q;
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    q.push_back(Query{l - 1, r - 1, i});
  }
  sort(all(q), comp_r);
  vi ans(Q);

  map<int, int> last; // last[i]:最も右にある色iの玉の座標

  fenwick_tree<int> fw(N);

  int cur = 0, i = 0;
  while (cur < N) {
    if (cur < q[i].r) {
      if (last.count(c[cur]))
        fw.add(last[c[cur]], -1);
      fw.add(cur, 1);
      last[c[cur]] = cur;
      cur++;
    } else if (cur == q[i].r) {
      if (last.count(c[cur]))
        fw.add(last[c[cur]], -1);
      fw.add(cur, 1);
      last[c[cur]] = cur;
      ans[q[i].i] = fw.sum(q[i].l, q[i].r + 1);
      i++;
    } else if (cur > q[i].r) {
      i++;
    }
  }
  rep(i, Q) cout << ans[i] << endl;
}