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

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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed << setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const int M = (1e9) + 7;
const int N = 500005;
const int B = 700;

int mp[N];

bool cmp(pair<pii, int> a, pair<pii, int> b) {
  return (a.ff.ff / B < b.ff.ff / B) ||
         (a.ff.ff / B == b.ff.ff / B && a.ff.ss < b.ff.ss);
}

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n, q;
    cin >> n >> q;
    int a[n];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    vector<pair<pii, int>> queries(q);
    int ans[q];
    for (int i = 0; i < q; i++) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      queries[i] = {{l, r}, i};
    }
    sort(all(queries), cmp);
    int L = queries[0].ff.ff;
    int R = queries[0].ff.ss;
    int cnt = 0;
    for (int i = L; i <= R; i++) {
      mp[a[i]]++;
      if (mp[a[i]] == 1)
        cnt++;
    }
    ans[queries[0].ss] = cnt;
    for (int i = 1; i < q; i++) {
      int l = queries[i].ff.ff;
      int r = queries[i].ff.ss;
      int ind = queries[i].ss;
      while (L < l) {
        mp[a[L]]--;
        if (!mp[a[L]])
          cnt--;
        L++;
      }
      while (L > l) {
        L--;
        mp[a[L]]++;
        if (mp[a[L]] == 1)
          cnt++;
      }
      while (R > r) {
        mp[a[R]]--;
        if (!mp[a[R]])
          cnt--;
        R--;
      }
      while (R < r) {
        R++;
        mp[a[R]]++;
        if (mp[a[R]] == 1)
          cnt++;
      }
      ans[ind] = cnt;
    }
    for (int i = 0; i < q; i++)
      cout << ans[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed << setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(), x.end()
#define pii pair<long long, long long>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const int M = (1e9) + 7;
const int N = 500005;
const int B = 700;

int mp[N];

bool cmp(pair<pii, int> a, pair<pii, int> b) {
  return (a.ff.ff / B < b.ff.ff / B) ||
         (a.ff.ff / B == b.ff.ff / B && a.ff.ss < b.ff.ss);
}

signed main() {
  FAST int tc = 1;
  // cin>>tc;
  for (int ti = 1; ti <= tc; ti++) {
    int n, q;
    cin >> n >> q;
    int a[n];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    vector<pair<pii, int>> queries(q);
    int ans[q];
    for (int i = 0; i < q; i++) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      queries[i] = {{l, r}, i};
    }
    sort(all(queries), cmp);
    int L = queries[0].ff.ff;
    int R = queries[0].ff.ss;
    set<int> s;
    for (int i = L; i <= R; i++) {
      mp[a[i]]++;
      s.insert(a[i]);
    }
    ans[queries[0].ss] = s.size();
    for (int i = 1; i < q; i++) {
      int l = queries[i].ff.ff;
      int r = queries[i].ff.ss;
      int ind = queries[i].ss;
      while (L < l) {
        mp[a[L]]--;
        if (!mp[a[L]])
          s.erase(s.find(a[L]));
        L++;
      }
      while (L > l) {
        L--;
        mp[a[L]]++;
        s.insert(a[L]);
      }
      while (R > r) {
        mp[a[R]]--;
        if (!mp[a[R]])
          s.erase(s.find(a[R]));
        R--;
      }
      while (R < r) {
        R++;
        mp[a[R]]++;
        s.insert(a[R]);
      }
      ans[ind] = s.size();
    }
    for (int i = 0; i < q; i++)
      cout << ans[i] << endl;
  }
  return 0;
}