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

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, a[N], tr[N], ans[N], vis[N];
struct node {
  int l, r, num;
} p[N];
bool cmp(node s1, node s2) { return s1.r < s2.r; }
void add(int i, int k) {
  while (i <= n) {
    tr[i] += k;
    i += (i & -i);
  }
}
int sum(int i) {
  int s = 0;
  while (i) {
    s += tr[i];
    i -= (i & -i);
  }
  return s;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++) {
    cin >> p[i].l >> p[i].r;
    p[i].num = i;
  }
  sort(p + 1, p + m + 1, cmp);
  int L = 1;
  for (int i = 1; i <= m; i++) {
    int num = p[i].num;
    int l = p[i].l;
    int r = p[i].r;
    for (int j = L; j <= r; j++) {
      int x = a[j];
      if (vis[x])
        add(vis[x], -1);
      add(j, 1);
      vis[x] = j;
    }
    L = r + 1;
    ans[num] = sum(r) - sum(l - 1);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: -11

Fixed submission:

// Problem: 2120  Time:556 ms  Memory:9508 kb
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define foru(i, a, b) for (int i = a; i <= b; ++i)
#define m(a, b) memset(a, b, sizeof a)
#define en '\\n'
using namespace std;
typedef long long ll;
const int N = 2e6 + 5, M = 2e7 + 5;
template <class T> void rd(T &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
struct Query {
  int id, l, r, tim;
} q[M];
struct Update {
  int pos, col, pre;
} u[M];
int c[N], las[N], cnt[N * 10];
int block, dexl, dexr, t, res, bl[N];
int ans[M];
inline int cmp(Query x, Query y) {
  if (bl[x.l] == bl[y.l])
    return (bl[x.r] == bl[y.r]) ? (x.tim < y.tim) : (bl[x.r] < bl[y.r]);
  return bl[x.l] < bl[y.l];
}
void modi_time(int t, int f) {
  if (f == 1) {
    c[u[t].pos] = u[t].col;
    if (dexl <= u[t].pos && u[t].pos <= dexr) {
      if ((--cnt[u[t].pre]) == 0)
        --res;
      if ((++cnt[u[t].col]) == 1)
        ++res;
    }
  } else {
    c[u[t].pos] = u[t].pre;
    if (dexl <= u[t].pos && u[t].pos <= dexr) {
      if ((--cnt[u[t].col]) == 0)
        --res;
      if ((++cnt[u[t].pre]) == 1)
        ++res;
    }
  }
}
void add(int x) {
  if ((++cnt[c[x]]) == 1)
    ++res;
}
void del(int x) {
  if ((--cnt[c[x]]) == 0)
    --res;
}
int main() {
  int n, m;
  rd(n), rd(m);
  block = sqrt(n);
  foru(i, 1, n) rd(c[i]), las[i] = c[i], bl[i] = (i - 1) / block + 1;
  int qq = 0, uu = 0;
  foru(i, 1, m) {
    char s[3];
    int x, y;
    scanf("%s%d%d", s, &x, &y);
    if (s[0] == 'Q')
      q[++qq] = (Query){qq, x, y, uu};
    else
      u[++uu] = (Update){x, y, las[x]}, las[x] = y;
  }
  sort(q + 1, q + qq + 1, cmp);
  dexl = 1, dexr = 0, t = 0, res = 0;
  foru(i, 1, m) {
    while (t < q[i].tim)
      modi_time(++t, 1);
    while (t > q[i].tim)
      modi_time(t--, -1);
    while (dexr < q[i].r)
      add(++dexr);
    while (dexl > q[i].l)
      add(--dexl);
    while (dexr > q[i].r)
      del(dexr--);
    while (dexl < q[i].l)
      del(dexl++);
    ans[q[i].id] = res;
  }
  foru(i, 1, qq) printf("%d\\n", ans[i]);
}