Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;

const int mod = 1000000007;

template <class Abel> struct BIT {
  const Abel UNITY_SUM = 0; // to be set
  vector<Abel> dat;

  /* [1, n] */
  BIT(int n) : dat(n + 1, UNITY_SUM) {}
  void init(int n) { dat.assign(n + 1, UNITY_SUM); }

  /* a is 1-indexed */
  inline void add(int a, Abel x) {
    for (int i = a; i < (int)dat.size(); i += i & -i)
      dat[i] = dat[i] + x;
  }

  /* [1, a], a is 1-indexed */
  inline Abel sum(int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[i];
    return res;
  }

  /* [a, b), a and b are 1-indexed */
  inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }

  /* debug */
  void print() {
    for (int i = 1; i < (int)dat.size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) cin >> c[i];

  vector<int> ids(q);
  vector<P> p;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    ids[i] = i;
    p.emplace_back(l, r);
  }
  sort(ids.begin(), ids.end(),
       [&](int x, int y) { return p[x].second < p[y].second; });
  // sort(p.begin(), p.end(), [](P x, P y){
  //     return x.second < y.second;
  // });
  BIT<int> bit(n);
  map<int, int> last;
  int now = 0;
  vector<int> ans(n);
  rep(i, q) {
    P x = p[ids[i]];
    while (now <= x.second) {
      int ci = c[now];
      auto itr = last.find(ci);
      if (itr != last.end()) {
        int pre = last[ci];
        last[ci] = now;
        bit.add(pre + 1, -1);
        bit.add(now + 1, 1);

      } else {
        last[ci] = now;
        bit.add(now + 1, 1);
      }
      ++now;
    }
    int cnt = bit.sum(x.first + 1, x.second + 2);
    ans[ids[i]] = cnt;
  }
  rep(i, q) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll LINF = 1001002003004005006LL;
const int INF = 1001001001;

const int mod = 1000000007;

template <class Abel> struct BIT {
  const Abel UNITY_SUM = 0; // to be set
  vector<Abel> dat;

  /* [1, n] */
  BIT(int n) : dat(n + 1, UNITY_SUM) {}
  void init(int n) { dat.assign(n + 1, UNITY_SUM); }

  /* a is 1-indexed */
  inline void add(int a, Abel x) {
    for (int i = a; i < (int)dat.size(); i += i & -i)
      dat[i] = dat[i] + x;
  }

  /* [1, a], a is 1-indexed */
  inline Abel sum(int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[i];
    return res;
  }

  /* [a, b), a and b are 1-indexed */
  inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }

  /* debug */
  void print() {
    for (int i = 1; i < (int)dat.size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  rep(i, n) cin >> c[i];

  vector<int> ids(q);
  vector<P> p;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    ids[i] = i;
    p.emplace_back(l, r);
  }
  sort(ids.begin(), ids.end(),
       [&](int x, int y) { return p[x].second < p[y].second; });
  // sort(p.begin(), p.end(), [](P x, P y){
  //     return x.second < y.second;
  // });
  BIT<int> bit(n);
  map<int, int> last;
  int now = 0;
  vector<int> ans(q);
  rep(i, q) {
    P x = p[ids[i]];
    while (now <= x.second) {
      int ci = c[now];
      auto itr = last.find(ci);
      if (itr != last.end()) {
        int pre = last[ci];
        last[ci] = now;
        bit.add(pre + 1, -1);
        bit.add(now + 1, 1);

      } else {
        last[ci] = now;
        bit.add(now + 1, 1);
      }
      ++now;
    }
    int cnt = bit.sum(x.first + 1, x.second + 2);
    ans[ids[i]] = cnt;
  }
  rep(i, q) cout << ans[i] << endl;
  return 0;
}