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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

typedef long long int ll;
// #define int long long int
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define debug(x) cout << #x << " is: " << (x) << endl;
#define debugx(x)                                                              \\
  cout << #x << " is: " << (x) << endl;                                        \\
  exit(0);
#define f(i, n) for (ll i = 0; i < n; i++)
#define fa(i, a, n) for (ll i = a; i < n; i++)
#define loop(i, a, n) for (ll i = a; i <= n; i++)
#define loopb(i, a, n) for (ll i = a; i >= n; i--)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define setmem(x, k) memset(x, k, sizeof(x))
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define PI 3.1415926535897932384626
#define MOD 1000000007
#define INF 1000111000111000111LL
#define endl "\\n"
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<vector<ll>> matrix;
ll dx[] = {0, 1, 0, -1};
ll dy[] = {-1, 0, 1, 0};
const ll N = 5e5 + 5;
// ll a[N], b[N], dp[N], level[N], vis[N], in[N], out[N];
// vl g[N];
ll n, m;

// sqrt(n)
ll BLOCK = 555;

struct query {
  ll l;
  ll r;
  ll i;
};
query Q[N];
ll ar[N], ans[N];
ll freq[N];
ll cnt = 0;

bool compare(query a, query b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l / BLOCK < b.l / BLOCK;

  return a.r < b.r;
}

void add(ll pos) {
  freq[ar[pos]]++;
  if (freq[ar[pos]] == 1)
    cnt++;
}

void remove(ll pos) {
  freq[ar[pos]]--;
  if (freq[ar[pos]] == 0)
    cnt--;
}

void solve() {
  ll q;
  cin >> n >> q;
  f(i, n) cin >> ar[i];
  f(i, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].i = i;
    Q[i].l--, Q[i].r--;
  }
  sort(Q, Q + q, compare);

  ll ML = 0, MR = -1;
  f(i, q) {
    ll L = Q[i].l;
    ll R = Q[i].r;

    // Its always suggested to use in this method : R A A R

    // contraction
    while (ML < L) {
      remove(ML), ML++;
    }
    // expansion
    while (ML > L) {
      ML--, add(ML);
    }
    // expansion
    while (MR < R) {
      MR++, add(MR);
    }
    // contraction
    while (MR > R) {
      remove(MR), MR--;
    }

    ans[Q[i].i] = cnt;
  }

  f(i, q) cout << ans[i] << endl;
}

int32_t main() {
  IOS clock_t begin = clock();
  ll T = 1;
  // cin >> T;
  while (T--)
    solve();
  cerr << "Time elapsed : " << (clock() - begin) * 1000.0 / CLOCKS_PER_SEC
       << "ms" << '\\n';
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define DB(x) cout << #x << "=" << x << endl;

using namespace std;

const int MAXN = 5e5 + 5;

const int MAXS = 2e7;
int root[MAXN], root2[MAXN];

// Begin persistent segment tree
int lch[MAXS], rch[MAXS], sum2[MAXS], cnt;

int new_node(int val) {
  assert(cnt < MAXS);
  sum2[cnt] = val;
  return cnt++;
}

int new_node(int l, int r, bool s) {
  lch[cnt] = l;
  rch[cnt] = r;

  return new_node(s ? sum2[l] + sum2[r] : max(sum2[l], sum2[r]));
}

int build(int b, int e) {
  if (b == e)
    return new_node(0);

  int m = (b + e) / 2;
  return new_node(build(b, m), build(m + 1, e), false);
}

int update2(int v, int b, int e, int p, int val, bool s) {
  if (b > p || e < p)
    return v;

  if (b == e)
    return new_node(val);

  int m = (b + e) / 2;
  int l = update2(lch[v], b, m, p, val, s),
      r = update2(rch[v], m + 1, e, p, val, s);

  return new_node(l, r, s);
}

int query(int v, int b, int e, int l, int r, bool s) {
  if (e < l || b > r)
    return 0;

  if (l <= b && e <= r)
    return sum2[v];

  int m = (b + e) / 2;

  if (s)
    return query(lch[v], b, m, l, r, s) + query(rch[v], m + 1, e, l, r, s);
  else
    return max(query(lch[v], b, m, l, r, s), query(rch[v], m + 1, e, l, r, s));
}

// End persistent segment tree

int n, a[MAXN], next_pos[MAXN], forw[MAXN], q, l, r, last[MAXN];
int pos[MAXN];

void update(int r, int a, int b) {
  root2[a] = update2(r, 0, MAXN - 1, a, b, 0);
}

int dquery(int l, int r) {
  return query(root[r + 1], 0, MAXN - 1, l + 1, r + 1, 1);
}

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  cin >> q;

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

  root[0] = build(0, MAXN - 1);
  root2[n] = build(0, MAXN - 1);

  for (int i = 1; i <= n; ++i) {
    int r = root[i - 1];
    int a = ::a[i - 1];

    if (pos[a])
      r = update2(r, 0, MAXN - 1, pos[a], 0, 1);

    root[i] = update2(r, 0, MAXN - 1, i, 1, 1);
    pos[a] = i;
  }

  memset(forw, -1, sizeof forw);

  for (int i = n - 1; i >= 0; --i) {
    if (forw[a[i]] == -1) {
      update(root2[i + 1], i, MAXN - 2);
      next_pos[i] = MAXN - 2;
      last[i] = MAXN - 2;
    } else {
      int idx = forw[a[i]];
      int rot = update2(root2[i + 1], 0, MAXN - 1, idx, 0, 0);
      int r = next_pos[idx];
      next_pos[i] = idx;

      if (r == MAXN - 2 || r - idx != idx - i) {
        update(rot, i, r);
        last[i] = r;
      } else {
        update(rot, i, last[idx]);
        last[i] = last[idx];
      }
    }

    forw[a[i]] = i;
  }

  for (int i = 0; i < q; ++i) {
    cin >> l >> r;
    --l, --r;
    int ans = dquery(l, r);
    int cnt = query(root2[l], 0, MAXN - 1, l, r, 0);
    ans += cnt <= r;
    cout << ans << '\\n';
  }
  return 0;
}