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

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

class node {
public:
  int cnt;
  node *left;
  node *right;

  node(int cnt, node *left, node *right) : cnt(cnt), left(left), right(right) {}

  node *insert(int x, int l, int h);
  node *update(int ind, int l, int h);
  int query(int l, int h, int s, int e);
};

node *root[500005];

node *null = new node(0, NULL, NULL);

node *node::insert(int x, int l, int h) {
  // cout<<l<<" "<<h<<"\\n";
  if (x >= l and x < h) {
    if (l + 1 == h)
      return new node(1, null, null);

    int m = (l + h) >> 1;

    return new node(this->cnt + 1, this->left->insert(x, l, m),
                    this->right->insert(x, m, h));
  }
  return this;
}

node *node::update(int ind, int l, int h) {
  if (ind >= l and ind < h) {
    if (l + 1 == h) {
      return new node(0, null, null);
    }

    int m = (l + h) >> 1;
    // cout<<ind<<"Yyyyy\\n";
    // this->cnt--;
    return new node((this->cnt) - 1, this->left->update(ind, l, m),
                    this->right->update(ind, m, h));
  }
  return this;
}

int node::query(int l, int h, int s, int e) {
  // cout<<l<<" "<<h<<" "<<this->cnt<<"\\n";

  if (l >= h)
    return 0;

  if (e <= l or s >= h)
    return 0;

  if (s <= l and e >= h)
    return this->cnt;

  int m = (l + h) >> 1;

  return this->left->query(l, m, s, e) + this->right->query(m, h, s, e);
}

int main() {
  fastio;
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int prev[500005];
  memset(prev, -1, sizeof(prev));
  null->left = null->right = null;
  root[0] = null->insert(0, 0, n);
  prev[a[0]] = 0;
  for (int i = 1; i < n; i++) {
    root[i] = (root[i - 1])->insert(i, 0, n);
    if (prev[a[i]] == -1)
      prev[a[i]] = i;
    else {
      root[i] = root[i]->update(prev[a[i]], 0, n);
      prev[a[i]] = i;
    }
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    // cout<<0<<"\\n";
    cout << root[y]->query(0, n, x, y + 1) << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

class node {
public:
  int cnt;
  node *left;
  node *right;

  node(int cnt, node *left, node *right) : cnt(cnt), left(left), right(right) {}

  node *insert(int x, int l, int h);
  node *update(int ind, int l, int h);
  int query(int l, int h, int s, int e);
};

node *root[500005];

node *null = new node(0, NULL, NULL);

node *node::insert(int x, int l, int h) {
  // cout<<l<<" "<<h<<"\\n";
  if (x >= l and x < h) {
    if (l + 1 == h)
      return new node(1, null, null);

    int m = (l + h) >> 1;

    return new node(this->cnt + 1, this->left->insert(x, l, m),
                    this->right->insert(x, m, h));
  }
  return this;
}

node *node::update(int ind, int l, int h) {
  if (ind >= l and ind < h) {
    if (l + 1 == h) {
      return new node(0, null, null);
    }

    int m = (l + h) >> 1;
    // cout<<ind<<"Yyyyy\\n";
    // this->cnt--;
    return new node((this->cnt) - 1, this->left->update(ind, l, m),
                    this->right->update(ind, m, h));
  }
  return this;
}

int node::query(int l, int h, int s, int e) {
  // cout<<l<<" "<<h<<" "<<this->cnt<<"\\n";

  if (l >= h)
    return 0;

  if (e <= l or s >= h)
    return 0;

  if (s <= l and e >= h)
    return this->cnt;

  int m = (l + h) >> 1;

  return this->left->query(l, m, s, e) + this->right->query(m, h, s, e);
}

int main() {
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int prev[500005];
  memset(prev, -1, sizeof(prev));
  null->left = null->right = null;
  root[0] = null->insert(0, 0, n);
  prev[a[0]] = 0;
  for (int i = 1; i < n; i++) {
    root[i] = (root[i - 1])->insert(i, 0, n);
    if (prev[a[i]] == -1)
      prev[a[i]] = i;
    else {
      root[i] = root[i]->update(prev[a[i]], 0, n);
      prev[a[i]] = i;
    }
  }
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    // cout<<0<<"\\n";
    cout << root[y]->query(0, n, x, y + 1) << "\\n";
  }
  return 0;
}