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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\\n"
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 5000 * 100 + 10;
const ll N2 = 70;
const ld PI = 3.141592653589793;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p = LLONG_MAX) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int freq[N] = {0};

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) {
  // Find block size
  block = (int)sqrt(n);

  // Sort all queries so that queries of same
  // blocks are arranged together.
  sort(q, q + m, compare);

  // Initialize current L, current R and current
  // different elements
  int currL = 0, currR = 0;
  int curr_Diff_elements = 0;

  // Traverse through all queries
  for (int i = 0; i < m; i++) {

    // L and R values of current range
    int L = q[i].L, R = q[i].R;

    while (currL < L) {

      // element a[currL] is removed
      freq[a[currL]]--;
      if (freq[a[currL]] == 0)
        curr_Diff_elements--;

      currL++;
    }

    // Add Elements of current Range
    // Note:- during addition of the left
    // side elements we have to add currL-1
    // because currL is already in range
    while (currL > L) {
      freq[a[currL - 1]]++;

      // include a element if it occurs first time
      if (freq[a[currL - 1]] == 1)
        curr_Diff_elements++;

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

      // include a element if it occurs first time
      if (freq[a[currR]] == 1)
        curr_Diff_elements++;

      currR++;
    }
    while (currR > R + 1) {

      // element a[currL] is removed
      freq[a[currR - 1]]--;

      // if occurrence of a number is reduced
      // to zero remove it from list of
      // different elements
      if (freq[a[currR - 1]] == 0)
        curr_Diff_elements--;

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

// print the result of all range queries in
// initial order of queries
void printResults(Query q[], int m) {
  sort(q, q + m, compare1);
  for (int i = 0; i < m; i++) {
    cout << q[i].result << endl;
  }
}
signed main() {
  fastio();
// cout<<fixed<<setprecision(20);
// CHECK for LONG LONG and LONG DOUBLE
//*comment for all except cc/cf
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif //*/
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int m = q;
  int i = 0;
  Query qq[q];
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    qq[i].L = l;
    qq[i].R = r;
    qq[i].index = i;
    i++;
  }
  queryResults(a, n, qq, m);
  printResults(qq, m);

  printclock;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// using namespace __gnu_pbds;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl "\\n"
#define int long long
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
const ll MOD2 = 998244353;
const ll N = 5000 * 100 + 10;
const ll N2 = 70;
const ld PI = 3.141592653589793;
// template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p = LLONG_MAX) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int freq[N] = {0};

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) {
  // Find block size
  block = (int)sqrt(n);

  // Sort all queries so that queries of same
  // blocks are arranged together.
  sort(q, q + m, compare);

  // Initialize current L, current R and current
  // different elements
  int currL = 0, currR = 0;
  int curr_Diff_elements = 0;

  // Traverse through all queries
  for (int i = 0; i < m; i++) {

    // L and R values of current range
    int L = q[i].L, R = q[i].R;

    while (currL < L) {

      // element a[currL] is removed
      freq[a[currL]]--;
      if (freq[a[currL]] == 0)
        curr_Diff_elements--;

      currL++;
    }

    // Add Elements of current Range
    // Note:- during addition of the left
    // side elements we have to add currL-1
    // because currL is already in range
    while (currL > L) {
      freq[a[currL - 1]]++;

      // include a element if it occurs first time
      if (freq[a[currL - 1]] == 1)
        curr_Diff_elements++;

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

      // include a element if it occurs first time
      if (freq[a[currR]] == 1)
        curr_Diff_elements++;

      currR++;
    }
    while (currR > R + 1) {

      // element a[currL] is removed
      freq[a[currR - 1]]--;

      // if occurrence of a number is reduced
      // to zero remove it from list of
      // different elements
      if (freq[a[currR - 1]] == 0)
        curr_Diff_elements--;

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

// print the result of all range queries in
// initial order of queries
void printResults(Query q[], int m) {
  sort(q, q + m, compare1);
  for (int i = 0; i < m; i++) {
    cout << q[i].result << endl;
  }
}
signed main() {
  fastio();
// cout<<fixed<<setprecision(20);
// CHECK for LONG LONG and LONG DOUBLE
//*comment for all except cc/cf
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif //*/
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int m = q;
  int i = 0;
  Query qq[q];
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    qq[i].L = l;
    qq[i].R = r;
    qq[i].index = i;
    i++;
  }
  queryResults(a, n, qq, m);
  printResults(qq, m);

  printclock;
  return 0;
}