Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int max_n = 5 * (int)1e5 + 5;
#define mp make_pair
#define pii pair<int, int>

/* Zero indexed FT */
class FenwickTree {
  vector<int> ft;

public:
  FenwickTree(int n) { ft.resize(n + 1); }

  int rsq(int b);
  int rsq(int a, int b);
  void adjust(int ind, int to_add);
};

int FenwickTree ::rsq(int b) {
  int sum = 0;
  for (; b > 0; b -= (b & -b))
    sum += ft[b];

  return sum;
}

int FenwickTree ::rsq(int a, int b) {
  a++;
  b++;
  return rsq(b) - (a > 0 ? rsq(a - 1) : 0);
}

void FenwickTree ::adjust(int ind, int to_add) {
  ind++;
  for (; ind < (int)ft.size(); ind += (ind & -ind))
    ft[ind] += to_add;
}

void solve() {
  int n, q_len;
  cin >> n >> q_len;

  vector<int> a(n);
  for (auto &ele : a)
    cin >> ele;

  vector<pair<pii, int>> queries;
  for (int ind = 0; ind < q_len; ind++) {
    int left, right;
    cin >> left >> right;
    left--;
    right--;
    queries.push_back(mp(mp(right, left), ind));
  }

  vector<int> ans(n);
  vector<int> last_occ(max_n, -1);

  FenwickTree ft(n);
  for (int i = 0; i < n; i++)
    ft.adjust(i, 1);

  sort(queries.begin(), queries.end());
  auto itr = queries.begin();

  for (int i = 0; i < n; i++) {
    if (last_occ[a[i]] != -1)
      ft.adjust(last_occ[a[i]], -1);

    while (itr != queries.end() and itr->first.first == i) {
      int r = (itr->first).first;
      int l = (itr->first).second;
      int q_ind = itr->second;

      ans[q_ind] = ft.rsq(l, r);
      itr++;
    }

    last_occ[a[i]] = i;
  }

  for (auto &ele : ans)
    cout << ele << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  solve();
  return 0;
}


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

const int max_n = 5 * (int)1e5 + 5;
#define mp make_pair
#define pii pair<int, int>

/* Zero indexed FT */
class FenwickTree {
  vector<int> ft;

public:
  FenwickTree(int n) { ft.resize(n + 1); }

  int rsq(int b);
  int rsq(int a, int b);
  void adjust(int ind, int to_add);
};

int FenwickTree ::rsq(int b) {
  int sum = 0;
  for (; b > 0; b -= (b & -b))
    sum += ft[b];

  return sum;
}

int FenwickTree ::rsq(int a, int b) {
  a++;
  b++;
  return rsq(b) - (a > 0 ? rsq(a - 1) : 0);
}

void FenwickTree ::adjust(int ind, int to_add) {
  ind++;
  for (; ind < (int)ft.size(); ind += (ind & -ind))
    ft[ind] += to_add;
}

void solve() {
  int n, q_len;
  cin >> n >> q_len;

  vector<int> a(n);
  for (auto &ele : a)
    cin >> ele;

  vector<pair<pii, int>> queries;
  for (int ind = 0; ind < q_len; ind++) {
    int left, right;
    cin >> left >> right;
    left--;
    right--;
    queries.push_back(mp(mp(right, left), ind));
  }

  vector<int> ans(q_len);
  vector<int> last_occ(max_n, -1);

  FenwickTree ft(n);
  for (int i = 0; i < n; i++)
    ft.adjust(i, 1);

  sort(queries.begin(), queries.end());
  auto itr = queries.begin();

  for (int i = 0; i < n; i++) {
    if (last_occ[a[i]] != -1)
      ft.adjust(last_occ[a[i]], -1);

    while (itr != queries.end() and itr->first.first == i) {
      int r = (itr->first).first;
      int l = (itr->first).second;
      int q_ind = itr->second;

      ans[q_ind] = ft.rsq(l, r);
      itr++;
    }

    last_occ[a[i]] = i;
  }

  for (auto &ele : ans)
    cout << ele << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  solve();
  return 0;
}