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

#include <bits/stdc++.h>
#define del(a, i) memset(a, i, sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l + r) >> 1)
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
using namespace std;
template <class T> inl T read() {
  T x = 0;
  int f = 1;
  char k = getchar();
  for (; k > '9' || k < '0'; k = getchar())
    if (k == '-')
      f = -1;
  for (; k >= '0' && k <= '9'; k = getchar())
    x = x * 10 + k - '0';
  return x * f;
}
template <class T> inl T read(T &x) { return x = read<T>(); }
int _num, _buf[20];
template <class T> il print(T x) {
  if (x == 0)
    return putchar('0'), void();
  if (x < 0)
    putchar('-'), x = -x;
  while (x)
    _buf[++_num] = x % 10, x /= 10;
  while (_num)
    putchar(_buf[_num--] + '0');
}
ll mul(ll a, ll b, ll mod) {
  long double c = 1.;
  return (a * b - (ll)(c * a * b / mod) * mod) % mod;
}
it qpow(int x, int m, int mod) {
  int res = 1, bas = x;
  while (m) {
    if (m & 1)
      res = (1ll * res * bas) % mod;
    bas = (1ll * bas * bas) % mod, m >>= 1;
  }
  return res;
}
const int N = 5e5 + 5, M = N * 40;
int n, q, lst[N], pre[N], col[N];
int num, rt[N], cnt[M], lc[M], rc[M];
il Build(int &cur, int l, int r) {
  cur = ++num;
  if (l == r)
    return;
  Build(lc[cur], l, mid);
  Build(rc[cur], mid + 1, r);
}
il Insert(int &cur, int cmp, int l, int r, int pos) {
  cur = ++num, lc[cur] = lc[cmp], rc[cur] = rc[cmp], cnt[cur] = cnt[cmp] + 1;
  if (l == r)
    return;
  if (mid >= pos)
    Insert(lc[cur], lc[cmp], l, mid, pos);
  else
    Insert(rc[cur], rc[cmp], mid + 1, r, pos);
}
it Query(int cur, int l, int r, int L, int R) {
  if (l >= L && r <= R)
    return cnt[cur];
  int res = 0;
  if (mid >= L)
    res += Query(lc[cur], l, mid, L, R);
  if (R > mid)
    res += Query(rc[cur], mid + 1, r, L, R);
  return res;
}
int main() {
  //	freopen( ".in", "r", stdin ) ;
  //	freopen( ".out", "w", stdout ) ;
  n = read<int>(), q = read<int>();
  for (ri i = 1; i <= n; ++i) {
    col[i] = read<int>();
    pre[i] = lst[col[i]];
    lst[col[i]] = i;
  }
  Build(rt[0], 0, n - 1);
  for (ri i = 1; i <= n; ++i)
    Insert(rt[i], rt[i - 1], 0, n - 1, pre[i]);
  for (ri i = 1; i <= q; ++i) {
    int l = read<int>(), r = read<int>();
    print(Query(rt[r], 0, n - 1, 0, l - 1) -
          Query(rt[l - 1], 0, n - 1, 0, l - 1)),
        puts("");
  }
  cerr << 1. * clock() / CLOCKS_PER_SEC << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#define il inline
#define rg register
#define vd void
#define sta static
typedef long long ll;
il int gi() {
  rg int x = 0, f = 1;
  rg char ch = getchar();
  while (ch < '0' || ch > '9')
    f = ch == '-' ? -1 : f, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
const int maxn = 500011;
int n;
namespace BITSegtree {
#define mid ((l + r) >> 1)
int rt[maxn], ls[maxn * 100], rs[maxn * 100], sum[maxn * 100], index;
il vd _update(int &x, int l, int r, const int &p, const int &k) {
  if (!x)
    x = ++index;
  sum[x] += k;
  if (l == r)
    return;
  if (p <= mid)
    _update(ls[x], l, mid, p, k);
  else
    _update(rs[x], mid + 1, r, p, k);
}
il int _query(int x, int l, int r, const int &L, const int &R) {
  if (!x || r < L || R < l)
    return 0;
  if (L <= l && r <= R)
    return sum[x];
  return _query(ls[x], l, mid, L, R) + _query(rs[x], mid + 1, r, L, R);
}
il int lb(const int &x) { return x & -x; }
il vd Update(int x, int y) {
  while (x <= n)
    _update(rt[x], 1, n + 1, y + 1, 1), x += lb(x);
}
il int Query(int l, int r, int x, int y) {
  sta int ret;
  ret = 0;
  while (r)
    ret += _query(rt[r], 1, n + 1, x + 1, y + 1), r -= lb(r);
  --l;
  while (l)
    ret -= _query(rt[l], 1, n + 1, x + 1, y + 1), l -= lb(l);
  return ret;
}
} // namespace BITSegtree
int lst[1000001];
int main() {
  freopen("diff.in", "r", stdin);
  freopen("diff.out", "w", stdout);
  using namespace BITSegtree;
  n = gi();
  int k;
  for (rg int i = 1; i <= n; ++i)
    k = gi(), Update(i, lst[k]), lst[k] = i;
  int l, r, m = gi();
  while (m--)
    l = gi(), r = gi(), printf("%d\\n", Query(l, r, 0, l - 1));
  return 0;
}