Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

const int sqn = 710;
const int maxn = 5e5;

int n, q;
vector<int> c(maxn + 1, 0);
vector<int> ans(maxn + 1, 0);
vector<int> freq(maxn + 1, 0);
vector<vector<int>> queries(maxn + 1, vector<int>(3, 0));

class myComp {
public:
  bool operator()(const vector<int> &a, const vector<int> &b) {
    // if ((a[0] / sqn) == (b[0] / sqn)) return a[1] < b[1];
    // return a[0] < b[0];
    if (a[0] / sqn != b[0] / sqn)
      return a[0] < b[0];
    return a[0] / sqn % 2 ? a[1] < b[1] : a[1] > b[1];
  }
};

int main() {

  cin >> n >> q;
  for (int i = 0; i <= n - 1; i++)
    cin >> c[i];
  for (int i = 0; i <= q - 1; i++)
    cin >> queries[i][0] >> queries[i][1], queries[i][0]--, queries[i][1]--,
        queries[i][2] = i;

  sort(queries.begin(), queries.begin() + q, myComp());

  int beg = 0, en = 1, cnt = 0;
  freq[c[0]]++;
  cnt = 1;

  for (int i = 0; i <= q - 1; i++) {
    int l = queries[i][0], r = queries[i][1];
    while (en <= r) {
      freq[c[en]]++;
      if (freq[c[en]] == 1)
        cnt++;
      en++;
    }

    while (beg < l) {
      freq[c[beg]]--;
      if (freq[c[beg]] == 0)
        cnt--;
      beg++;
    }

    while (beg > l) {
      beg--;
      freq[c[beg]]++;
      if (freq[c[beg]] == 1)
        cnt++;
    }

    while (en > r + 1) {
      en--;
      freq[c[en]]--;
      if (freq[c[en]] == 0)
        cnt--;
    }
    ans[queries[i][2]] = cnt;
  }

  for (int i = 0; i <= q - 1; i++)
    cout << ans[i] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
struct _ {
  ios_base::Init i;
  _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;

const int sqn = 316;
const int maxn = 5e5;

int n, q;
vector<int> c(maxn + 1, 0);
vector<int> ans(maxn + 1, 0);
vector<int> freq(maxn + 1, 0);
vector<vector<int>> queries(maxn + 1, vector<int>(3, 0));

class myComp {
public:
  bool operator()(const vector<int> &a, const vector<int> &b) {
    // if ((a[0] / sqn) == (b[0] / sqn)) return a[1] < b[1];
    // return a[0] < b[0];
    if (a[0] / sqn != b[0] / sqn)
      return a[0] < b[0];
    return a[0] / sqn % 2 ? a[1] < b[1] : a[1] > b[1];
  }
};

int main() {

  cin >> n >> q;
  for (int i = 0; i <= n - 1; i++)
    cin >> c[i];
  for (int i = 0; i <= q - 1; i++)
    cin >> queries[i][0] >> queries[i][1], queries[i][0]--, queries[i][1]--,
        queries[i][2] = i;

  sort(queries.begin(), queries.begin() + q, myComp());

  int beg = 0, en = 1, cnt = 0;
  freq[c[0]]++;
  cnt = 1;

  for (int i = 0; i <= q - 1; i++) {
    int l = queries[i][0], r = queries[i][1];
    while (en <= r) {
      freq[c[en]]++;
      if (freq[c[en]] == 1)
        cnt++;
      en++;
    }

    while (beg < l) {
      freq[c[beg]]--;
      if (freq[c[beg]] == 0)
        cnt--;
      beg++;
    }

    while (beg > l) {
      beg--;
      freq[c[beg]]++;
      if (freq[c[beg]] == 1)
        cnt++;
    }

    while (en > r + 1) {
      en--;
      freq[c[en]]--;
      if (freq[c[en]] == 0)
        cnt--;
    }
    ans[queries[i][2]] = cnt;
  }

  for (int i = 0; i <= q - 1; i++)
    cout << ans[i] << '\\n';
  return 0;
}