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

// #include<i_am_noob_orz>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define int ll
#define ull unsigned long long
#define pii pair<int, int>
#define X first
#define Y second
#define mod ((ll)1e9 + 7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x) > 0 ? (x) : (-(x)))
#define F(n) Fi(i, n)
#define Fi(i, n) Fl(i, 0, n)
#define Fl(i, l, n) for (int i = l; i < n; i++)
#define memres(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#define ceiling(a, b) (((a) + (b)-1) / (b))
#define endl '\\n'
#define bit_count(x) __builtin_popcount((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #define LOCAL
#ifdef LOCAL
#define debug(a) cerr << #a << " " << a << endl;
#else
#define debug(a)
#endif
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int col[500010], cnt = 0, ar[500010], o[500010];
struct NODE {
  int l, r, lbl, ind;
} qry[500010];
bool cmp(NODE a, NODE b) {
  if (a.lbl == b.lbl)
    return (a.r < b.r);
  return a.lbl < b.lbl;
}
inline void add(int x) {
  if (!col[x])
    cnt++;
  col[x]++;
}
inline void sub(int x) {
  col[x]--;
  if (!col[x])
    cnt--;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  memres(col);
  F(n) cin >> ar[i];
  int m = sqrt(n);
  F(q) {
    int l, r, bl;
    cin >> l >> r;
    --l, --r;
    bl = l / m;
    qry[i] = {l, r, bl, i};
  }
  sort(qry, qry + q, cmp);
  int l = 0, r = 0;
  add(ar[0]);
  F(q) {
    while (l < qry[i].l)
      sub(ar[l++]);
    while (l > qry[i].l)
      add(ar[--l]);
    while (r > qry[i].r)
      sub(ar[r--]);
    while (r < qry[i].r)
      add(ar[++r]);
    o[qry[i].ind] = cnt;
  }
  F(q) cout << o[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

// #include<i_am_noob_orz>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int, int>
#define X first
#define Y second
#define mod ((ll)1e9 + 7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x) > 0 ? (x) : (-(x)))
#define F(n) Fi(i, n)
#define Fi(i, n) Fl(i, 0, n)
#define Fl(i, l, n) for (int i = l; i < n; i++)
#define memres(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#define ceiling(a, b) (((a) + (b)-1) / (b))
#define endl '\\n'
#define bit_count(x) __builtin_popcount((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #define LOCAL
#ifdef LOCAL
#define debug(a) cerr << #a << " " << a << endl;
#else
#define debug(a)
#endif
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int col[500010], cnt = 0, ar[500010], o[500010];
struct NODE {
  int l, r, lbl, ind;
} qry[500010];
bool cmp(NODE a, NODE b) {
  if (a.lbl == b.lbl)
    return (a.l < b.l);
  return a.lbl < b.lbl;
}
void add(int x) {
  if (!col[x])
    cnt++;
  col[x]++;
}
void sub(int x) {
  col[x]--;
  if (!col[x])
    cnt--;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  memres(col);
  F(n) cin >> ar[i];
  int m = sqrt(n);
  F(q) {
    int l, r, bl;
    cin >> l >> r;
    --l, --r;
    bl = l / m;
    qry[i] = {l, r, bl, i};
  }
  sort(qry, qry + q, cmp);
  int l = 0, r = 0;
  add(ar[0]);
  F(q) {
    while (l < qry[i].l)
      sub(ar[l++]);
    while (l > qry[i].l)
      add(ar[--l]);
    while (r > qry[i].r)
      sub(ar[r--]);
    while (r < qry[i].r)
      add(ar[++r]);
    o[qry[i].ind] = cnt;
  }
  F(q) cout << o[i] << endl;
  return 0;
}