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

// header{{{
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<boost/multiprecision/cpp_int.hpp>
// #include<boost/math/common_factor_rt.hpp>

using namespace std;
// using namespace boost::multiprecision;
// using namespace boost::math;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Fixed fixed << setprecision(12)
#define int int64_t
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A, B>;

inline int square(int a) { return a * a; }
inline int updiv(int a, int b) { return (a + b - 1) / b; }

constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
//}}}

struct Mo {

  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 <typename ADD, typename DEL, typename 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);
    }
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  vector<int> c(n), v(n + 1), res(q);

  rep(i, n) { cin >> c[i]; }

  Mo mo(n, q);

  rep(i, q) {
    int l, r;
    cin >> l >> r;
    mo.add(l - 1, r);
  }

  int ret = 0;
  auto add = [&](int p) {
    if (v[c[p]]++ == 0) {
      ret++;
    }
  };
  auto del = [&](int p) {
    if (v[c[p]]-- == 1) {
      ret--;
    }
  };
  auto rem = [&](int p) { res[p] = ret; };

  mo.run(add, del, rem);

  for (auto &&e : res) {
    cout << e << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

// header{{{
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<boost/multiprecision/cpp_int.hpp>
// #include<boost/math/common_factor_rt.hpp>

using namespace std;
// using namespace boost::multiprecision;
// using namespace boost::math;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Fixed fixed << setprecision(12)
#define int int64_t
using pii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A, B>;

inline int square(int a) { return a * a; }
inline int updiv(int a, int b) { return (a + b - 1) / b; }

constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1};
//}}}

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);
    }
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;

  vector<int> c(n), v(n + 1), res(q);

  rep(i, n) { cin >> c[i]; }

  Mo mo(n, q);

  rep(i, q) {
    int l, r;
    cin >> l >> r;
    mo.add(l - 1, r);
  }

  int ret = 0;
  auto add = [&](int p) {
    if (v[c[p]]++ == 0) {
      ret++;
    }
  };
  auto del = [&](int p) {
    if (v[c[p]]-- == 1) {
      ret--;
    }
  };
  auto rem = [&](int p) { res[p] = ret; };

  mo.run(add, del, rem);

  for (auto &&e : res) {
    cout << e << '\\n';
  }

  return 0;
}