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

#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Aug  2 21:38:01 JST 2020
 **/

using namespace std;
struct Mo {
  // using ADD = function<void(int)>;
  // using DEL = function<void(int)>;
  // using REM = function<void(int)>;

  int width;
  vector<int> left, right, order;
  vector<bool> v;

  Mo(int N, int Q) : width((int)sqrt(N)), order(Q), v(N) {
    iota(begin(order), end(order), 0);
  }

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

  template <class ADD, class DEL, class REM>
  void run(const ADD &add, const DEL &del, const REM &rem) {
    assert(left.size() == order.size());
    sort(begin(order), end(order), [&](int a, int b) {
      int ablock = left[a] / width, bblock = left[b] / width;
      if (ablock != bblock)
        return ablock < bblock;
      if (ablock & 1)
        return right[a] < right[b];
      return right[a] > right[b];
    });
    int nl = 0, nr = 0;
    auto push = [&](int idx) {
      v[idx].flip();
      if (v[idx])
        add(idx);
      else
        del(idx);
    };
    for (auto idx : order) {
      while (nl > left[idx])
        push(--nl);
      while (nr < right[idx])
        push(nr++);
      while (nl < left[idx])
        push(nl++);
      while (nr > right[idx])
        push(--nr);
      rem(idx);
    }
  }
};

constexpr Int K = (Int)5e5 + 1;
Int a[K], ans[K], cnt[K];
signed main() {
  Int n, Q;
  std::cin >> n >> Q;
  REP(i, n) std::cin >> a[i];
  Mo mo(n, Q);
  REP(q, Q) {
    Int l, r;
    std::cin >> l >> r, l--;
    mo.add(l, r);
  }
  Int cur = 0;
  auto add = [&](Int i) {
    if (cnt[a[i]]++ == 0)
      cur++;
  };
  auto del = [&](Int i) {
    if (--cnt[a[i]] == 0)
      cur--;
  };
  auto rem = [&](Int i) { ans[i] = cur; };
  mo.run(add, del, rem);
  REP(q, Q) std::cout << ans[q] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Aug  2 21:38:01 JST 2020
 **/

using namespace std;
struct Mo {
  using ADD = function<void(int)>;
  using DEL = function<void(int)>;
  using REM = function<void(int)>;

  int width;
  vector<int> left, right, order;
  vector<bool> v;

  Mo(int N, int Q) : width((int)sqrt(N)), order(Q), v(N) {
    iota(begin(order), end(order), 0);
  }

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

  void run(const ADD &add, const DEL &del, const REM &rem) {
    assert(left.size() == order.size());
    sort(begin(order), end(order), [&](int a, int b) {
      int ablock = left[a] / width, bblock = left[b] / width;
      if (ablock != bblock)
        return ablock < bblock;
      if (ablock & 1)
        return right[a] < right[b];
      return right[a] > right[b];
    });
    int nl = 0, nr = 0;
    auto push = [&](int idx) {
      v[idx].flip();
      if (v[idx])
        add(idx);
      else
        del(idx);
    };
    for (auto idx : order) {
      while (nl > left[idx])
        push(--nl);
      while (nr < right[idx])
        push(nr++);
      while (nl < left[idx])
        push(nl++);
      while (nr > right[idx])
        push(--nr);
      rem(idx);
    }
  }
};

constexpr Int K = (Int)5e5 + 1;
Int a[K], ans[K], cnt[K];
signed main() {
  Int n, Q;
  std::cin >> n >> Q;
  REP(i, n) std::cin >> a[i];
  Mo mo(n, Q);
  REP(q, Q) {
    Int l, r;
    std::cin >> l >> r, l--;
    mo.add(l, r);
  }
  Int cur = 0;
  auto add = [&](Int i) {
    if (cnt[a[i]]++ == 0)
      cur++;
  };
  auto del = [&](Int i) {
    if (--cnt[a[i]] == 0)
      cur--;
  };
  auto rem = [&](Int i) { ans[i] = cur; };
  mo.run(add, del, rem);
  REP(q, Q) std::cout << ans[q] << "\\n";
}