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

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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (auto i = 0; i < n; i++)
#define Rep1(i, n) for (auto i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

#define BIT_MAX 500001

// Vector starting from 1
// index ==0 -> dummy, not used
vector<int> BIT(BIT_MAX + 1);

void add_BIT(int ix, const int &v) {
  while (ix <= BIT_MAX) {
    BIT.at(ix) += v;
    ix += ix & -ix;
  }
}

int sum_BIT(int ix) {
  int S = 0;
  while (ix > 0) {
    S += BIT.at(ix);
    ix -= ix & -ix;
  }
  return S;
}

void F(const int r, int &rr, const vector<int> &c, vector<int> &R) {
  while (rr < r) {
    rr++;
    int C = c.at(rr);
    if (0 != R.at(C))
      add_BIT(R.at(C), -1);
    add_BIT(rr, 1);
    R.at(C) = rr;
  }
  return;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N + 1, 0);
  Rep1(i, N) cin >> c.at(i);
  vector<vector<int>> q;
  Rep0(i, Q) {
    int l, r;
    cin >> l >> r;
    q.push_back({r, l, i});
  }
  Sort(q);
  //
  vector<int> R(N + 1, 0);
  vector<int> ANS(Q, 0);
  int r0 = 0;
  Rep0(i, Q) {
    int r = q.at(i).at(0);
    int l = q.at(i).at(1);
    int j = q.at(i).at(2);
    F(r, r0, c, R);
    ANS.at(j) = sum_BIT(r) - sum_BIT(l - 1);
  }
  Rep0(i, Q) cout << ANS.at(i) << endl;
  return 0;
}


Error: 0

Fixed submission:

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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (auto i = 0; i < n; i++)
#define Rep1(i, n) for (auto i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

#define BIT_MAX 500001

// Vector starting from 1
// index ==0 -> dummy, not used
vector<int> BIT(BIT_MAX + 1);

void add_BIT(int ix, const int &v) {
  while (ix <= BIT_MAX) {
    BIT.at(ix) += v;
    ix += ix & -ix;
  }
}

int sum_BIT(int ix) {
  int S = 0;
  while (ix > 0) {
    S += BIT.at(ix);
    ix -= ix & -ix;
  }
  return S;
}

void F(const int r, int &rr, const vector<int> &c, vector<int> &R) {
  while (rr < r) {
    rr++;
    int C = c.at(rr);
    if (0 != R.at(C))
      add_BIT(R.at(C), -1);
    add_BIT(rr, 1);
    R.at(C) = rr;
  }
  return;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N + 1, 0);
  Rep1(i, N) cin >> c.at(i);
  vector<vector<int>> q;
  Rep0(i, Q) {
    int l, r;
    cin >> l >> r;
    q.push_back({r, l, i});
  }
  Sort(q);
  //
  vector<int> R(N, 0);
  vector<int> ANS(Q, 0);
  int r0 = 0;
  Rep0(i, Q) {
    int r = q.at(i).at(0);
    int l = q.at(i).at(1);
    int j = q.at(i).at(2);
    F(r, r0, c, R);
    ANS.at(j) = sum_BIT(r) - sum_BIT(l - 1);
  }
  Rep0(i, Q) cout << ANS.at(i) << endl;
  return 0;
}