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

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  int n;
  cin >> n;
  int m;
  cin >> m;
  vector<int> V(n);
  for (int &i : V)
    cin >> i;
  vector<pair<pair<int, int>, pair<int, int>>> Q;
  int sq = sqrt(n);
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    Q.emplace_back(make_pair(l, r), make_pair(i, l / sq));
  }

  sort(Q.begin(), Q.end(),
       [&](pair<pair<int, int>, pair<int, int>> &l,
           pair<pair<int, int>, pair<int, int>> &r) {
         if (l.second.second == r.second.second)
           return l.first.second < r.first.second;
         return l.second.second < r.second.second;
       });

  int prev = -1;
  vector<int> ans(m);
  vector<int> cnt(n + 1);
  int anss = 0;
  int prevl = -1;
  int prevr = -1;
  for (int i = 0; i < m; ++i) {
    if (Q[i].second.second != prev) {
      for (int &q : cnt)
        q = 0;
      anss = 0;
      for (int q = Q[i].first.first; q <= Q[i].first.second; ++q) {
        if (cnt[V[q]] == 0)
          ++anss;
        cnt[V[q]]++;
      }
    } else {
      int l = Q[i].first.first;
      int r = Q[i].first.second;
      if (prevl < l) {
        for (int q = prevl; q < l; ++q) {
          cnt[V[q]]--;
          if (cnt[V[q]] == 0)
            --anss;
        }
      } else {
        for (int q = l; q < prevl; ++q) {
          if (cnt[V[q]] == 0)
            ++anss;
          cnt[V[q]]++;
        }
      }
      for (int q = prevr + 1; q <= r; ++q) {
        if (cnt[V[q]] == 0)
          ++anss;
        cnt[V[q]]++;
      }
    }
    prevl = Q[i].first.first;
    prevr = Q[i].first.second;
    prev = Q[i].second.second;
    int kek = 0;
    ans[Q[i].second.first] = anss;
  }

  for (int i = 0; i < m; ++i) {
    cout << ans[i] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifndef ONLINE_JUDGE
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  int n;
  cin >> n;
  int m;
  cin >> m;
  vector<int> V(n);
  for (int &i : V)
    cin >> i;
  vector<pair<pair<int, int>, pair<int, int>>> Q;
  int sq = sqrt(n);
  for (int i = 0; i < m; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    Q.emplace_back(make_pair(l, r), make_pair(i, l / sq));
  }

  sort(Q.begin(), Q.end(),
       [&](pair<pair<int, int>, pair<int, int>> &l,
           pair<pair<int, int>, pair<int, int>> &r) {
         if (l.second.second == r.second.second)
           return l.first.second < r.first.second;
         return l.second.second < r.second.second;
       });

  int prev = -1;
  vector<int> ans(m);
  vector<int> cnt(n + 1);
  int prevl = -1;
  int prevr = -1;
  for (int i = 0; i < m; ++i) {
    if (Q[i].second.second != prev) {
      for (int &q : cnt)
        q = 0;
      for (int q = Q[i].first.first; q <= Q[i].first.second; ++q) {
        cnt[V[q]]++;
      }
    } else {
      int l = Q[i].first.first;
      int r = Q[i].first.second;
      if (prevl < l) {
        for (int q = prevl; q < l; ++q)
          cnt[V[q]]--;
      } else {
        for (int q = l; q < prevl; ++q)
          cnt[V[q]]++;
      }
      for (int q = prevr + 1; q <= r; ++q)
        cnt[V[q]]++;
    }
    prevl = Q[i].first.first;
    prevr = Q[i].first.second;
    prev = Q[i].second.second;
    int kek = 0;
    for (int q : cnt)
      if (q != 0)
        ++kek;
    ans[Q[i].second.first] = kek;
  }

  for (int i = 0; i < m; ++i) {
    cout << ans[i] << "\\n";
  }
  return 0;
}