Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
const int maxn = 500010;

typedef pair<pi, int> pii;
const int len = 600;

int n, q;
int A[maxn];
int co[maxn];
int rans = 0;

struct query {
  int l, r, index, ans;
  void read(int _l, int _r, int _index) {
    l = _l;
    r = _r;
    index = _index;
    ans = 0;
  }
};

vector<query> queries;

bool cmp(const query &a, query &b) {
  int buc1 = a.l / len, buc2 = b.l / len;
  if (buc1 == buc2)
    return a.r < b.r;
  else
    return buc1 < buc2;
}

bool cmpind(const query &a, const query &b) { return a.index < b.index; }

void add(int x) {
  co[A[x]]++;
  if (co[A[x]] == 1)
    rans++;
}

void rem(int x) {
  co[A[x]]--;
  if (co[A[x]] == 0)
    rans--;
}

int32_t main() {
  FAST cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    queries.push_back(query());
    queries[i].read(--l, --r, i);
  }

  sort(queries.begin(), queries.end(), cmp);

  // cout << "HELLO\n";
  // for (auto i: queries) {
  // cout << i.l << " " << i.r << "\n";
  //}

  int ql = 1, qr = 0;
  for (int i = 0; i < q; i++) {
    int l = queries[i].l, r = queries[i].r;
    while (ql > l)
      add(--ql);
    while (ql < l)
      rem(ql++);
    while (qr < r)
      add(++qr);
    while (qr > r)
      rem(qr--);

    queries[i].ans = rans;
  }

  sort(queries.begin(), queries.end(), cmpind);
  for (int i = 0; i < q; i++) {
    cout << queries[i].ans << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
const int maxn = 500010;

typedef pair<pi, int> pii;
const int len = 800;

int n, q;
int A[maxn];
int co[maxn];
int rans = 0;

struct query {
  int l, r, index, ans;
  void read(int _l, int _r, int _index) {
    l = _l;
    r = _r;
    index = _index;
    ans = 0;
  }
};

vector<query> queries;

bool cmp(const query &a, query &b) {
  int buc1 = a.l / len, buc2 = b.l / len;
  if (buc1 == buc2)
    return a.r < b.r;
  else
    return buc1 < buc2;
}

bool cmpind(const query &a, const query &b) { return a.index < b.index; }

void add(int x) {
  co[A[x]]++;
  if (co[A[x]] == 1)
    rans++;
}

void rem(int x) {
  co[A[x]]--;
  if (co[A[x]] == 0)
    rans--;
}

int32_t main() {
  FAST cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    queries.push_back(query());
    queries[i].read(--l, --r, i);
  }

  sort(queries.begin(), queries.end(), cmp);

  // cout << "HELLO\n";
  // for (auto i: queries) {
  // cout << i.l << " " << i.r << "\n";
  //}

  int ql = 1, qr = 0;
  for (int i = 0; i < q; i++) {
    int l = queries[i].l, r = queries[i].r;
    while (ql > l)
      add(--ql);
    while (ql < l)
      rem(ql++);
    while (qr < r)
      add(++qr);
    while (qr > r)
      rem(qr--);

    queries[i].ans = rans;
  }

  sort(queries.begin(), queries.end(), cmpind);
  for (int i = 0; i < q; i++) {
    cout << queries[i].ans << "\n";
  }
}