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

#include <bits/stdc++.h>

#define ll int
#define pb push_back
#define all(n) n.begin(), n.end()
#define eb emplace_back
#define endl "\\n"
#define pll pair<ll, ll>
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define ff first
#define ss second
// do not use "\\n" in interactive problem
// #define ((x|y)-y) (x&(~y))
// #define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn = 5e5 + 50;

const ll mod = 1000000007;
const ll mod1 = 998244353;
ll a[maxn];

struct tk {
  ll lf, rt;
  ll id;
};
ll p = 890;
ll cur = 0;
bool lf1(tk a, tk b) {
  return (a.lf / p < b.lf / p || (a.lf / p == b.lf / p && a.rt < b.rt));
}
ll cnt[maxn];
tk b[maxn];

void add(ll id) {

  cnt[a[id]]++;
  if (cnt[a[id]] == 1)
    cur++;
}
void ers(ll id) {
  cnt[a[id]]--;
  if (cnt[a[id]] == 0)
    cur--;
}
ll ans[maxn];
int main() {
  // freopen("test.txt","r",stdin);
  //  freopen("CHEMIST.OUT","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, t;
  cin >> n >> t;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= t; i++) {
    cin >> b[i].lf >> b[i].rt;
    b[i].id = i;
  }
  sort(b + 1, b + t + 1, lf1);
  for (int i = b[1].lf; i <= b[1].rt; i++) {
    add(i);
  }
  ans[b[1].id] = cur;
  ll lnw = b[1].lf;
  ll rnw = b[1].rt;
  for (int i = 2; i <= t; i++) {
    while (lnw < b[i].lf) {
      ers(lnw);
      lnw++;
    }
    while (rnw < b[i].rt) {
      rnw++;
      add(rnw);
    }
    while (lnw > b[i].lf) {

      lnw--;
      add(lnw);
    }
    while (rnw > b[i].rt) {
      ers(rnw);
      rnw--;
    }
    ans[b[i].id] = cur;
  }
  for (int i = 1; i <= t; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll int
#define pb push_back
#define all(n) n.begin(), n.end()
#define eb emplace_back
#define endl "\\n"
#define pll pair<ll, ll>
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define ff first
#define ss second
// do not use "\\n" in interactive problem
// #define ((x|y)-y) (x&(~y))
// #define __gcd(fibo(x),fibo(y)=fibo(__gcd(x,y))
using namespace std;
const ll maxn = 5e5 + 50;
const ll maxn1 = 2e5 + 1;
const ll maxx = 1e13;
const ll mod = 1000000007;
const ll mod1 = 998244353;
ll a[maxn];

struct tk {
  ll lf, rt;
  ll id;
};
ll p = 850;
ll cur = 0;
bool lf1(tk a, tk b) {
  return (a.lf / p < b.lf / p || (a.lf / p == b.lf / p && a.rt < b.rt));
}
ll cnt[maxn];
tk b[maxn];

void add(ll id) {

  cnt[a[id]]++;
  if (cnt[a[id]] == 1)
    cur++;
}
void ers(ll id) {
  cnt[a[id]]--;
  if (cnt[a[id]] == 0)
    cur--;
}
ll ans[maxn];
int main() {
  // freopen("test.txt","r",stdin);
  //  freopen("CHEMIST.OUT","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, t;
  cin >> n >> t;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= t; i++) {
    cin >> b[i].lf >> b[i].rt;
    b[i].id = i;
  }
  sort(b + 1, b + t + 1, lf1);
  for (int i = b[1].lf; i <= b[1].rt; i++) {
    add(i);
  }
  ans[b[1].id] = cur;
  ll lnw = b[1].lf;
  ll rnw = b[1].rt;
  for (int i = 2; i <= t; i++) {
    while (lnw < b[i].lf) {
      ers(lnw);
      lnw++;
    }
    while (rnw < b[i].rt) {
      rnw++;
      add(rnw);
    }
    while (lnw > b[i].lf) {

      lnw--;
      add(lnw);
    }
    while (rnw > b[i].rt) {
      ers(rnw);
      rnw--;
    }
    ans[b[i].id] = cur;
  }
  for (int i = 1; i <= t; i++) {
    cout << ans[i] << endl;
  }
}