Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const int B = 500;

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

  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  rep(i, N) cin >> C[i];
  vector<int> L(Q), R(Q);
  rep(i, Q) cin >> L[i] >> R[i];

  rep(i, Q) L[i]--;

  vector<int> ord(Q);
  rep(i, Q) ord[i] = i;
  sort(ord.begin(), ord.end(), [&](int a, int b) {
    if ((L[a] / B) != (L[b] / B))
      return L[a] < L[b];
    else
      return R[a] < R[b];
  });

  vector<int> exi(N, 0);
  int pos = 0;
  int lef = 0, rig = 0;
  vector<int> cnt(N + 1, 0);
  int sum = 0;

  auto change = [&](int idx) {
    if (exi[idx]) {
      exi[idx] = 0;
      cnt[C[idx]]--;
      if (cnt[C[idx]] == 0)
        sum--;
    } else {
      exi[idx] = 1;
      cnt[C[idx]]++;
      if (cnt[C[idx]] == 1)
        sum++;
    }
  };

  auto go = [&]() {
    if (pos == ord.size())
      exit(0);
    int id = ord[pos];
    while (lef < L[id])
      change(lef++);
    while (rig < R[id])
      change(rig++);
    while (lef > L[id])
      change(--lef);
    while (rig > R[id])
      change(--rig);
    return ord[pos++];
  };

  vector<int> ans(Q, 0);
  for (int i = 0; i < Q; i++) {
    int res = go();
    ans[res] = sum;
  }

  rep(i, Q) cout << ans[i] << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const int B = 1500;

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

  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  rep(i, N) cin >> C[i];
  vector<int> L(Q), R(Q);
  rep(i, Q) cin >> L[i] >> R[i];

  rep(i, Q) L[i]--;

  vector<int> ord(Q);
  rep(i, Q) ord[i] = i;
  sort(ord.begin(), ord.end(), [&](int a, int b) {
    if ((L[a] / B) != (L[b] / B))
      return L[a] < L[b];
    else
      return R[a] < R[b];
  });

  vector<int> exi(N, 0);
  int pos = 0;
  int lef = 0, rig = 0;
  vector<int> cnt(N + 1, 0);
  int sum = 0;

  auto change = [&](int idx) {
    if (exi[idx]) {
      exi[idx] = 0;
      cnt[C[idx]]--;
      if (cnt[C[idx]] == 0)
        sum--;
    } else {
      exi[idx] = 1;
      cnt[C[idx]]++;
      if (cnt[C[idx]] == 1)
        sum++;
    }
  };

  auto go = [&]() {
    if (pos == ord.size())
      exit(0);
    int id = ord[pos];
    while (lef < L[id])
      change(lef++);
    while (rig < R[id])
      change(rig++);
    while (lef > L[id])
      change(--lef);
    while (rig > R[id])
      change(--rig);
    return ord[pos++];
  };

  vector<int> ans(Q, 0);
  for (int i = 0; i < Q; i++) {
    int res = go();
    ans[res] = sum;
  }

  rep(i, Q) cout << ans[i] << "\n";

  return 0;
}