Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define PII pair<int, int>
using namespace std;
const int N = 5e5 + 5;
int a[N], rt[N];
int lc[N * 4], rc[N * 4], tot;
LL sum[N * 4];
map<int, int> vis;
void update(int &u, int x, int y, int p, LL v) {
  sum[tot] = sum[u] + v;
  lc[tot] = lc[u];
  rc[tot] = rc[u];
  u = tot++;
  if (x == y)
    return;
  int m = x + y >> 1;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
LL query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr)
    return sum[u];
  int m = (x + y) >> 1;
  LL ans = 0;
  if (ql <= m)
    ans += query(lc[u], x, m, ql, qr);
  if (qr > m)
    ans += query(rc[u], m + 1, y, ql, qr);
  return ans;
}
int main() {
  int n, q;
  tot++;
  vis.clear();
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (vis.find(a[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[a[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\n", query(rt[r], 1, n, l, r));
  }
  system("pause");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define PII pair<int, int>
using namespace std;
const int N = 5e5 + 5;
int a[N], rt[N];
int lc[N * 40], rc[N * 40], tot;
LL sum[N * 40];
map<int, int> vis;
void update(int &u, int x, int y, int p, LL v) {
  sum[tot] = sum[u] + v;
  lc[tot] = lc[u];
  rc[tot] = rc[u];
  u = tot++;
  if (x == y)
    return;
  int m = x + y >> 1;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
LL query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr)
    return sum[u];
  int m = (x + y) >> 1;
  LL ans = 0;
  if (ql <= m)
    ans += query(lc[u], x, m, ql, qr);
  if (qr > m)
    ans += query(rc[u], m + 1, y, ql, qr);
  return ans;
}
int main() {
  int n, q;
  tot++;
  vis.clear();
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (vis.find(a[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[a[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\n", query(rt[r], 1, n, l, r));
  }
  system("pause");
  return 0;
}