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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\\n'
#define rep(i, n) for (int i = 0; i < n; i++)
#define clr(arr) memset(arr, 0, sizeof arr)
#define all(x) x.begin(), x.end()
const int MOD = 1e9 + 7;

#ifdef SMIT
template <class F, class S>
ostream &operator<<(ostream &out, const pair<F, S> &p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  out << '{';
  for (auto i = x.begin(); i != x.end(); i++)
    out << *i << (i == prev(x.end()) ? "" : ", ");
  return out << '}';
}
void dmp() {}
template <class T, class... Args> void dmp(T &&x, Args... args) {
  cerr << x << ";  ";
  dmp(args...);
}
struct Nl {
  ~Nl() { cerr << '\\n'; }
};
#define debug(x...)                                                            \\
  cerr << (strcmp(#x, "") ? #x ":  " : ""), dmp(x), Nl(), cerr << ""
#else
#define debug(...)                                                             \\
  {}
#endif
// Yes you CAN

typedef long long ll;
// One-based indexing is used to construct this BIT
// So, ask for sum(1, n) instead of sum(0, n-1)
class BIT {
public:
  ll n;
  vector<ll> bit;
  BIT(vector<ll> &a) {
    this->n = a.size();
    bit.assign(n + 1, 0);
  }
  void update(ll index, ll value) {
    while (index <= n) {
      bit[index] += value;
      index += (index) & (-index);
    }
  }
  ll sum(ll index) {
    // returns ans, same as sum from 1 to index
    ll ans = 0;
    while (index > 0) {
      ans += bit[index];
      index -= (index) & (-index);
    }
    return ans;
  }
  ll sum(ll left, ll right) { return (sum(right) - sum(left - 1)); }
  void range_update(ll left, ll right, ll value) {
    // just like ordinary O(1) range update in array
    // In ordinary array, we first had to obtain difference array
    // Then, we do point updates at left and right+1
    // We then have to take prefix sum to get final range updates

    // But here in BIT, we know that sum will always stored as a prefix sum
    update(left, value);
    if (right != n)
      update(right + 1, -value);
  }
};
class Query {
public:
  int l, r, idx;
};
const int N = 5e5 + 5;

bool com(Query &q1, Query &q2) { return q1.r < q2.r; }
int n, q;
void solve() {
  // #define TEST
  cin >> n >> q;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<Query> qr(q);
  rep(i, q) {
    cin >> qr[i].l >> qr[i].r;
    qr[i].idx = i;
  }
  sort(all(qr), com);
  BIT bit(a);
  int last[N], ans[q], qp = 0;
  memset(last, -1, sizeof last);

  // debug(qr);
  // rep(i, n) cout << qr[i].r << ' ';
  // cout << endl;
  for (int i = 1; i <= n; i++) {
    if (last[a[i]] != -1)
      bit.update(last[a[i]], -1);
    last[a[i]] = i;
    bit.update(i, 1);
    while (qp < q and qr[qp].r == i) {
      debug(qp);
      ans[qr[qp].idx] = bit.sum(qr[qp].l, qr[qp].r);
      qp++;
    }
  }
  rep(i, q) cout << ans[i] << endl;
}

int32_t main() {
#ifdef SMIT
  // freopen("/home/smit/Desktop/in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
#ifdef TEST
  cin >> t;
#endif
  while (t--)
    solve();
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\\n'
#define rep(i, n) for (int i = 0; i < n; i++)
#define clr(arr) memset(arr, 0, sizeof arr)
#define all(x) x.begin(), x.end()
const int MOD = 1e9 + 7;

#ifdef SMIT
template <class F, class S>
ostream &operator<<(ostream &out, const pair<F, S> &p) {
  return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  out << '{';
  for (auto i = x.begin(); i != x.end(); i++)
    out << *i << (i == prev(x.end()) ? "" : ", ");
  return out << '}';
}
void dmp() {}
template <class T, class... Args> void dmp(T &&x, Args... args) {
  cerr << x << ";  ";
  dmp(args...);
}
struct Nl {
  ~Nl() { cerr << '\\n'; }
};
#define debug(x...)                                                            \\
  cerr << (strcmp(#x, "") ? #x ":  " : ""), dmp(x), Nl(), cerr << ""
#else
#define debug(...)                                                             \\
  {}
#endif
// Yes you CAN

typedef long long ll;
// One-based indexing is used to construct this BIT
// So, ask for sum(1, n) instead of sum(0, n-1)
class BIT {
public:
  ll n;
  vector<ll> bit;
  BIT(vector<ll> &a) {
    this->n = a.size();
    bit.assign(n + 1, 0);
  }
  void update(ll index, ll value) {
    while (index <= n) {
      bit[index] += value;
      index += (index) & (-index);
    }
  }
  ll sum(ll index) {
    // returns ans, same as sum from 1 to index
    ll ans = 0;
    while (index > 0) {
      ans += bit[index];
      index -= (index) & (-index);
    }
    return ans;
  }
  ll sum(ll left, ll right) { return (sum(right) - sum(left - 1)); }
  void range_update(ll left, ll right, ll value) {
    // just like ordinary O(1) range update in array
    // In ordinary array, we first had to obtain difference array
    // Then, we do point updates at left and right+1
    // We then have to take prefix sum to get final range updates

    // But here in BIT, we know that sum will always stored as a prefix sum
    update(left, value);
    if (right != n)
      update(right + 1, -value);
  }
};
class Query {
public:
  int l, r, idx;
};
const int N = 5e5 + 5;

bool com(Query &q1, Query &q2) { return q1.r < q2.r; }
int n, q;
void solve() {
  // #define TEST
  cin >> n >> q;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<Query> qr(q);
  rep(i, q) {
    cin >> qr[i].l >> qr[i].r;
    qr[i].idx = i;
  }
  sort(all(qr), com);
  BIT bit(a);
  int last[N], ans[n], qp = 0;
  memset(last, -1, sizeof last);

  // debug(qr);
  // rep(i, n) cout << qr[i].r << ' ';
  // cout << endl;
  for (int i = 1; i <= n; i++) {
    if (last[a[i]] != -1)
      bit.update(last[a[i]], -1);
    last[a[i]] = i;
    bit.update(i, 1);
    while (qp < q and qr[qp].r == i) {
      debug(qp);
      ans[qr[qp].idx] = bit.sum(qr[qp].l, qr[qp].r);
      qp++;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}

int32_t main() {
#ifdef SMIT
  // freopen("/home/smit/Desktop/in.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
#ifdef TEST
  cin >> t;
#endif
  while (t--)
    solve();
}