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

#include <bits/stdc++.h>
using namespace std;
const int MAX = 500005;
int block;
struct Query {
  int L, R, index, result;
};
bool compare(Query x, Query y) {
  if (x.L / block != y.L / block)
    return x.L / block < y.L / block;
  return x.R < y.R;
}
bool compare1(Query x, Query y) { return x.index < y.index; }
void queryResults(int a[], int n, Query q[], int m) {
  block = (int)sqrt(n);
  sort(q, q + m, compare);
  int currL = 0, currR = 0;
  int curr_Diff_elements = 0;
  int freq[MAX] = {0};
  for (int i = 0; i < m; i++) {

    int L = q[i].L, R = q[i].R;

    while (currL < L) {

      freq[a[currL]]--;
      if (freq[a[currL]] == 0)
        curr_Diff_elements--;

      currL++;
    }

    while (currL > L) {
      freq[a[currL - 1]]++;

      if (freq[a[currL - 1]] == 1)
        curr_Diff_elements++;

      currL--;
    }
    while (currR <= R) {
      freq[a[currR]]++;

      if (freq[a[currR]] == 1)
        curr_Diff_elements++;

      currR++;
    }

    while (currR > R + 1) {

      freq[a[currR - 1]]--;

      if (freq[a[currR - 1]] == 0)
        curr_Diff_elements--;

      currR--;
    }
    q[i].result = curr_Diff_elements;
  }
}

void printResults(Query q[], int m) {
  sort(q, q + m, compare1);
  for (int i = 0; i < m; i++) {
    cout << q[i].result << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  Query q[m];
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    q[i].index = i;
    q[i].L = l - 1;
    q[i].R = r - 1;
    q[i].result = 0;
  }
  queryResults(a, n, q, m);
  printResults(q, m);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
int block;
struct Query {
  int L, R, index, result;
};
bool compare(Query x, Query y) {
  if (x.L / block != y.L / block)
    return x.L / block < y.L / block;
  return x.R < y.R;
}
bool compare1(Query x, Query y) { return x.index < y.index; }
void queryResults(int a[], int n, Query q[], int m) {
  block = (int)sqrt(n);

  sort(q, q + m, compare);

  int currL = 0, currR = 0;
  int curr_Diff_elements = 0;

  int freq[MAX] = {0};

  for (int i = 0; i < m; i++) {

    int L = q[i].L, R = q[i].R;

    while (currL < L) {

      freq[a[currL]]--;
      if (freq[a[currL]] == 0)
        curr_Diff_elements--;

      currL++;
    }

    while (currL > L) {
      freq[a[currL - 1]]++;

      if (freq[a[currL - 1]] == 1)
        curr_Diff_elements++;

      currL--;
    }
    while (currR <= R) {
      freq[a[currR]]++;

      if (freq[a[currR]] == 1)
        curr_Diff_elements++;

      currR++;
    }

    while (currR > R + 1) {

      freq[a[currR - 1]]--;

      if (freq[a[currR - 1]] == 0)
        curr_Diff_elements--;

      currR--;
    }
    q[i].result = curr_Diff_elements;
  }
}

void printResults(Query q[], int m) {
  sort(q, q + m, compare1);
  for (int i = 0; i < m; i++) {
    cout << q[i].result << endl;
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  Query q[m];
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    q[i].index = i;
    q[i].L = l - 1;
    q[i].R = r - 1;
    q[i].result = 0;
  }
  queryResults(a, n, q, m);
  printResults(q, m);
  return 0;
}