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

#include <bits/stdc++.h>
#define ll long long int
#define pragi(a, b) for (ll i = a; i < b; i++)
#define pragj(a, b) for (ll j = a; j < b; j++)
#define pragk(a, b) for (ll k = a; k >= b; k--)
#define all(v) (v.begin(), v.end())
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define MT make_tuple
#define F first
#define S second
#define BLK 750
#define KAKA                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef long double ld;
typedef pair<int, int> pii;
// typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ll, ll> pll;
// ll BLK;
struct query {
  ll L, R, i;
};
query Q[1000005];
ll v[1000005], res[1000005], freq[1000005];
ll cnt = 0;
// const ll BLK = 750;
bool cmp(query a, query b) {
  if (a.L / BLK != b.L / BLK)
    return (a.L / BLK < b.L / BLK);

  return (a.R < b.R);
}
bool cmp1(query a, query b) { return (a.i < b.i); }
void add(ll pos) {
  freq[v[pos]]++;
  if (freq[v[pos]] == 1)
    cnt++;
}
void erased(ll pos) {
  freq[v[pos]]--;
  if (freq[v[pos]] == 0)
    cnt--;
}
int main() {
  KAKA ll n, q;
  cin >> n >> q;
  // BLK = sqrt(n);
  pragi(0, n) { cin >> v[i]; }
  pragi(0, q) {
    cin >> Q[i].L >> Q[i].R;
    Q[i].i = i, Q[i].L--, Q[i].R--;
  }
  sort(Q, Q + q, cmp);
  ll ml = 0, mr = -1;
  pragi(0, q) {
    ll left = Q[i].L;
    ll right = Q[i].R;

    while (ml > left) {
      ml--;
      add(ml);
    }
    while (mr < right) {
      mr++;
      add(mr);
    }
    while (ml < left) {
      erased(ml);
      ml++;
    }
    while (mr > right) {
      erased(mr);
      mr--;
    }
    res[Q[i].i] = cnt;
  }
  sort(Q, Q + q, cmp1);
  pragi(0, q) { cout << res[i] << "\\n"; }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define pragi(a, b) for (ll i = a; i < b; i++)
#define pragj(a, b) for (ll j = a; j < b; j++)
#define pragk(a, b) for (ll k = a; k >= b; k--)
#define all(v) (v.begin(), v.end())
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define MT make_tuple
#define F first
#define S second
#define KAKA                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef long double ld;
typedef pair<int, int> pii;
// typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ll, ll> pll;
ll BLK;
struct query {
  ll L, R, i;
};
query Q[1000005];
ll v[1000005], res[1000005], freq[1000005];
ll cnt = 0;
// const ll BLK = 750;
bool cmp(query a, query b) {
  if (a.L / BLK != b.L / BLK)
    return (a.L / BLK < b.L / BLK);

  return (a.R < b.R);
}
bool cmp1(query a, query b) { return (a.i < b.i); }
void add(ll pos) {
  freq[v[pos]]++;
  if (freq[v[pos]] == 1)
    cnt++;
}
void erased(ll pos) {
  freq[v[pos]]--;
  if (freq[v[pos]] == 0)
    cnt--;
}
int main() {
  KAKA ll n, q;
  cin >> n >> q;
  BLK = sqrt(n);
  pragi(0, n) { cin >> v[i]; }
  pragi(0, q) {
    cin >> Q[i].L >> Q[i].R;
    Q[i].i = i, Q[i].L--, Q[i].R--;
  }
  sort(Q, Q + q, cmp);
  ll ml = 0, mr = -1;
  pragi(0, q) {
    ll left = Q[i].L;
    ll right = Q[i].R;

    while (ml > left) {
      ml--;
      add(ml);
    }
    while (mr < right) {
      mr++;
      add(mr);
    }
    while (ml < left) {
      erased(ml);
      ml++;
    }
    while (mr > right) {
      erased(mr);
      mr--;
    }
    res[Q[i].i] = cnt;
  }
  sort(Q, Q + q, cmp1);
  pragi(0, q) { cout << res[i] << "\\n"; }
}