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

#include <bits/stdc++.h>
using namespace std;
#define PI acos(-1)
#define INF 0x3f3f3f
#define ll long long
#define pb push_back
#define mp make_pair
#define pair pair<int, int>
#define C(a) cout << a << endl;
#define CY cout << "YES" << endl;
#define CN cout << "NO" << endl;
#define Cy cout << "Yes" << endl;
#define Cn cout << "No" << endl;
#define ull unsigned long long
#define me(a) memset(a, 0, sizeof(a));
#define my(a, b) memcpy(a, b, sizeof(b));
#define cio ios::sync_with_stdio(false)
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return (a % b == 0) ? b : gcd(b, a % b); }
ll s[501000];
int rt[501000];
int lc[20000100], rc[20000100], tol;
ll sum[20000100];
map<ll, ll> vis;
void init() {
  tol = 1;
  vis.clear();
}
void update(int &u, int x, int y, int p, ll v) {
  sum[tol] = sum[u] + v;
  lc[tol] = lc[u];
  rc[tol] = rc[u];
  u = tol++;
  if (x == y)
    return;
  int m = (x + y) / 2;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
ll query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr) {
    return sum[u];
  }
  int m = (x + y) / 2;
  ll ans = 0;
  if (ql <= m) {
    ans += query(lc[u], x, m, ql, qr);
  }
  if (qr > m) {
    ans += query(rc[u], m + 1, y, ql, qr);
  }
  return ans;
}
inline void solve(int n, int q) {

  for (int i = 1; i <= n; i++) {
    scanf("%lld", &s[i]);
  }
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (vis.find(s[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[s[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[s[i]] = i;
  }
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    ll g = query(rt[r], 1, n, l, r);
    printf("%lld\\n", g);
  }
}
int main() {
  cio;
  int n, q;
  init();
  scanf("%d %d", &n, &q);
  solve(n, q);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI acos(-1)
#define INF 0x3f3f3f
#define ll long long
#define pb push_back
#define mp make_pair
#define pair pair<int, int>
#define C(a) cout << a << endl;
#define CY cout << "YES" << endl;
#define CN cout << "NO" << endl;
#define Cy cout << "Yes" << endl;
#define Cn cout << "No" << endl;
#define ull unsigned long long
#define me(a) memset(a, 0, sizeof(a));
#define my(a, b) memcpy(a, b, sizeof(b));
#define cio ios::sync_with_stdio(false)
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return (a % b == 0) ? b : gcd(b, a % b); }
int s[501000], rt[501000];
int lc[20000100], rc[20000100], tol;
ll sum[20000100];
map<ll, ll> vis;
void init() {
  tol = 1;
  vis.clear();
}
void update(int &u, int x, int y, int p, ll v) {
  sum[tol] = sum[u] + v;
  lc[tol] = lc[u];
  rc[tol] = rc[u];
  u = tol++;
  if (x == y)
    return;
  int m = (x + y) / 2;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
ll query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr) {
    return sum[u];
  }
  int m = (x + y) / 2;
  ll ans = 0;
  if (ql <= m) {
    ans += query(lc[u], x, m, ql, qr);
  }
  if (qr > m) {
    ans += query(rc[u], m + 1, y, ql, qr);
  }
  return ans;
}
inline void solve(int n, int q) {

  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (vis.find(s[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[s[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[s[i]] = i;
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    int g = query(rt[r], 1, n, l, r);
    cout << g << endl;
  }
}
int main() {
  cio;
  int n, q;
  init();
  cin >> n >> q;
  solve(n, q);
  return 0;
}