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

#include <bits/stdc++.h>

using namespace std;

class mergeSort {
public:
  mergeSort(int _N, int *_data) : N(_N), data(_data) {
    tree.resize(N * 4);
    build(0, 0, N - 1);
  }
  int query(int sq, int eq, int k) { return query(0, 0, N - 1, sq, eq, k); }

private:
  int N, *data;
  vector<vector<int>> tree;
  void build(int node, int s, int e) {
    if (s == e) {
      tree[node].push_back(data[s]);
    } else {
      int lc = node * 2 + 1;
      int rc = node * 2 + 2;
      int mid = s + (e - s) / 2;
      build(lc, s, mid);
      build(rc, mid + 1, e);
      tree[node].resize(e - s + 1);
      merge(tree[lc].begin(), tree[lc].end(), tree[rc].begin(), tree[rc].end(),
            tree[node].begin());
    }
  }

  inline int query(int node, int s, int e, int sq, int eq, int k) {
    if (s > eq || e < sq)
      return 0;
    if (s >= sq && e <= eq)
      return tree[node].end() -
             lower_bound(tree[node].begin(), tree[node].end(), k);
    int lc = node * 2 + 1;
    int rc = node * 2 + 2;
    int mid = s + (e - s) / 2;
    return query(lc, s, mid, sq, eq, k) + query(rc, mid + 1, e, sq, eq, k);
  }
};

int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int &i : c) {
    cin >> i;
  }
  vector<int> lastOcc(N + 1, N + 1);
  vector<int> a(N);
  for (int i = N - 1; i >= 0; i--) {
    a[i] = lastOcc[c[i]];
    lastOcc[c[i]] = i;
  }
  mergeSort ms(N, a.data());
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << ms.query(l, r, r + 1) << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

class mergeSort {
public:
  mergeSort(int _N, int *_data) : N(_N), data(_data) {
    tree.resize(N * 4);
    build(0, 0, N - 1);
  }
  int query(int sq, int eq, int k) { return query(0, 0, N - 1, sq, eq, k); }

private:
  int N, *data;
  vector<vector<int>> tree;
  void build(int node, int s, int e) {
    if (s == e) {
      tree[node].push_back(data[s]);
    } else {
      int lc = node * 2 + 1;
      int rc = node * 2 + 2;
      int mid = s + (e - s) / 2;
      build(lc, s, mid);
      build(rc, mid + 1, e);
      tree[node].resize(e - s + 1);
      merge(tree[lc].begin(), tree[lc].end(), tree[rc].begin(), tree[rc].end(),
            tree[node].begin());
    }
  }

  int query(int node, int s, int e, int sq, int eq, int k) {
    if (s > eq || e < sq)
      return 0;
    if (s >= sq && e <= eq) {
      vector<int>::iterator it =
          lower_bound(tree[node].begin(), tree[node].end(), k);
      return tree[node].end() - it;
    }
    int lc = node * 2 + 1;
    int rc = node * 2 + 2;
    int mid = s + (e - s) / 2;
    return query(lc, s, mid, sq, eq, k) + query(rc, mid + 1, e, sq, eq, k);
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int &i : c) {
    cin >> i;
  }
  vector<int> lastOcc(N + 1, N + 1);
  vector<int> a(N);
  for (int i = N - 1; i >= 0; i--) {
    a[i] = lastOcc[c[i]];
    lastOcc[c[i]] = i;
  }
  mergeSort ms(N, a.data());
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << ms.query(l, r, r + 1) << '\\n';
  }
  return 0;
}