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

/* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;

template <int mod> struct modint {
  int x;

  modint() : x(0) {}

  modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  modint &operator+=(const modint &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  modint &operator-=(const modint &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  modint &operator*=(const modint &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  modint &operator/=(const modint &p) {
    *this *= p.inverse();
    return *this;
  }

  modint operator-() const { return modint(-x); }

  modint operator+(const modint &p) const { return modint(*this) += p; }

  modint operator-(const modint &p) const { return modint(*this) -= p; }

  modint operator*(const modint &p) const { return modint(*this) *= p; }

  modint operator/(const modint &p) const { return modint(*this) /= p; }

  bool operator==(const modint &p) const { return x == p.x; }

  bool operator!=(const modint &p) const { return x != p.x; }

  modint inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return modint(u);
  }

  modint pow(int64_t n) const {
    modint ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }

  friend istream &operator>>(istream &is, modint &a) {
    int64_t t;
    is >> t;
    a = modint<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...)                                            \\
  for (int i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x)                                                              \\
  rep(i, x.size()) cout << x[i] << ' ';                                        \\
  cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T mypow(T x, ll n) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x);
    (x *= x);
    n >>= 1;
  }
  return ret;
}
ll modpow(ll x, ll n, const ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x);
    (x *= x);
    n >>= 1;
    x %= mod;
    ret %= mod;
  }
  return ret;
}
template <typename T> uint64_t my_rand(void) {
  static uint64_t x = 88172645463325252ULL;
  x = x ^ (x << 13);
  x = x ^ (x >> 7);
  return x = x ^ (x << 17);
}
int popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  bool operator<(const edge<T> &r) const { return cost < r.cost; }

  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
  clock_t start_time;
  void start() { start_time = clock(); }
  int lap() {
    // return x ms.
    return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
  }
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library

int cnt[500003];
int res = 0;
struct Mo {
  // 区間に入れる際に呼ぶ関数. 配列のidxを受け取る
  // using ADD = function< void(int) >;
  // 区間から出る際に呼ぶ関数. 配列のidxを受け取る
  // using DEL = function< void(int) >;
  // 答えを格納する関数. クエリのidxを受け取る
  // 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);
  }
  // クエリをソート順に処理して答えをremする
  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);
    }
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << setprecision(20);
  int n, q;
  scanf("%d", &n);
  scanf("%d", &q);
  vi c(n);
  rep(i, n) scanf("%d", &c[i]);
  Mo mo(n, q);
  vi ans(q);
  rep(i, q) {
    int l, r;
    scanf("%d", &l);
    scanf("%d", &r);
    mo.add(l - 1, r);
  }
  auto f = [&](int i) {
    if (!cnt[c[i]])
      ++res;
    ++cnt[c[i]];
  };
  auto g = [&](int i) {
    if (cnt[c[i]])
      --cnt[c[i]];
    if (!cnt[c[i]])
      --res;
  };
  auto h = [&](int i) { ans[i] = res; };
  mo.run(f, g, h);
  rep(i, q) printf("%d\\n", ans[i]);
}


Error: TLE

Fixed submission:

/* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;

template <int mod> struct modint {
  int x;

  modint() : x(0) {}

  modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  modint &operator+=(const modint &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  modint &operator-=(const modint &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  modint &operator*=(const modint &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  modint &operator/=(const modint &p) {
    *this *= p.inverse();
    return *this;
  }

  modint operator-() const { return modint(-x); }

  modint operator+(const modint &p) const { return modint(*this) += p; }

  modint operator-(const modint &p) const { return modint(*this) -= p; }

  modint operator*(const modint &p) const { return modint(*this) *= p; }

  modint operator/(const modint &p) const { return modint(*this) /= p; }

  bool operator==(const modint &p) const { return x == p.x; }

  bool operator!=(const modint &p) const { return x != p.x; }

  modint inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return modint(u);
  }

  modint pow(int64_t n) const {
    modint ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }

  friend istream &operator>>(istream &is, modint &a) {
    int64_t t;
    is >> t;
    a = modint<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...)                                            \\
  for (int i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x)                                                              \\
  rep(i, x.size()) cout << x[i] << ' ';                                        \\
  cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T mypow(T x, ll n) {
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x);
    (x *= x);
    n >>= 1;
  }
  return ret;
}
ll modpow(ll x, ll n, const ll mod) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x);
    (x *= x);
    n >>= 1;
    x %= mod;
    ret %= mod;
  }
  return ret;
}
template <typename T> uint64_t my_rand(void) {
  static uint64_t x = 88172645463325252ULL;
  x = x ^ (x << 13);
  x = x ^ (x >> 7);
  return x = x ^ (x << 17);
}
int popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  bool operator<(const edge<T> &r) const { return cost < r.cost; }

  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
  clock_t start_time;
  void start() { start_time = clock(); }
  int lap() {
    // return x ms.
    return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
  }
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library

int cnt[500003];
int res = 0;
struct Mo {
  // 区間に入れる際に呼ぶ関数. 配列のidxを受け取る
  using ADD = function<void(int)>;
  // 区間から出る際に呼ぶ関数. 配列のidxを受け取る
  using DEL = function<void(int)>;
  // 答えを格納する関数. クエリのidxを受け取る
  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);
  }
  // クエリをソート順に処理して答えをremする
  int 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);
    }
    return 0;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << setprecision(20);
  int n, q;
  scanf("%d", &n);
  scanf("%d", &q);
  vi c(n);
  rep(i, n) scanf("%d", &c[i]);
  Mo mo(n, q);
  vi ans(q);
  rep(i, q) {
    int l, r;
    scanf("%d", &l);
    scanf("%d", &r);
    mo.add(l - 1, r);
  }
  auto f = [&](int i) {
    if (!cnt[c[i]])
      ++res;
    ++cnt[c[i]];
  };
  auto g = [&](int i) {
    if (cnt[c[i]])
      --cnt[c[i]];
    if (!cnt[c[i]])
      --res;
  };
  auto h = [&](int i) { ans[i] = res; };
  mo.run(f, g, h);
  rep(i, q) printf("%d\\n", ans[i]);
}