Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \
  do {                                                                         \
    cout << #var << " : ";                                                     \
    view(var);                                                                 \
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

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, false) {}

  // [l,r)
  void insert(int l, int 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];
      else
        return right[a] < right[b];
    });
  }

  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, q;
int a[500000];
int ans[500000];
int cnt[500001], sum;

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

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

int main() {
  scanf("%d %d", &n, &q);
  rep(i, n) scanf("%d", &a[i]);
  Mo mo(n);
  rep(i, q) {
    int x, y;
    scanf("%d %d", &x, &y);
    mo.insert(--x, y);
  }
  mo.build();
  rep(i, q) {
    int idx = mo.process();
    ans[idx] = sum;
  }
  rep(i, q) printf("%d\n", ans[i]);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \
  do {                                                                         \
    cout << #var << " : ";                                                     \
    view(var);                                                                 \
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

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, false) {}

  // [l,r)
  void insert(int l, int 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 bool((right[a] < right[b]) ^ (left[a] / width % 2));
    });
  }

  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, q;
int a[500000];
int ans[500000];
int cnt[500001], sum;

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

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

int main() {
  scanf("%d %d", &n, &q);
  rep(i, n) scanf("%d", &a[i]);
  Mo mo(n);
  rep(i, q) {
    int x, y;
    scanf("%d %d", &x, &y);
    mo.insert(--x, y);
  }
  mo.build();
  rep(i, q) {
    int idx = mo.process();
    ans[idx] = sum;
  }
  rep(i, q) printf("%d\n", ans[i]);
}