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

#include <bits/stdc++.h>
using namespace std;
int block;
struct Query {
  int L, R;
};
map<pair<int, int>, int> mp;
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;
}
void queryResults(int a[], int n, Query q[], int m) {
  block = (int)sqrt(n);
  sort(q, q + m, compare);
  int x = 0;
  int arr[600000];
  for (int i = 0; i < 600000; i++)
    arr[i] = 0;
  for (int i = 0; i < n; i++) {
    if (arr[a[i]] == 0)
      x++;
    arr[a[i]]++;
  }
  int currL = 0;
  int currR = n;
  // mp[make_pair(q[0].L,q[0].R)]=x;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currL < L) {
      if (arr[a[currL]] == 1)
        x--;
      arr[a[currL]]--;
      currL++;
    }
    while (currL > L) {
      if (arr[a[currL - 1]] == 0)
        x++;
      arr[a[currL - 1]]++;
      currL--;
    }
    while (currR <= R) {
      if (arr[a[currR]] == 0)
        x++;
      arr[a[currR]]++;
      currR++;
    }
    while (currR > R + 1) {
      if (arr[a[currR - 1]] == 1)
        x--;
      arr[a[currR - 1]]--;
      currR--;
    }
    mp[make_pair(q[i].L, q[i].R)] = x;
  }
}

// Driver program
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  Query q[m];
  Query ns[m];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    q[i].L = a;
    q[i].R = b;
    ns[i].L = a;
    ns[i].R = b;
  }
  queryResults(a, n, q, m);
  // cout<<q[1].L<<" "<<q[1].R<<endl;
  for (int i = 0; i < m; i++) {
    cout << mp[make_pair(ns[i].L, ns[i].R)] << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int block;
struct Query {
  int L, R;
};
map<pair<int, int>, int> mp;
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;
}
void queryResults(int a[], int n, Query q[], int m) {
  block = (int)sqrt(n);
  sort(q, q + m, compare);
  int x = 0;
  int arr[600000];
  for (int i = 0; i < 600000; i++)
    arr[i] = 0;
  for (int i = 0; i < n; i++) {
    if (arr[a[i]] == 0)
      x++;
    arr[a[i]]++;
  }
  int currL = 0;
  int currR = n;
  // mp[make_pair(q[0].L,q[0].R)]=x;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currL < L) {
      if (arr[a[currL]] == 1)
        x--;
      arr[a[currL]]--;
      currL++;
    }
    while (currL > L) {
      if (arr[a[currL - 1]] == 0)
        x++;
      arr[a[currL - 1]]++;
      currL--;
    }
    while (currR <= R) {
      if (arr[a[currR]] == 0)
        x++;
      arr[a[currR]]++;
      currR++;
    }
    while (currR > R + 1) {
      if (arr[a[currR - 1]] == 1)
        x--;
      arr[a[currR - 1]]--;
      currR--;
    }
    mp[make_pair(q[i].L, q[i].R)] = x;
  }
}

// Driver program
int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  Query q[m];
  Query ns[m];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    q[i].L = a;
    q[i].R = b;
    ns[i].L = a;
    ns[i].R = b;
  }
  queryResults(a, n, q, m);
  // cout<<q[1].L<<" "<<q[1].R<<endl;
  for (int i = 0; i < m; i++) {
    cout << mp[make_pair(ns[i].L, ns[i].R)] << endl;
  }
  return 0;
}