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

#include <bits/stdc++.h>
using namespace std;
const int nax = 5e5 + 10;
/*
int lc[nax * 60], rc[nax * 60], root[nax * 60];
int sg[nax * 60];
int cnt;

inline void update (int &v, int x, int y, int pos, int val) {
    ++ cnt;
    sg[cnt] = sg[v];
    lc[cnt] = lc[v];
    rc[cnt] = rc[v];
    v = cnt;
    if (x == y) {
        sg[v] = val;
    } else {
        int mid = x + y >> 1;
        if (pos <= mid) update(lc[v], x, mid, pos, val);
        else update(rc[v], mid + 1, y, pos, val);
        sg[v] = sg[lc[v]] + sg[rc[v]];
    }
}

inline int query (int v, int x, int y, int l, int r) {
    if (x == y) {
        return sg[v];
    } else {
        int mid = x + y >> 1;
        if (l > mid) return query(rc[v], mid + 1, y, l, r);
        else if (r <= mid) return query(lc[v], x, mid, l, r);
        else return query(lc[v], x, mid, l, mid) + query(rc[v], mid + 1, y, mid
+ 1, r);
    }
}
*/

int n, q;
int c[nax];
int lst[nax];
int bit[nax];

void update(int i, int val) {
  for (; i < nax; i += i & -i)
    bit[i] += val;
}

int get(int i) {
  int res = 0;
  for (; i > 0; i -= i & -i)
    res += bit[i];
  return res;
}

vector<pair<int, int>> query[nax];
int ans[nax];

int main() {
  cin.tie(0)->sync_with_stdio(false);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }

  for (int i = 1; i <= q; ++i) {
    int l, r;
    cin >> l >> r;
    query[r].emplace_back(l, i);
  }

  for (int i = 1; i <= n; ++i) {
    if (lst[c[i]]) {
      update(lst[c[i]], -1);
    }
    update(i, 1);

    for (auto &[l, idx] : query[i]) {
      ans[idx] = get(i) - get(l - 1);
    }
    lst[c[i]] = i;
  }

  for (int i = 1; i <= q; ++i) {
    cout << ans[i] << '\\n';
  }
}


Error: TLE

Fixed submission:

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <bits/stdc++.h>
using namespace std;
const int nax = 5e5 + 10;
int lc[nax * 60], rc[nax * 60], root[nax * 60];
int sg[nax * 60];
int cnt;

// Fast IO
// ********************************************************************************************************
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template <class T = int> T readInt() {
  auto c = [&]() {
    if (__ir == __er)
      std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
  };
  c();
  while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-')
    ++__ir;
  c();
  bool m = false;
  if (*__ir == '-')
    ++__ir, c(), m = true;
  T r = 0;
  while (*__ir >= '0' && *__ir <= '9')
    r = r * 10 + *__ir - '0', ++__ir, c();
  ++__ir;
  return m ? -r : r;
}
string readString() {
  auto c = [&]() {
    if (__ir == __er)
      std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
  };
  string r;
  c();
  while (*__ir && isspace(*__ir))
    ++__ir, c();
  while (!isspace(*__ir))
    r.push_back(*__ir), ++__ir, c();
  ++__ir;
  return r;
}
char readChar() {
  auto c = [&]() {
    if (__ir == __er)
      std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
  };
  c();
  while (*__ir && isspace(*__ir))
    ++__ir, c();
  return *__ir++;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template <class T> void writeInt(T x, char endc = '\\n') {
  if (x < 0)
    *__iw++ = '-', x = -x;
  if (x == 0)
    *__iw++ = '0';
  char *s = __iw;
  while (x) {
    T t = x / 10;
    char c = x - 10 * t + '0';
    *__iw++ = c;
    x = t;
  }
  char *f = __iw - 1;
  while (s < f)
    swap(*s, *f), ++s, --f;
  if (__iw > __ew)
    cout.write(__buw, __iw - __buw), __iw = __buw;
  if (endc) {
    *__iw++ = endc;
  }
}
template <class T> void writeStr(const T &str) {
  int i = 0;
  while (str[i]) {
    *__iw++ = str[i++];
    if (__iw > __ew)
      cout.write(__buw, __iw - __buw), __iw = __buw;
  }
}
struct __FL__ {
  ~__FL__() {
    if (__iw != __buw)
      cout.write(__buw, __iw - __buw);
  }
};
static __FL__ __flushVar__;

////////////////// Thanks MrDindows

inline void update(int &v, int x, int y, int pos, int val) {
  ++cnt;
  sg[cnt] = sg[v];
  lc[cnt] = lc[v];
  rc[cnt] = rc[v];
  v = cnt;
  if (x == y) {
    sg[v] = val;
  } else {
    int mid = x + y >> 1;
    if (pos <= mid)
      update(lc[v], x, mid, pos, val);
    else
      update(rc[v], mid + 1, y, pos, val);
    sg[v] = sg[lc[v]] + sg[rc[v]];
  }
}

inline int query(int v, int x, int y, int l, int r) {
  if (x == y) {
    return sg[v];
  } else {
    int mid = x + y >> 1;
    if (l > mid)
      return query(rc[v], mid + 1, y, l, r);
    else if (r <= mid)
      return query(lc[v], x, mid, l, r);
    else
      return query(lc[v], x, mid, l, mid) +
             query(rc[v], mid + 1, y, mid + 1, r);
  }
}

int n, q;
int c[nax];
int lst[nax];
/**
    last appearance
    moving by r
*/

int main() {
  n = readInt(), q = readInt();
  for (int i = 1; i <= n; ++i) {
    c[i] = readInt();
  }

  for (int i = 1; i <= n; ++i) {
    int cur = root[i - 1];
    if (lst[c[i]]) {
      update(cur, 1, n, lst[c[i]], 0);
    }
    update(cur, 1, n, i, 1);
    root[i] = cur;
    lst[c[i]] = i;
  }

  for (int i = 1; i <= q; ++i) {
    int l, r;
    l = readInt(), r = readInt();
    writeInt(query(root[r], 1, n, l, r));
  }
}