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

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

const int MOD1 = 1000000007;
const int MOD2 = 998244353;
const long double PI = 3.1415926535897932;
const long long MAXLL = 9223372036854775807;
const long long INF = 2305843009213693951;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};

#define ll long long
#define ull unsigned long long
#define ld long double
#define pll pair<long long, long long>
#define pcc pair<char, char>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vvvl vector<vector<vector<long long>>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vs vector<string>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<pair<long long, long long>>
#define umap unordered_map
#define uset unordered_set
#define Lqueue priority_queue<long long>
#define Squeue priority_queue<long long, vector<long long>, greater<long long>>
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define eb emplace_back

#define unless(a) if (!(a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rbf(a, x) for (auto &a : x)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)
#define bitrep(i, s, n) for (int i = (s); i < (1LL << (n)); i++)
#define bitcheck(bit, i) (bit) & (1LL << (i))
#define Maxe(x) *max_element((x).begin(), (x).end())
#define Mine(x) *min_element((x).begin(), (x).end())
#define Size(x) ((long long)(x).size())
#define Lin(s) getline(cin, (s))

void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long cutup(long long a, long long b) { return (a + b - 1) / b; }
long long popcnt(long long a) { return __builtin_popcountll(a); }
template <class T, class U> inline bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename t> constexpr t mpow(t a, long long b) {
  if (b == 0)
    return 1;
  if (a == 0)
    return 0;
  t x = 1;
  while (b > 0) {
    if (b & 1LL)
      x *= a;
    a *= a;
    b >>= 1LL;
  }
  return x;
}

template <typename t> constexpr t nCr(t n, int r) {
  if (r == 0)
    return 1;
  if (n == 0)
    return 0;
  if (n < r)
    return 0;
  t x = 1;
  for (int i = 1; i <= r; i++) {
    x *= n - i + 1;
    x /= i;
  }
  return x;
}

// 1-indexed
class BIT {
public:
  ll n;                              // データの長さ
  vector<ll> bit;                    // データの格納先
  BIT(ll n) : n(n), bit(n + 1, 0) {} // コンストラクタ

  // k & -kはLSB

  // bit_iにxをO(log(n))で加算する
  void add(ll i, ll x) {
    if (i == 0)
      return;
    for (ll k = i; k <= n; k += (k & -k)) {
      bit[k] += x;
    }
  }

  // bit_1 + bit_2 + …  + bit_n をO(log(n))で求める
  ll sum(ll i) {
    ll s = 0;
    if (i == 0)
      return s;
    for (ll k = i; k > 0; k -= (k & -k)) {
      s += bit[k];
    }
    return s;
  }

  // a_1 + a_2 + … + a_i >= x となるような最小のiを求める(a_k >= 0)
  // xが0以下の場合は該当するものなし→0を返す
  ll lower_bound(ll x) {
    if (x <= 0) {
      return 0;
    } else {
      ll i = 0;
      ll r = 1;
      // 最大としてありうる区間の長さを取得する
      // n以下の最小の二乗のべき(BITで管理する数列の区間で最大のもの)を求める
      while (r < n)
        r = r << 1;
      // 区間の長さは調べるごとに半分になる
      for (int len = r; len > 0; len = len >> 1) {
        // その区間を採用する場合
        if (i + len < n && bit[i + len] < x) {
          x -= bit[i + len];
          i += len;
        }
      }
      return i + 1;
    }
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, q;
  cin >> n >> q;
  vl c(n);
  rep(i, n) cin >> c[i];
  vector<tuple<ll, ll, ll>> rl(q);
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    rl[i] = mt(r, l, i);
  }
  sort(all(rl));
  vl Ans(q);
  vl C(n, -1);
  ll nr = 0;
  BIT B(n);
  rep(i, q) {
    ll r = get<0>(rl[i]);
    ll l = get<1>(rl[i]);
    ll ind = get<2>(rl[i]);
    rep2(j, nr, r) {
      if (C[c[j] - 1] > 0) {
        B.add(C[c[j] - 1], -1);
      }
      C[c[j] - 1] = j + 1;
      B.add(j + 1, 1);
    }
    nr = r;
    Ans[ind] = B.sum(r) - B.sum(l - 1);
  }
  rep(i, q) cout << Ans[i] << endl;
}

Error: 0

Fixed submission:

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

