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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <unordered_map>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ll long long
#define INF 1000000000000000000
#define MOD 1000000007
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define uid uniform_int_distribution<int>
using namespace std;

int sz = 1000;
using PIII = pair<pair<int, int>, int>;
const int N = 5 * 100 * 1000 + 4;
PIII p[N];
int res = 0;
vector<int> cnt(N);
bool cmp(PIII x, PIII y) {
  if (x.ff.ff / sz != y.ff.ff / sz)
    return x.ff.ff < y.ff.ff;
  else
    return x.ff.ss < y.ff.ss;
}
void add(int x) {
  if (cnt[x] == 0)
    res++;
  cnt[x]++;
}
void remove(int x) {
  if (cnt[x] == 1)
    res--;
  cnt[x]--;
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> p[i].ff.ff >> p[i].ff.ss;
    p[i].ss = i;
  }
  sort(p, p + q, cmp);
  vector<int> ans(q);
  int l = 0, r = -1;
  for (int i = 0; i < q; i++) {
    int L = p[i].ff.ff - 1;
    int R = p[i].ff.ss - 1;
    // cout << l << "  " << r << "\\n";
    while (l < L)
      remove(A[l]), l++;
    while (l > L)
      l--, add(A[l]);
    while (r < R)
      r++, add(A[r]);
    while (r > R)
      remove(A[r]), r--;
    ans[p[i].ss] = res;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <unordered_map>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ll long long
#define INF 1000000000000000000
#define MOD 1000000007
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define uid uniform_int_distribution<int>
using namespace std;

int sz = 500;
using PIII = pair<pair<int, int>, int>;
const int N = 5 * 100 * 1000 + 4;
PIII p[N];
int res = 0;
vector<int> cnt(N);
bool cmp(PIII x, PIII y) {
  if (x.ff.ff / sz != y.ff.ff / sz)
    return x.ff.ff < y.ff.ff;
  else
    return x.ff.ss < y.ff.ss;
}
void add(int x) {
  if (cnt[x] == 0)
    res++;
  cnt[x]++;
}
void remove(int x) {
  if (cnt[x] == 1)
    res--;
  cnt[x]--;
}
int main() {
  FIO int n, q;
  cin >> n >> q;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < q; i++) {
    cin >> p[i].ff.ff >> p[i].ff.ss;
    p[i].ss = i;
  }
  sort(p, p + q, cmp);
  vector<int> ans(q);
  int l = 0, r = -1;
  for (int i = 0; i < q; i++) {
    int L = p[i].ff.ff - 1;
    int R = p[i].ff.ss - 1;
    // cout << l << "  " << r << "\\n";
    while (l < L)
      remove(A[l]), l++;
    while (l > L)
      l--, add(A[l]);
    while (r < R)
      r++, add(A[r]);
    while (r > R)
      remove(A[r]), r--;
    ans[p[i].ss] = res;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }
}