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

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

struct node {
  int x, y, z;
  bool operator<(const node &right) const {
    if (x == right.x) {
      if (y == right.y)
        return z < right.z;
      else
        return y < right.y;
    } else
      return x < right.x;
  }
};

vector<int> v;
int N;
inline void init(int m) {
  N = m;
  v.resize(m + 1, 0);
}
inline void add(int a, int x) {
  a++;
  for (int i = a; i <= N; i += i & -i)
    v[i] += x;
}
inline int sum(int a) {
  a++;
  int ret = 0;
  for (int i = a; i > 0; i -= i & -i)
    ret += v[i];
  return ret;
}
inline int sum(int a, int b) { return sum(b) - sum(a - 1); }

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n, q, l, r;
  cin >> n >> q;
  int c[n], num[n];
  memset(num, -1, sizeof(num));
  init(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<node> qu;
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    l--;
    r--;
    qu.push_back({r, l, i});
  }
  int ans[q];
  sort(be(qu));
  int idx = 0;
  for (int i = 0; i < n; i++) {
    if (num[c[i]] != -1)
      add(num[c[i]], -1);
    num[c[i]] = i;
    add(num[c[i]], 1);
    while (idx != q && qu[idx].x == i) {
      ans[qu[idx].z] = sum(qu[idx].y, qu[idx].x);
      idx++;
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007, INF = (1LL << 60);
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

struct Mo {
  vector<int> left, right, order;
  vector<bool> v;
  int width;
  int nl, nr, ptr;

  Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}

  void insert(int l, int r) /* [l, r) */
  {
    left.push_back(l);
    right.push_back(r);
  }

  /* ソート */
  void build() {
    order.resize(left.size());
    iota(begin(order), end(order), 0);
    sort(begin(order), end(order), [&](int a, int b) {
      if (left[a] / width != left[b] / width)
        return left[a] < left[b];
      return right[a] < right[b];
    });
  }

  /* クエリを 1 つぶんすすめて, クエリのidを返す */
  int process() {
    if (ptr == order.size())
      return (-1);
    const auto id = order[ptr];
    while (nl > left[id])
      distribute(--nl);
    while (nr < right[id])
      distribute(nr++);
    while (nl < left[id])
      distribute(nl++);
    while (nr > right[id])
      distribute(--nr);
    return (order[ptr++]);
  }

  inline void distribute(int idx) {
    v[idx].flip();
    if (v[idx])
      add(idx);
    else
      del(idx);
  }

  void add(int idx);

  void del(int idx);
};
int N, A[300000], Q;
int ans[200000];
int cnt[1000001], sum;

void Mo::add(int idx) {
  if (cnt[A[idx]]++ == 0)
    ++sum;
}

void Mo::del(int idx) {
  if (--cnt[A[idx]] == 0)
    --sum;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  scanf("%d", &Q);
  Mo mo(N);
  for (int i = 0; i < Q; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    mo.insert(--a, b);
  }
  mo.build();
  for (int i = 0; i < Q; i++) {
    ans[mo.process()] = sum;
  }
  for (int i = 0; i < Q; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}