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

// Code by Nguyen Huu Lam
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef int LL;
typedef pair<LL, LL> II;

#define y0 Sword_Art_Online
#define y1 Your_lie_in_April
#define yn Darling_in_the_Franxx
#define tm Plastic_Memories
#define lr Charlotte
#define norm Weathering_with_you
#define left Violet_Evergarden
#define have Date_a_live
#define ends Your_name
#define prev Five_centimeters_per_second
#define hash Silent_voice
#define endl "\\n"

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

LL Rand(LL l, LL h) { return uniform_int_distribution<LL>(l, h)(rng); }

template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while (isdigit(ch));
  t *= f;
}

const LL MaxN = 1 + 5e5;

LL n, a[MaxN], q, bloc, mark[MaxN], ans[MaxN], vt[MaxN];
vector<LL> query[MaxN], pos[MaxN];

class Tree {

  LL TNode[4 * MaxN], ri[4 * MaxN], le[4 * MaxN], leaf[MaxN], qi, qj;

  LL Find(LL node) {
    if (le[node] > qj || ri[node] < qi)
      return 0;
    if (qi <= le[node] && ri[node] <= qj)
      return TNode[node];
    return (Find(node * 2) + Find(node * 2 + 1));
  }

public:
  void createTree(LL node, LL l, LL r) {
    le[node] = l;
    ri[node] = r;
    if (l == r) {
      leaf[l] = node;
      TNode[node] = 0;
      return;
    }
    createTree(node * 2, l, (l + r) / 2);
    createTree(node * 2 + 1, (l + r) / 2 + 1, r);
  }

  void update(LL node, LL val) {
    LL h = leaf[node];
    TNode[h] = (TNode[h] + val);
    for (h /= 2; h; h /= 2)
      TNode[h] = (TNode[h * 2] + TNode[h * 2 + 1]);
  }

  LL query(LL l, LL r) {
    qi = l;
    qj = r;
    return Find(1);
  }
} tr;

void InOut() {
#define TASK "ABC"
  freopen(TASK ".inp", "r", stdin);
  freopen(TASK ".out", "w", stdout);
}

int main() {
  // InOut();
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 0; i < q; ++i) {
    LL x, y;
    cin >> x >> y;
    query[y].push_back(x);
    pos[y].push_back(i);
  }
  tr.createTree(1, 1, n);
  for (int i = 1; i <= n; ++i) {
    if (mark[a[i]])
      tr.update(mark[a[i]], -1);
    tr.update(i, 1);
    mark[a[i]] = i;
    for (int j = 0; j < query[i].size(); ++j) {
      ans[pos[i][j]] = tr.query(query[i][j], i);
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

// Code by Nguyen Huu Lam
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef int LL;
typedef pair<LL, LL> II;

#define y0 Sword_Art_Online
#define y1 Your_lie_in_April
#define yn Darling_in_the_Franxx
#define tm Plastic_Memories
#define lr Charlotte
#define norm Weathering_with_you
#define left Violet_Evergarden
#define have Date_a_live
#define ends Your_name
#define prev Five_centimeters_per_second
#define hash Silent_voice
#define endl "\\n"

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

LL Rand(LL l, LL h) { return uniform_int_distribution<LL>(l, h)(rng); }

template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while (isdigit(ch));
  t *= f;
}

const LL MaxN = 1 + 5e5;

LL n, a[MaxN], q, bloc, mark[MaxN], ans[MaxN], cnt;

class Task {

  LL bloc = sqrt(n);

public:
  LL id, l, r;

  Task(LL _id = 0, LL _l = 0, LL _r = 0) : id(_id), l(_l), r(_r) {}

  bool operator<(const Task &op) const {
    if (l / bloc != op.l / bloc)
      return l / bloc < op.l / bloc;
    return r / bloc < op.r / bloc;
  }
} t[MaxN];

void InOut() {
#define TASK "ABC"
  freopen(TASK ".inp", "r", stdin);
  freopen(TASK ".out", "w", stdout);
}

int main() {
  // InOut();
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 0; i < q; ++i) {
    LL x, y;
    cin >> x >> y;
    t[i] = Task(i, x, y);
    // cout << x << " " << y << endl;
    // cout << t[i].id << " " << t[i].l << " " << t[i].r << endl;
  }
  sort(t, t + q);
  // for(int i = 0 ; i < q ; ++i) cout << t[i].id << " " << t[i].l << " " <<
  // t[i].r << endl;
  LL le = t[0].l, ri = t[0].r;
  for (int i = t[0].l; i <= t[0].r; ++i) {
    mark[a[i]]++;
    if (mark[a[i]] == 1)
      cnt++;
    ans[t[0].id] = cnt;
  }
  for (int i = 1; i < q; ++i) {
    if (t[i].l < le) {
      for (int j = t[i].l; j < le; ++j) {
        mark[a[j]]++;
        if (mark[a[j]] == 1)
          cnt++;
      }
    } else {
      for (int j = le; j < t[i].l; ++j) {
        mark[a[j]]--;
        if (mark[a[j]] == 0)
          cnt--;
      }
    }
    if (t[i].r > ri) {
      for (int j = ri + 1; j <= t[i].r; ++j) {
        mark[a[j]]++;
        if (mark[a[j]] == 1)
          cnt++;
      }
    } else {
      for (int j = t[i].r + 1; j <= ri; ++j) {
        mark[a[j]]--;
        if (mark[a[j]] == 0)
          cnt--;
      }
    }
    le = t[i].l;
    ri = t[i].r;
    ans[t[i].id] = cnt;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;

  return 0;
}