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

/*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;

#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 1000000000
#define PI 2 * acos(0.0)
#define dbg(a) cout << #a << " ->->->-> " << a << "\\n"
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};

ll bigmod(ll x, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return res;
}

//=============================================ASIFAZAD==============================================//
const int N = 500050;
vi col(N), tree(4 * N);

void upd(int cur, int strt, int en, int ind, int val) {
  if (strt == en)
    tree[cur] = val;
  else {
    int mid = (strt + en) / 2;
    if (ind <= mid)
      upd(2 * cur, strt, mid, ind, val);
    else
      upd(2 * cur + 1, mid + 1, en, ind, val);
    tree[cur] = tree[2 * cur] + tree[2 * cur + 1];
  }
}

int dif(int cur, int strt, int en, int l, int r) {
  if (strt > r || en < l)
    return 0;
  if (strt >= l && en <= r)
    return tree[cur];
  int mid = (strt + en) / 2;
  return dif(2 * cur, strt, mid, l, r) + dif(2 * cur + 1, mid + 1, en, l, r);
}

int32_t main() {
  io;
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> col[i];

  vector<pair<int, pii>> qr;
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qr.pb({r, {l, i}});
  }
  sort(qr.begin(), qr.end());

  vi ans(q + 1), lst(n + 1, -1);

  for (int i = 1, actq = 0; i <= n; i++) {
    if (lst[col[i]] == -1) {
      lst[col[i]] = i;
      upd(1, 1, n, i, 1);
    } else {
      upd(1, 1, n, lst[col[i]], 0);
      lst[col[i]] = i;
      upd(1, 1, n, i, 1);
    }

    while (qr[actq].first == i) {
      int l = qr[actq].second.first, r = qr[actq].first,
          ind = qr[actq].second.second;
      ans[ind] = dif(1, 1, n, l, r);
      actq++;
    }
  }

  for (int i = 1; i <= q; i++)
    cout << ans[i] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

/*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;

#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 1000000000
#define PI 2 * acos(0.0)
#define dbg(a) cout << #a << " ->->->-> " << a << "\\n"
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};

ll bigmod(ll x, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return res;
}

//=============================================ASIFAZAD==============================================//
const int N = 500050;
set<int> dif[4 * N];
vi inp(N);

void build(int cur, int strt, int en) {
  if (strt == en)
    dif[cur].insert(inp[strt]);
  else {
    int mid = (strt + en) / 2;
    build(2 * cur, strt, mid);
    build(2 * cur + 1, mid + 1, en);

    for (auto x : dif[2 * cur])
      dif[cur].insert(x);
    for (auto x : dif[2 * cur + 1])
      dif[cur].insert(x);
  }
}

set<int> wolf(int cur, int strt, int en, int l, int r) {
  set<int> ret;
  if (strt > r || en < l)
    return ret;
  if (strt >= l && en <= r)
    return dif[cur];
  int mid = (strt + en) / 2;
  set<int> a, b;
  a = wolf(2 * cur, strt, mid, l, r);
  b = wolf(2 * cur + 1, mid + 1, en, l, r);
  for (auto x : a)
    ret.insert(x);
  for (auto x : b)
    ret.insert(x);
  return ret;
}

int32_t main() {
  io;
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> inp[i];

  build(1, 1, n);
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << wolf(1, 1, n, l, r).size() << "\\n";
  }
  return 0;
}