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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

template <class Abel> struct BIT {
  vector<Abel> dat[2];
  Abel UNITY_SUM = 0; // to be set

  /* [1, n] */
  BIT(int n) { init(n); }
  void init(int n) {
    for (int iter = 0; iter < 2; ++iter)
      dat[iter].assign(n + 1, UNITY_SUM);
  }

  /* a, b are 1-indexed, [a, b) */
  inline void sub_add(int p, int a, Abel x) {
    for (int i = a; i < (int)dat[p].size(); i += i & -i)
      dat[p][i] = dat[p][i] + x;
  }
  inline void add(int a, int b, Abel x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  /* a is 1-indexed, [a, b) */
  inline Abel sub_sum(int p, int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[p][i];
    return res;
  }
  inline Abel sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }

  /* debug */
  void print() {
    for (int i = 1; i < (int)dat[0].size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int N;
  cin >> N;
  int Q;
  cin >> Q;

  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];

  vector<int> lefts(Q), rights(Q), ids(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> lefts[i] >> rights[i];
    --lefts[i];
    ids[i] = i;
  }
  sort(ids.begin(), ids.end(),
       [&](int i, int j) { return rights[i] < rights[j]; });

  BIT<int> bit(N + 5);
  vector<int> prev(1100000, -1);
  vector<int> res(Q, 0);
  int r = 0;
  for (auto i : ids) {
    for (; r < rights[i]; ++r) {
      bit.add(prev[a[r]] + 2, r + 2, 1);
      prev[a[r]] = r;
    }
    int tmp = bit.sum(lefts[i] + 1, lefts[i] + 2);
    res[i] = max(res[i], tmp);
  }
  for (int i = 0; i < Q; ++i)
    cout << res[i] << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define sar(a, n)                                                              \\
  cout << #a << ":";                                                           \\
  rep(pachico, n) cout << " " << a[pachico];                                   \\
  cout << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(pachico, v.size()) cout << " " << v[pachico];                            \\
  cout << endl
#define svecp(v)                                                               \\
  cout << #v << ":";                                                           \\
  each(pachico, v) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(pachico, s) cout << " " << pachico;                                     \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(pachico, m) cout << " {" << pachico.first << ":" << pachico.second      \\
                        << "}";                                                \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

#define getchar getchar_unlocked
#define putchar putchar_unlocked

inline int in() {
  int n, c;
  while ((c = getchar()) < '0')
    if (c == EOF)
      return -1;
  n = c - '0';
  while ((c = getchar()) >= '0')
    n = n * 10 + c - '0';
  return n;
}

inline void out(int n) {
  short res[11], i = 0;
  do {
    res[i++] = n % 10, n /= 10;
  } while (n);
  while (i)
    putchar(res[--i] + '0');
  putchar('\\n');
}

vi pr[MAX_N], id[MAX_N];
bool is_prime[MAX_N];

void all_fac() {
  fill(is_prime, is_prime + MAX_N, true);
  for (int i = 2; i < MAX_N; i++) {
    if (is_prime[i]) {
      pr[i].pb(i);
      id[i].pb(1);
      for (int j = 2 * i; j < MAX_N; j += i) {
        is_prime[j] = false;
        int cnt = 0;
        int nw = j;
        while (nw % i == 0) {
          nw /= i;
          cnt++;
        }
        pr[j].pb(i);
        id[j].pb(cnt);
      }
    }
  }
}

int inv[20 * MAX_N];

void make() {
  inv[1] = 1;
  for (int i = 2; i < 20 * MAX_N; i++) {
    inv[i] = MOD - (ll)inv[MOD % i] * (MOD / i) % MOD;
  }
}

inline int add(int x, int y) { return (x + y) % MOD; }

inline int sub(int x, int y) { return (x + MOD - y) % MOD; }

inline int mul(int x, int y) { return (ll)x * y % MOD; }

int res;
int cnt[MAX_N], a[MAX_N];

class Mo {
private:
  vector<int> left, right;
  int w;
  void add(int id);
  void del(int id);

public:
  vector<int> ans;

  Mo(int n) : w(600) {}
  // クエリ[l,r)
  void insert(int l, int r) { left.push_back(l), right.push_back(r); }
  void solve() {
    int sz = (int)left.size();
    int nl = 0, nr = 0;
    vector<int> ord(sz);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int a, int b) {
      const int c = left[a] / w, d = left[b] / w;
      return (c == d)
                 ? ((c & 1) ? (right[a] < right[b]) : (right[b] < right[a]))
                 : (c < d);
    });
    ans.resize(sz);
    for (int i = 0; i < sz; i++) {
      const int id = ord[i]; // order:クエリのsort後のorder→元のorder
      while (nl > left[id])
        add(--nl);
      while (nr < right[id])
        add(nr++); // add
      while (nl < left[id])
        del(nl++);
      while (nr > right[id])
        del(--nr); // del
      ans[id] = res;
    }
  }
};

// idは元の配列のインデックス
void Mo::add(int index) {
  if (cnt[a[index]]++ == 0)
    res++;
}

void Mo::del(int index) {
  if (--cnt[a[index]] == 0)
    --res;
}

int main() {
  int n = in(), q = in();
  rep(i, n) { a[i] = in(); }
  make();
  all_fac();
  Mo mo(n);
  rep(i, q) {
    int l = in(), r = in();
    mo.insert(l - 1, r);
  }
  rep(i, MAX_N) { cnt[i] = 1; }
  res = 1;
  mo.solve();
  rep(i, q) { out(mo.ans[i]); }
  return 0;
}