const int MOD1 = 1000000007;
const int MOD2 = 998244353;
const long double PI = 3.1415926535897932;
const long long MAXLL = 9223372036854775807;
const long long INF = 2305843009213693951;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};

#define ll long long
#define ull unsigned long long
#define ld long double
#define pll pair<long long, long long>
#define pcc pair<char, char>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vvvl vector<vector<vector<long long>>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vs vector<string>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<pair<long long, long long>>
#define umap unordered_map
#define uset unordered_set
#define Lqueue priority_queue<long long>
#define Squeue priority_queue<long long, vector<long long>, greater<long long>>
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define eb emplace_back

#define unless(a) if (!(a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rbf(a, x) for (auto &a : x)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)
#define bitrep(i, s, n) for (int i = (s); i < (1LL << (n)); i++)
#define bitcheck(bit, i) (bit) & (1LL << (i))
#define Maxe(x) *max_element((x).begin(), (x).end())
#define Mine(x) *min_element((x).begin(), (x).end())
#define Size(x) ((long long)(x).size())
#define Lin(s) getline(cin, (s))

void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; }
void YES(bool a) { cout << (a ? "YES" : "NO") << endl; }
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long cutup(long long a, long long b) { return (a + b - 1) / b; }
long long popcnt(long long a) { return __builtin_popcountll(a); }
template <class T, class U> inline bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename t> constexpr t mpow(t a, long long b) {
  if (b == 0)
    return 1;
  if (a == 0)
    return 0;
  t x = 1;
  while (b > 0) {
    if (b & 1LL)
      x *= a;
    a *= a;
    b >>= 1LL;
  }
  return x;
}

template <typename t> constexpr t nCr(t n, int r) {
  if (r == 0)
    return 1;
  if (n == 0)
    return 0;
  if (n < r)
    return 0;
  t x = 1;
  for (int i = 1; i <= r; i++) {
    x *= n - i + 1;
    x /= i;
  }
  return x;
}

// 1-indexed
class BIT {
public:
  ll n;                              // データの長さ
  vector<ll> bit;                    // データの格納先
  BIT(ll n) : n(n), bit(n + 1, 0) {} // コンストラクタ

  // k & -kはLSB

  // bit_iにxをO(log(n))で加算する
  void add(ll i, ll x) {
    if (i == 0)
      return;
    for (ll k = i; k <= n; k += (k & -k)) {
      bit[k] += x;
    }
  }

  // bit_1 + bit_2 + …  + bit_n をO(log(n))で求める
  ll sum(ll i) {
    ll s = 0;
    if (i == 0)
      return s;
    for (ll k = i; k > 0; k -= (k & -k)) {
      s += bit[k];
    }
    return s;
  }

  // a_1 + a_2 + … + a_i >= x となるような最小のiを求める(a_k >= 0)
  // xが0以下の場合は該当するものなし→0を返す
  ll lower_bound(ll x) {
    if (x <= 0) {
      return 0;
    } else {
      ll i = 0;
      ll r = 1;
      // 最大としてありうる区間の長さを取得する
      // n以下の最小の二乗のべき(BITで管理する数列の区間で最大のもの)を求める
      while (r < n)
        r = r << 1;
      // 区間の長さは調べるごとに半分になる
      for (int len = r; len > 0; len = len >> 1) {
        // その区間を採用する場合
        if (i + len < n && bit[i + len] < x) {
          x -= bit[i + len];
          i += len;
        }
      }
      return i + 1;
    }
  }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, q;
  cin >> n >> q;
  vc c(n);
  rep(i, n) cin >> c[i];
  vector<tuple<ll, ll, ll>> rl(q);
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    r--;
    l--;
    rl[i] = mt(r, l, i);
  }
  sort(all(rl));
  vl Ans(q);
  vl C(n, -1);
  ll nr = 0;
  BIT B(n + 1);
  rep(i, q) {
    ll r = get<0>(rl[i]);
    ll l = get<1>(rl[i]);
    ll ind = get<2>(rl[i]);
    rep2(j, nr, r + 1) {
      if (C[c[j] - '0' - 1] > 0) {
        B.add(C[c[j] - '0' - 1], -1);
      }
      C[c[j] - '0' - 1] = j + 1;
      B.add(j + 1, 1);
    }
    nr = r + 1;
    Ans[ind] = B.sum(r + 1) - B.sum(l);
  }
  rep(i, q) cout << Ans[i] << endl;
}