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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 5e5 + 4;
int n, m;
int root[N], sum[N * 100], lc[N * 100], rc[N * 100];
int tim;
int nxt[N];
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
inline void pushup(int rt) { sum[rt] = sum[lc[rt]] + sum[rc[rt]]; }
inline void build(int &rt, int l, int r) {
  rt = ++tim;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(lc[rt], l, mid);
  build(rc[rt], mid + 1, r);
}
inline void modify(int &rt, int pre, int l, int r, int pos, int val) {
  rt = ++tim;
  lc[rt] = lc[pre];
  rc[rt] = rc[pre];
  sum[rt] = sum[pre] + val;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (pos <= mid)
    modify(lc[rt], lc[pre], l, mid, pos, val);
  else
    modify(rc[rt], rc[pre], mid + 1, r, pos, val);
  pushup(rt);
}
inline int query(int rt, int l, int r, int L) {
  if (l == r)
    return sum[rt];
  int mid = l + r >> 1;
  if (mid >= L)
    return sum[rc[rt]] + query(lc[rt], l, mid, L);
  else
    return query(rc[rt], mid + 1, r, L);
}
int main() {
  // freopen("f3.txt","r",stdin);
  n = read(), m = read();
  build(root[0], 1, n);
  for (register int i = 1; i <= n; ++i) {
    int x = read();
    if (!nxt[x]) {
      modify(root[i], root[i - 1], 1, n, i, 1);
    } else {
      int temp;
      modify(temp, root[i - 1], 1, n, nxt[x], -1);
      modify(root[i], temp, 1, n, i, 1);
    }
    nxt[x] = i;
  }
  while (m--) {
    int x = read(), y = read();
    printf("%d\\n", query(root[y], 1, n, x));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 5e5 + 4;
int n, m;
int root[N], sum[N * 100], lc[N * 100], rc[N * 100];
int tim;
int nxt[N];
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x * f;
}
inline void pushup(int rt) { sum[rt] = sum[lc[rt]] + sum[rc[rt]]; }
inline void build(int &rt, int l, int r) {
  rt = ++tim;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(lc[rt], l, mid);
  build(rc[rt], mid + 1, r);
}
inline void modify(int &rt, int pre, int l, int r, int pos, int val) {
  rt = ++tim;
  lc[rt] = lc[pre];
  rc[rt] = rc[pre];
  sum[rt] = sum[pre] + val;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (pos <= mid)
    modify(lc[rt], lc[pre], l, mid, pos, val);
  else
    modify(rc[rt], rc[pre], mid + 1, r, pos, val);
  pushup(rt);
}
inline int query(int rt, int l, int r, int L) {
  if (l == r)
    return sum[rt];
  int mid = l + r >> 1;
  if (mid >= L)
    return sum[rc[rt]] + query(lc[rt], l, mid, L);
  else
    return query(rc[rt], mid + 1, r, L);
}
int main() {
  freopen("f3.txt", "r", stdin);
  n = read(), m = read();
  build(root[0], 1, n);
  for (register int i = 1; i <= n; ++i) {
    int x = read();
    if (!nxt[x]) {
      modify(root[i], root[i - 1], 1, n, i, 1);
    } else {
      int temp;
      modify(temp, root[i - 1], 1, n, nxt[x], -1);
      modify(root[i], temp, 1, n, i, 1);
    }
    nxt[x] = i;
  }
  while (m--) {
    int x = read(), y = read();
    printf("%d\\n", query(root[y], 1, n, x));
  }
  return 0;
}