Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int k = 1000;

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / k < b.first.first / k)
    return true;
  if (a.first.first / k == b.first.first / k && a.first.second < b.first.second)
    return true;
  return false;
}

int cnt[300001];
int a[300001];
int ans[300001];
int answ = 0;

void add(int i) {
  if (cnt[a[i]] == 0) {
    ++answ;
  }
  cnt[a[i]]++;
}

void del(int i) {
  if (cnt[a[i]] == 1) {
    --answ;
  }
  cnt[a[i]]--;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> f;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    f.push_back(a[i]);
  }
  sort(f.begin(), f.end());
  f.resize(unique(f.begin(), f.end()) - f.begin());
  unordered_map<int, int> d;
  for (int i = 0; i < f.size(); ++i) {
    d[f[i]] = i;
  }
  for (int i = 0; i < n; ++i) {
    a[i] = d[a[i]];
  }
  vector<pair<pair<int, int>, int>> qq;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    qq.push_back(make_pair(make_pair(l, r), i));
  }
  sort(qq.begin(), qq.end(), cmp);
  int L = 0, R = -1;
  for (int i = 0; i < q; ++i) {
    int l = qq[i].first.first;
    int r = qq[i].first.second;
    while (L > l)
      add(--L);
    while (R < r)
      add(++R);
    while (L < l)
      del(L++);
    while (R > r)
      del(R--);
    // cout << R  << " RR " << answ << endl;
    // cout << L << " LL " << answ << endl;
    ans[qq[i].second] = answ;
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\n';
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

int k = 1000;

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / k < b.first.first / k)
    return true;
  if (a.first.first / k == b.first.first / k && a.first.second < b.first.second)
    return true;
  return false;
}

int cnt[500001];
int a[500001];
int ans[500001];
int answ = 0;

void add(int i) {
  if (cnt[a[i]] == 0) {
    ++answ;
  }
  cnt[a[i]]++;
}

void del(int i) {
  if (cnt[a[i]] == 1) {
    --answ;
  }
  cnt[a[i]]--;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> f;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    f.push_back(a[i]);
  }
  sort(f.begin(), f.end());
  f.resize(unique(f.begin(), f.end()) - f.begin());
  unordered_map<int, int> d;
  for (int i = 0; i < f.size(); ++i) {
    d[f[i]] = i;
  }
  for (int i = 0; i < n; ++i) {
    a[i] = d[a[i]];
  }
  vector<pair<pair<int, int>, int>> qq;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    qq.push_back(make_pair(make_pair(l, r), i));
  }
  sort(qq.begin(), qq.end(), cmp);
  int L = 0, R = -1;
  for (int i = 0; i < q; ++i) {
    int l = qq[i].first.first;
    int r = qq[i].first.second;
    while (L > l)
      add(--L);
    while (R < r)
      add(++R);
    while (L < l)
      del(L++);
    while (R > r)
      del(R--);
    // cout << R  << " RR " << answ << endl;
    // cout << L << " LL " << answ << endl;
    ans[qq[i].second] = answ;
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\n';
  }
}