Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 5e5 + 10;
const int inf = 0x3f3f3f3f;
struct node {
  int l, r, sum;
  node(int l = 0, int r = 0, int sum = 0) : l(l), r(r), sum(sum){};
} t[maxn * 50];
int root[maxn];
int tot;

void build(int &tar, int l, int r) {
  tar = ++tot;
  t[tar].sum = 0;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(t[tar].l, l, mid), build(t[tar].r, mid + 1, r);
}

void update(int pre, int &now, int l, int r, int pos, int val) {
  now = ++tot;
  t[now] = t[pre];
  t[now].sum += val;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (pos <= mid)
    update(t[pre].l, t[now].l, l, mid, pos, val);
  else
    update(t[pre].r, t[now].r, mid + 1, r, pos, val);
}

int query(int posl, int tar, int l, int r) {
  if (l == r)
    return t[tar].sum;
  int mid = l + r >> 1;
  if (posl <= mid)
    return t[t[tar].r].sum + query(posl, t[tar].l, l, mid);
  else
    return query(posl, t[tar].r, mid + 1, r);
}
map<int, int> vis;

int main() {
  close();
  int n, q;
  cin >> n >> q;

  build(root[0], 1, n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (!vis[x])
      vis[x] = i, update(root[i - 1], root[i], 1, n, i, 1);
    else
      update(root[i - 1], root[i], 1, n, vis[x], -1),
          vis[x] = i, update(root[i], root[i], 1, n, i, 1);
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    cout << query(l, root[r], 1, n) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
const ll mod = 1e9 + 7;
const int maxn = 5e5 + 10;
const int inf = 0x3f3f3f3f;
struct node {
  int l, r, sum;
  node(int l = 0, int r = 0, int sum = 0) : l(l), r(r), sum(sum){};
} t[maxn * 50];
int root[maxn];
int tot;

void build(int &tar, int l, int r) {
  tar = ++tot;
  t[tar].sum = 0;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(t[tar].l, l, mid), build(t[tar].r, mid + 1, r);
}

void update(int pre, int &now, int l, int r, int pos, int val) {
  now = ++tot;
  t[now] = t[pre];
  t[now].sum += val;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (pos <= mid)
    update(t[pre].l, t[now].l, l, mid, pos, val);
  else
    update(t[pre].r, t[now].r, mid + 1, r, pos, val);
}

int query(int posl, int tar, int l, int r) {
  if (l == r)
    return t[tar].sum;
  int mid = l + r >> 1;
  if (posl <= mid)
    return t[t[tar].r].sum + query(posl, t[tar].l, l, mid);
  else
    return query(posl, t[tar].r, mid + 1, r);
}
map<int, int> vis;

int main() {
  close();
  int n, q;
  scanf("%d%d", &n, &q);

  build(root[0], 1, n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (!vis[x])
      vis[x] = i, update(root[i - 1], root[i], 1, n, i, 1);
    else
      update(root[i - 1], root[i], 1, n, vis[x], -1),
          vis[x] = i, update(root[i], root[i], 1, n, i, 1);
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    cout << query(l, root[r], 1, n) << endl;
  }
}