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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define owo                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs\\n", d,                                           \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
int gcd(int A, int B) {
  if (!B)
    return A;
  return gcd(B, A % B);
}
const int MAXN = 5e5 + 5;
vector<ll> a;
VII t(4 * MAXN);
ll res = 0;
ll r1 = 0;
void build(int v, int l, int r) {
  if (l == r) {
    t[v].pb(a[l]);
    return;
  }
  int mid = (l + r) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  for (int x : t[2 * v])
    t[v].pb(x);
  for (int x : t[2 * v + 1])
    t[v].pb(x);
  sort(all(t[v]));
}
void get(int v, int l, int r, int tl, int tr) {
  if (l > r)
    return;
  if (tl == l && tr == r) {
    int p = ub(all(t[v]), r1) - t[v].begin();
    res += t[v].size() - p;
    return;
  }
  int tm = (tl + tr) / 2;
  get(2 * v, l, min(r, tm), tl, tm);
  get(2 * v + 1, max(l, tm + 1), r, tm + 1, tr);
}
int main() {
  owo int n, q;
  cin >> n >> q;
  a.resize(n + 1);
  vector<ll> occ(n + 1, n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i > 0; i--) {
    int tmp = a[i];
    a[i] = occ[a[i]];
    occ[tmp] = i;
  }
  build(1, 1, n);
  while (q--) {
    int l, r;
    cin >> l >> r;
    r1 = r;
    res = 0;
    get(1, l, r, 1, n);
    cout << res << '\\n';
  }
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) a.size()
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define owo                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define MOD (ll)(1e8)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs\\n", d,                                           \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<int, int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  else
    return gcd(b, a % b);
}
const int MAXN = 1e6 + 5;
ll st[MAXN], ed[MAXN], belong[MAXN], t[MAXN], sz[MAXN];
vector<ll> a;
void init(int n) {
  int num = sqrt(n);
  for (int i = 1; i <= num; i++) {
    st[i] = n / num * (i - 1) + 1;
    ed[i] = n / num * i;
  }
  ed[num] = n;
  for (int i = 1; i <= num; i++) {
    for (int j = st[i]; j <= ed[i]; j++) {
      belong[j] = i;
      t[j] = a[j];
    }
    sort(t + st[i], t + ed[i] + 1);
    sz[i] = ed[i] - st[i] + 1;
  }
}
ll get(int l, int r) {
  ll res = 0;
  int x = belong[l], y = belong[r];
  if (x == y) {
    for (int i = l; i <= r; i++) {
      if (a[i] > r)
        res++;
    }
    return res;
  }
  for (int i = l; i <= ed[x]; i++)
    if (a[i] > r)
      res++;
  for (int i = st[y]; i <= r; i++)
    if (a[i] > r)
      res++;
  for (int i = x + 1; i < y; i++) {
    int p = ub(t + st[i], t + ed[i] + 1, r) - t;
    p -= st[i];
    res += sz[i] - p;
    // cout<<size[i]<<" "<<p<<'\\n';
  }
  return res;
}
int main() {
  owo int n, q;
  cin >> n >> q;
  a.resize(n + 1);
  vector<ll> occ(n + 1, n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i > 0; i--) {
    int tmp = a[i];
    a[i] = occ[a[i]];
    occ[tmp] = i;
  }
  ///	for(int i=1;i<=n;i++)cout<<a[i]<<" ";
  // cout<<'\\n';
  init(n);
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << get(l, r) << '\\n';
  }
}