Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define dbg(x) cout << #x << ": " << x << endl
#define clr(x, v) memset(x, v, sizeof(x))
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const int MAXN = 5e5 + 5;

struct query {
  int l, r, idx;
};

int arr[MAXN], t[4 * MAXN], last[MAXN], ans[MAXN];
vector<query> queries;

int update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    t[v] += val;
  else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(2 * v, tl, tm, pos, val);
    else
      update(2 * v + 1, tm + 1, tr, pos, val);
    t[v] = t[2 * v] + t[2 * v + 1];
  }
}

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (tl == l && tr == r) {
    return t[v];
  } else {
    int tm = (tl + tr) / 2;
    return sum(2 * v, tl, tm, l, min(r, tm)) +
           sum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
  }
}

void cp() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    queries.pb({l, r, i});
  }
  sort(all(queries), [&](query a, query b) { return a.r < b.r; });

  clr(last, -1);

  int pos = 0;
  for (int i = 0; i < n; i++) {
    if (last[arr[i]] != -1)
      update(1, 0, n - 1, last[arr[i]], -1);
    // reduce 1 at pos last[arr[i]]
    last[arr[i]] = i;
    update(1, 0, n - 1, last[arr[i]], 1);
    while (pos < q && queries[pos].r == i)
      ans[queries[pos].idx] = sum(1, 0, n - 1, queries[pos].l, queries[pos].r),
      pos++;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

int main() {
  FASTIO;
  int t;
  t = 1;
  // cin >> t;
  while (t--) {
    cp();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pi pair<int, int>
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define ln(x) ((int)x.length())
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define dbg(x) cout << #x << ": " << x << endl
#define clr(x, v) memset(x, v, sizeof(x))
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const int MAXN = 5e5 + 5;

struct query {
  int l, r, idx;
};

int arr[MAXN], t[4 * MAXN], last[MAXN], ans[MAXN];
vector<query> queries;

void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    t[v] += val;
  else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(2 * v, tl, tm, pos, val);
    else
      update(2 * v + 1, tm + 1, tr, pos, val);
    t[v] = t[2 * v] + t[2 * v + 1];
  }
}

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (tl == l && tr == r) {
    return t[v];
  } else {
    int tm = (tl + tr) / 2;
    return sum(2 * v, tl, tm, l, min(r, tm)) +
           sum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
  }
}

void cp() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    queries.pb({l, r, i});
  }
  sort(all(queries), [&](query a, query b) { return a.r < b.r; });

  clr(last, -1);

  int pos = 0;
  for (int i = 0; i < n; i++) {
    if (last[arr[i]] != -1)
      update(1, 0, n - 1, last[arr[i]], -1);
    // reduce 1 at pos last[arr[i]]
    last[arr[i]] = i;
    update(1, 0, n - 1, last[arr[i]], 1);
    while (pos < q && queries[pos].r == i)
      ans[queries[pos].idx] = sum(1, 0, n - 1, queries[pos].l, queries[pos].r),
      pos++;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
}

int main() {
  FASTIO;
  int t;
  t = 1;
  // cin >> t;
  while (t--) {
    cp();
  }
  return 0;
}