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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

#define MAXN 500002

int block_sze;
int ans = 0;
int freq[MAXN] = {};
int arr[MAXN] = {};
int answer[MAXN] = {};
int cl, cr;
int bl[MAXN] = {};

struct query {
  int id, l, r, lc;
  bool operator<(const query &rhs) {
    return (bl[l] == bl[rhs.l]) ? (r < rhs.r) : (bl[l] < bl[rhs.l]);
  }
} queries[MAXN];

inline void add(int idx) {
  if (!freq[arr[idx]])
    ans++;
  freq[arr[idx]]++;
}

inline void remove(int idx) {
  freq[arr[idx]]--;
  if (!freq[arr[idx]])
    ans--;
}

int n, q, l, r, i;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  /*
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
  //*/

  cin >> n >> q;

  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }

  ans = 0;

  for (i = 0; i < q; i++) {
    cin >> l >> r;
    l--, r--;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].id = i;
  }

  block_sze = sqrt(n) + 1;

  for (i = 0; i < n + 1; i++) {
    bl[i] = (i - 1) / block_sze + 1;
  }

  sort(queries, queries + q);

  cl = 0, cr = -1;
  for (i = 0; i < q; i++) {
    l = queries[i].l, r = queries[i].r;

    for (; cl < l;) {
      remove(cl);
      cl++;
    }
    for (; r < cr;) {
      remove(cr);
      cr--;
    }

    for (; l < cl;) {
      cl--;
      add(cl);
    }

    for (; cr < r;) {
      cr++;
      add(cr);
    }

    answer[queries[i].id] = ans;
  }

  for (i = 0; i < q; i++) {
    cout << answer[i] << '\\n';
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

#define MAXN 500005

int block_sze;
int ans = 0;
int freq[MAXN] = {};
int arr[MAXN] = {};
int answer[MAXN] = {};
int cl, cr;
int bl[MAXN] = {};

struct query {
  int id, l, r, lc;
  bool operator<(const query &rhs) {
    return (bl[l] == bl[rhs.l]) ? (r < rhs.r) : (bl[l] < bl[rhs.l]);
  }
} queries[MAXN];

inline void add(int idx) {
  freq[arr[idx]]++;
  if (freq[arr[idx]] == 1)
    ans++;
}

inline void remove(int idx) {
  freq[arr[idx]]--;
  if (freq[arr[idx]] == 0)
    ans--;
}

int n, q, l, r, i;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  /*
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
  //*/

  cin >> n >> q;

  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }

  ans = 0;

  for (i = 0; i < q; i++) {
    cin >> l >> r;
    l--, r--;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].id = i;
  }

  block_sze = sqrt(n) + 1;

  for (i = 0; i < n + 1; i++) {
    bl[i] = (i - 1) / block_sze + 1;
  }

  sort(queries, queries + q);

  cl = 0, cr = -1;
  for (i = 0; i < q; i++) {
    l = queries[i].l, r = queries[i].r;

    for (; cl < l;) {
      remove(cl);
      cl++;
    }
    for (; r < cr;) {
      remove(cr);
      cr--;
    }

    for (; l < cl;) {
      cl--;
      add(cl);
    }

    for (; cr < r;) {
      cr++;
      add(cr);
    }

    answer[queries[i].id] = ans;
  }

  for (i = 0; i < q; i++) {
    cout << answer[i] << '\\n';
  }

  return 0;
}