Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \\
  cerr << __LINE__ << ": " << #x << " = {";                                    \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \\
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

struct Mo {
  static constexpr int MAX_N = 512345;
  int left[MAX_N], right[MAX_N], order[MAX_N];
  int width;
  int nl, nr, ptr, li, sz;
  Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), li(0) {}
  /* [l, r) */
  void insert(int l, int r) {
    left[li] = l;
    right[li++] = r;
  }

  void build() {
    iota(order, order + li, 0);
    sort(order, order + li, [&](int a, int b) {
      const int ab = left[a] / width, bb = left[b] / width;
      return ab != bb ? ab < bb
                      : (ab & 1 ? right[a] < right[b] : right[b] < right[a]);
    });
  }

  inline int process() {
    if (ptr == li)
      return (-1);
    const auto id = order[ptr];
    while (nl > left[id])
      add(--nl);
    while (nr < right[id])
      add(nr++);
    while (nl < left[id])
      del(nl++);
    while (nr > right[id])
      del(--nr);
    return (order[ptr++]);
  }
  inline void add(int idx);
  inline void del(int idx);
};

int c[512345];
int cnt[512345];
int sum;

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

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

int ans[512345];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    cin >> c[i];
  Mo mo(N);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    mo.insert(--l, r);
  }
  mo.build();
  for (int i = 0; i < Q; i++) {
    int idx = mo.process();
    ans[idx] = sum;
  }
  for (int i = 0; i < Q; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                                                       \\
  cerr << __LINE__ << ": " << #x << " = {";                                    \\
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \\
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \\
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

struct Mo {
  static constexpr int MAX_N = 512345;
  int left[MAX_N], right[MAX_N], order[MAX_N];
  int width;
  int nl, nr, ptr, li, sz;
  Mo(int n) : width((int)sqrt(n) + 2), nl(0), nr(0), ptr(0), li(0) {}
  /* [l, r) */
  void insert(int l, int r) {
    left[li] = l;
    right[li++] = r;
  }

  void build() {
    iota(order, order + li, 0);
    sort(order, order + li, [&](int a, int b) {
      const int ab = left[a] / width, bb = left[b] / width;
      return ab != bb ? ab < bb
                      : (ab & 1 ? right[a] < right[b] : right[b] < right[a]);
    });
  }

  inline int process() {
    if (ptr == li)
      return (-1);
    const auto id = order[ptr];
    while (nl > left[id])
      add(--nl);
    while (nr < right[id])
      add(nr++);
    while (nl < left[id])
      del(nl++);
    while (nr > right[id])
      del(--nr);
    return (order[ptr++]);
  }
  inline void add(int idx);
  inline void del(int idx);
};

int c[512345];
int cnt[512345];
int sum;

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

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

int ans[512345];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    cin >> c[i];
  Mo mo(N);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    mo.insert(--l, r);
  }
  mo.build();
  for (int i = 0; i < Q; i++) {
    int idx = mo.process();
    ans[idx] = sum;
  }
  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;
  return 0;
}