Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 500005, INF = 1 << 30, D = 3;
int N, Q;
int A[MAX], cnt[MAX], ans[MAX];
int sum = 0, l = -1, r = -1;

struct dat {
  int l;
  int r;
  int id;
};

vector<dat> S;

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> Q;

  for (int i = 0; i < N; i++)
    cin >> A[i];

  S.resize(Q);
  for (int i = 0; i < Q; i++) {
    cin >> S[i].l >> S[i].r;
    S[i].id = i;
    S[i].l--;
    S[i].r--;
  }

  sort(all(S), [](auto a, auto b) {
    if (a.l / D == b.l / D)
      return a.r < b.r;
    return a.l / D < b.l / D;
  });

  for (int i = 0; i < Q; i++) {
    while (S[i].r < r) {
      cnt[A[r]]--;
      if (cnt[A[r]] == 0)
        sum--;
      r--;
    }
    while (S[i].r > r) {
      cnt[A[r + 1]]++;
      if (cnt[A[r + 1]] == 1)
        sum++;
      r++;
    }

    while (S[i].l < l) {
      cnt[A[l - 1]]++;
      if (cnt[A[l - 1]] == 1)
        sum++;
      l--;
    }
    while (S[i].l > l) {
      cnt[A[l]]--;
      if (cnt[A[l]] == 0)
        sum--;
      l++;
    }

    ans[S[i].id] = sum;
  }

  for (int i = 0; i < Q; i++)
    cout << ans[i] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 500005, INF = 1 << 30, D = 700;
int N, Q;
int A[MAX], cnt[MAX], ans[MAX];
int sum = 0, l = -1, r = -1;

struct dat {
  int l;
  int r;
  int id;
};

vector<dat> S;

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> Q;

  for (int i = 0; i < N; i++)
    cin >> A[i];

  S.resize(Q);
  for (int i = 0; i < Q; i++) {
    cin >> S[i].l >> S[i].r;
    S[i].id = i;
    S[i].l--;
    S[i].r--;
  }

  sort(all(S), [](auto a, auto b) {
    if (a.l / D == b.l / D)
      return a.r < b.r;
    return a.l / D < b.l / D;
  });

  for (int i = 0; i < Q; i++) {
    while (S[i].r < r) {
      cnt[A[r]]--;
      if (cnt[A[r]] == 0)
        sum--;
      r--;
    }
    while (S[i].r > r) {
      cnt[A[r + 1]]++;
      if (cnt[A[r + 1]] == 1)
        sum++;
      r++;
    }

    while (S[i].l < l) {
      cnt[A[l - 1]]++;
      if (cnt[A[l - 1]] == 1)
        sum++;
      l--;
    }
    while (S[i].l > l) {
      cnt[A[l]]--;
      if (cnt[A[l]] == 0)
        sum--;
      l++;
    }

    ans[S[i].id] = sum;
  }

  for (int i = 0; i < Q; i++)
    cout << ans[i] << "\n";
}