Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}

const int N = 2e5 + 5;
int A[N], L[N], R[N];
set<int> dp[N][18];

int n, qq;
map<int, int> seen;

// fenwick
int fenw[N];
void upd(int k, int x) {
  for (int i = k; i <= n; i += i & -i) {
    fenw[i] += x;
  }
}

int get(int k) {
  int res = 0;
  for (int i = k; i; i -= i & -i) {
    res += fenw[i];
  }
  return res;
}

// 用BIT記錄i之前，distinct value的數量
// 如果在j位置(j<i)已經出現A[i]，先將j位置-1，在i位置+1
// fenw[i]=1，代表A[i]在位置i最後出現
// 將query按照[l,r]的r升冪排序，
// 再遍歷array A時，如果對到達的r，query BIT，計算distinct value
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> qq;
  REP(i, n) cin >> A[i];
  REP(i, qq) cin >> L[i] >> R[i];
  VI Q(qq), ans(qq);
  iota(ALL(Q), 0);
  sort(ALL(Q),
       [&](int i, int j) { return R[i] == R[j] ? L[i] < L[j] : R[i] < R[j]; });
  int k = 0;
  REP(i, n) {
    if (seen.count(A[i])) {
      upd(seen[A[i]], -1);
    }
    upd(i + 1, 1);
    seen[A[i]] = i + 1;
    while (k < qq && R[Q[k]] - 1 == i) {
      ans[Q[k]] = get(R[Q[k]]) - get(L[Q[k]] - 1);
      k++;
    }
  }
  for (int i : ans) {
    cout << i << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}

const int N = 5e5 + 5;
int A[N], L[N], R[N];
set<int> dp[N][18];

int n, qq;
map<int, int> seen;

// fenwick
int fenw[N];
void upd(int k, int x) {
  for (int i = k; i <= n; i += i & -i) {
    fenw[i] += x;
  }
}

int get(int k) {
  int res = 0;
  for (int i = k; i; i -= i & -i) {
    res += fenw[i];
  }
  return res;
}

// 用BIT記錄i之前，distinct value的數量
// 如果在j位置(j<i)已經出現A[i]，先將j位置-1，在i位置+1
// fenw[i]=1，代表A[i]在位置i最後出現
// 將query按照[l,r]的r升冪排序，
// 再遍歷array A時，如果對到達的r，query BIT，計算distinct value
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> qq;
  REP(i, n) cin >> A[i];
  REP(i, qq) cin >> L[i] >> R[i];
  VI Q(qq), ans(qq);
  iota(ALL(Q), 0);
  sort(ALL(Q),
       [&](int i, int j) { return R[i] == R[j] ? L[i] < L[j] : R[i] < R[j]; });
  int k = 0;
  REP(i, n) {
    if (seen.count(A[i])) {
      upd(seen[A[i]], -1);
    }
    upd(i + 1, 1);
    seen[A[i]] = i + 1;
    while (k < qq && R[Q[k]] - 1 == i) {
      ans[Q[k]] = get(R[Q[k]]) - get(L[Q[k]] - 1);
      k++;
    }
  }
  for (int i : ans) {
    cout << i << '\n';
  }
  return 0;
}