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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0), eps = 1e-8;
const int maxn = 5e5 + 10;
const ll mod = 1e9 + 7;
int a[maxn], rt[maxn];
int lc[maxn * 40], rc[maxn * 40], tol;
ll sum[maxn * 40];
map<int, int> vis;
void update(int &u, int x, int y, int p, ll v) {
  sum[tol] = sum[u] + v, lc[tol] = lc[u], rc[tol] = rc[u];
  u = tol++;
  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;
  tol = 1;
  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));
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0), eps = 1e-8;
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
int a[maxn], rt[maxn];
int lc[maxn * 40], rc[maxn * 40], tol;
ll sum[maxn * 40];
map<int, int> vis;
void init() {
  tol = 1;
  vis.clear();
}
void update(int &u, int x, int y, int p, ll v) {
  sum[tol] = sum[u] + v, lc[tol] = lc[u], rc[tol] = rc[u];
  u = tol++;
  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;
  init();
  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));
  }
  return 0;
}