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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

struct BIT {
  int N;
  vector<int> bit;
  void add(int x, int y) {
    while (x <= N) {
      bit[x] += y;
      x += x & -x;
    }
  }
  int sum(int x) {
    int res = 0;
    while (x > 0) {
      res += bit[x];
      x -= x & -x;
    }
    return res;
  }
  BIT(int x) : N(x), bit(x + 1) {}
};

int N, Q;
int A[500005];
pair<P, int> q[500005];
int las[500005];
int ans[500005];
signed main() {
  cin >> N >> Q;
  rep(i, N) cin >> A[i];
  rep(i, Q) {
    cin >> q[i].first.second >> q[i].first.first;
    q[i].first.second--;
    q[i].first.first--;
    q[i].second = i;
  }
  int idx = 0;
  sort(q, q + Q);
  BIT bit(N + 10);
  fill(las, las + N + 1, -1);
  rep(i, N) {
    if (las[A[i]] != -1) {
      bit.add(las[A[i]] + 1, -1);
    }
    las[A[i]] = i;
    bit.add(i + 1, 1);
    while (idx < Q && q[idx].first.first <= i) {
      ans[q[idx].second] =
          bit.sum(q[idx].first.first + 1) - bit.sum(q[idx].first.second);
      idx++;
    }
  }
  rep(i, Q) cout << ans[i] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
// #define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
// constexpr int inf = 3e18;

struct Mo {
  vector<int> left, right, order;
  vector<bool> v;
  int width;
  int nl, nr, ptr;

  Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), v(n) {}

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

  void build() {
    order.resize(left.size());
    iota(begin(order), end(order), 0);
    sort(begin(order), end(order), [&](int a, int b) {
      if (left[a] / width != left[b] / width)
        return left[a] < left[b];
      return right[a] < right[b];
    });
  }

  /* クエリを 1 つぶんすすめて, クエリのidを返す */
  int process() {
    if (ptr == order.size())
      return (-1);
    const auto id = order[ptr];
    while (nl > left[id])
      distribute(--nl);
    while (nr < right[id])
      distribute(nr++);
    while (nl < left[id])
      distribute(nl++);
    while (nr > right[id])
      distribute(--nr);
    return (order[ptr++]);
  }

  inline void distribute(int idx) {
    v[idx].flip();
    if (v[idx])
      add(idx);
    else
      del(idx);
  }

  void add(int idx);

  void del(int idx);
};

int N, A[500005], Q;
int ans[500005];
int cnt[500005], sum;

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

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  rep(i, N) { cin >> A[i]; }
  Mo mo(N);
  rep(i, Q) {
    int a, b;
    cin >> a >> b;
    mo.insert(--a, b);
  }
  mo.build();
  rep(i, Q) {
    int j = mo.process();
    ans[j] = sum;
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << "\\n";
  }
}