Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define dofloat cout << fixed << setprecision(8)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define bitcount __builtin_popcount
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef pair<int, int> ii;
const long long MOD = 1000000007;
const long long MAX = 100005;
const long double PI = 3.14159265359;
const long double G = 9.807;
const long long INF = 1e18;
const long double EPS = 1e-6;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
bool isprime(ll a) {
  if (a == 2)
    return 1;
  if (!(a & 1))
    return 0;
  for (ll i = 3; i * i <= a; i += 2)
    if (a % i == 0)
      return 0;
  return 1;
}
ll mpow(ll base, ll exponent, ll modulus) {
  if (modulus == 1)
    return 0;
  long long result = 1;
  base = base % modulus;
  while (exponent) {
    if (exponent % 2 == 1)
      result = (result * base) % modulus;
    exponent = exponent >> 1;
    base = (base * base) % modulus;
  }
  return result;
}
ll minv(ll a, ll mod) {
  ll _gcd = gcd(a, mod);
  assert(_gcd == 1);
  return mpow(a, mod - 2, mod);
}

/*
ll ncr(ll N,ll K){
  if(N<K)return 0;
  if(K==0)return 1;
  if(N==0)return 0;

  ll den=1;
  for(ll i=1;i<=K;i++)den*=i;
  ll num=1;
  while(K--){
    num*=N;
    if(num%den==0){
      num/=den;
      den=1;
    }
    N--;
  }

  return num;
}
*/

const int BZ = 725;

int n, m, a[5 * MAX], ans[5 * MAX], f[5 * MAX];
int cur;

void take(int x) {
  if (!f[a[x]])
    ++cur;
  ++f[a[x]];
  return;
}

void drop(int x) {
  --f[a[x]];
  if (!f[a[x]])
    --cur;
  return;
}

int omg(pair<ii, int> x, pair<ii, int> y) {
  ii A = x.first;
  ii B = y.first;
  int L1 = A.first, R1 = A.second;
  int L2 = B.first, R2 = B.second;
  L1 /= BZ;
  L2 /= BZ;
  if (L1 != L2)
    return L1 < L2;
  return R1 < R2;
}

int main() {
  FAST;
  cin >> n >> m;

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }

  vector<pair<ii, int>> qs;

  for (int i = 1; i <= m; ++i) {
    int l, r;
    cin >> l >> r;
    qs.pb({{l, r}, i});
  }

  sort(all(qs), omg);

  int mol = 1, mor = 0;

  for (int i = 0; i < m; ++i) {
    ii p = qs[i].first;
    int idx = qs[i].second;
    int l = p.first, r = p.second;
    while (mor < r) {
      take(++mor);
    }
    while (mor > r) {
      drop(mor--);
    }
    while (mol < l) {
      drop(mol++);
    }
    while (mol > l) {
      take(--mol);
    }
    ans[idx] = cur;
  }

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

/*
 WA? check edge cases, N=1,M=1 etc. Read code fully once and check all
 variables. Read the question again and see what you might have missed. Consider
 changing your solution or find more cases.
 */


Fixed submission:
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define dofloat cout << fixed << setprecision(8)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define bitcount __builtin_popcount
#define all(vec) vec.begin(), vec.end()
#define rall(vec) vec.rbegin(), vec.rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef pair<int, int> ii;
const long long MOD = 1000000007;
const long long MAX = 100005;
const long double PI = 3.14159265359;
const long double G = 9.807;
const long long INF = 1e18;
const long double EPS = 1e-6;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
bool isprime(ll a) {
  if (a == 2)
    return 1;
  if (!(a & 1))
    return 0;
  for (ll i = 3; i * i <= a; i += 2)
    if (a % i == 0)
      return 0;
  return 1;
}
ll mpow(ll base, ll exponent, ll modulus) {
  if (modulus == 1)
    return 0;
  long long result = 1;
  base = base % modulus;
  while (exponent) {
    if (exponent % 2 == 1)
      result = (result * base) % modulus;
    exponent = exponent >> 1;
    base = (base * base) % modulus;
  }
  return result;
}
ll minv(ll a, ll mod) {
  ll _gcd = gcd(a, mod);
  assert(_gcd == 1);
  return mpow(a, mod - 2, mod);
}

/*
ll ncr(ll N,ll K){
  if(N<K)return 0;
  if(K==0)return 1;
  if(N==0)return 0;

  ll den=1;
  for(ll i=1;i<=K;i++)den*=i;
  ll num=1;
  while(K--){
    num*=N;
    if(num%den==0){
      num/=den;
      den=1;
    }
    N--;
  }

  return num;
}
*/

const int BZ = 1000;

int n, m, a[5 * MAX], ans[5 * MAX], f[5 * MAX];
int cur;

void take(int x) {
  if (!f[a[x]])
    ++cur;
  ++f[a[x]];
  return;
}

void drop(int x) {
  --f[a[x]];
  if (!f[a[x]])
    --cur;
  return;
}

int omg(pair<ii, int> x, pair<ii, int> y) {
  ii A = x.first;
  ii B = y.first;
  int L1 = A.first, R1 = A.second;
  int L2 = B.first, R2 = B.second;
  L1 /= BZ;
  L2 /= BZ;
  if (L1 != L2)
    return L1 < L2;
  return R1 < R2;
}

int main() {
  FAST;
  cin >> n >> m;

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }

  vector<pair<ii, int>> qs;

  for (int i = 1; i <= m; ++i) {
    int l, r;
    cin >> l >> r;
    qs.pb({{l, r}, i});
  }

  sort(all(qs), omg);

  int mol = 1, mor = 0;

  for (int i = 0; i < m; ++i) {
    ii p = qs[i].first;
    int idx = qs[i].second;
    int l = p.first, r = p.second;
    while (mor < r) {
      take(++mor);
    }
    while (mor > r) {
      drop(mor--);
    }
    while (mol < l) {
      drop(mol++);
    }
    while (mol > l) {
      take(--mol);
    }
    ans[idx] = cur;
  }

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

/*
 WA? check edge cases, N=1,M=1 etc. Read code fully once and check all
 variables. Read the question again and see what you might have missed. Consider
 changing your solution or find more cases.
 */