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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
int n, q;
vector<int> v;
vector<pair<pair<int, int>, int>> query;
int block;
bool comp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / block != b.first.first / block)
    return a.first.first / block < b.first.first / block;
  return a.first.second < b.first.second;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  v.resize(n);
  for (int &i : v)
    cin >> i;
  query.resize(q);
  int pos = 0;
  for (auto &i : query) {
    cin >> i.first.first >> i.first.second;
    i.second = pos++;
  }

  block = sqrt(n);
  sort(query.begin(), query.end(), comp);

  int curL = 0, curR = 0;
  vector<int> ans(q);
  int freq[n + 1];
  memset(freq, 0, sizeof freq);
  int si = 0;
  for (auto i : query) {
    int l = i.first.first - 1, r = i.first.second - 1;
    while (curR <= r) {
      if (freq[v[curR]] == 0)
        si++;
      freq[v[curR]]++;
      curR++;
    }
    while (curR > r + 1) {
      freq[v[curR - 1]]--;
      if (freq[v[curR - 1]] == 0)
        si--;
      curR--;
    }
    while (curL < l) {
      freq[v[curL]]--;
      if (freq[v[curL]] == 0)
        si--;
      curL++;
    }
    while (curL > l) {
      if (freq[v[curL - 1]] == 0)
        si++;
      freq[v[curL - 1]]++;
      curL--;
    }
    ans[i.second] = si;
  }
  for (int i : ans)
    cout << i << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define deb(x) cerr << "\\n" << #x << "=" << x << "\\n";
#define deb2(x, y)                                                             \\
  cerr << "\\n" << #x << "=" << x << "\\n" << #y << "=" << y << "\\n";
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
int n, q;
vector<int> v;
vector<pair<pair<int, int>, int>> query;
int block;
bool comp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / block != b.first.first / block)
    return a.first.first / block < b.first.first / block;
  return a.first.second < b.first.second;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  v.resize(n);
  for (int &i : v)
    cin >> i;
  query.resize(q);
  int pos = 0;
  for (auto &i : query) {
    cin >> i.first.first >> i.first.second;
    i.second = pos++;
  }

  block = sqrt(n);
  sort(query.begin(), query.end(), comp);

  int curL = 0, curR = 0;
  vector<pair<int, int>> ans;
  int freq[n + 1];
  memset(freq, 0, sizeof freq);
  int si = 0;
  for (auto i : query) {
    int l = i.first.first - 1, r = i.first.second - 1;
    while (curR <= r) {
      if (freq[v[curR]] == 0)
        si++;
      freq[v[curR]]++;
      curR++;
    }
    while (curR > r + 1) {
      freq[v[curR - 1]]--;
      if (freq[v[curR - 1]] == 0)
        si--;
      curR--;
    }
    while (curL < l) {
      freq[v[curL]]--;
      if (freq[v[curL]] == 0)
        si--;
      curL++;
    }
    while (curL > l) {
      if (freq[v[curL - 1]] == 0)
        si++;
      freq[v[curL - 1]]++;
      curL--;
    }
    ans.emplace_back(i.second, si);
  }
  sort(ans.begin(), ans.end());
  for (auto i : ans)
    cout << i.second << "\\n";
  return 0;
}