Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repl(i, a, b) for (ll i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = b; i >= (a); --i)
#define repdl(i, a, b) for (ll i = b; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool ihmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO

template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
#endif
#ifndef _TEMPLATE_OPT
#define _TEMPLATE_OPT
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#endif
using namespace std;

int a[500005];

struct Query {
  int l, r, id, ans;
};

// freqcnt
int freq[500005];
int distinct = 0;
void freqadd(int x) {
  // write("FA", x, freq[x], "->", freq[x]+1);
  if (!freq[x])
    distinct++;
  freq[x]++;
}
void freqrem(int x) {

  // write("FR", x, freq[x], "->", freq[x]-1);
  freq[x]--;
  if (!freq[x])
    distinct--;
}
Query query[500005];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n, q;
  read(n, q);
  rep(i, 1, n + 1) read(a[i]);
  rep(i, 0, q) read(query[i].l, query[i].r), query[i].id = i;

  const int BS = 500;
  sort(query, query + q, [](const Query &a, const Query &b) {
    if (a.l / BS != b.l / BS)
      return a.l / BS < b.l / BS;
    else
      return a.r < b.r;
  });

  // inclusive [cl, cr]
  int cl = 1, cr = 0;
  rep(i, 0, q) {
    int nl = query[i].l, nr = query[i].r;
    while (cl > nl)
      freqadd(a[--cl]);
    while (cr < nr)
      freqadd(a[++cr]);
    while (cl < nl)
      freqrem(a[cl++]);
    while (cr > nr)
      freqrem(a[cr--]);
    query[i].ans = distinct;
  }

  sort(query, query + q,
       [](const Query &a, const Query &b) { return a.id < b.id; });
  rep(i, 0, q) write(query[i].ans);
}


Fixed submission:
#ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repl(i, a, b) for (ll i = a; i < (b); ++i)
#define repd(i, a, b) for (int i = b; i >= (a); --i)
#define repdl(i, a, b) for (ll i = b; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool ihmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO

template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
#endif
#ifndef _TEMPLATE_OPT
#define _TEMPLATE_OPT
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#endif
using namespace std;

int a[500005];

struct Query {
  int l, r, id, ans;
};

// freqcnt
int freq[500005];
int distinct = 0;
void freqadd(int x) {
  // write("FA", x, freq[x], "->", freq[x]+1);
  if (!freq[x])
    distinct++;
  freq[x]++;
}
void freqrem(int x) {

  // write("FR", x, freq[x], "->", freq[x]-1);
  freq[x]--;
  if (!freq[x])
    distinct--;
}
Query query[500005];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n, q;
  read(n, q);
  rep(i, 1, n + 1) read(a[i]);
  rep(i, 0, q) read(query[i].l, query[i].r), query[i].id = i;

  const int BS = 1500;
  sort(query, query + q, [](const Query &a, const Query &b) {
    if (a.l / BS != b.l / BS)
      return a.l / BS < b.l / BS;
    else
      return a.r < b.r;
  });

  // inclusive [cl, cr]
  int cl = 1, cr = 0;
  rep(i, 0, q) {
    int nl = query[i].l, nr = query[i].r;
    while (cl > nl)
      freqadd(a[--cl]);
    while (cr < nr)
      freqadd(a[++cr]);
    while (cl < nl)
      freqrem(a[cl++]);
    while (cr > nr)
      freqrem(a[cr--]);
    query[i].ans = distinct;
  }

  sort(query, query + q,
       [](const Query &a, const Query &b) { return a.id < b.id; });
  rep(i, 0, q) write(query[i].ans);
}