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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl

// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }

  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }

  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  int n, q;
  cin >> n >> q;

  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<vector<pii>> querys(n);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    querys[l].push_back({r, i});
  }

  vector<vector<int>> points(n);
  vector<int> prev(n + 1, -1);
  for (int i = 0; i < n; i++) {
    int p = prev[a[i]];
    if (p != -1) {
      points[p].push_back(i);
    }
    prev[a[i]] = i;
  }

  vector<int> ans(q);
  BIT<int> data(n);
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < points[i].size(); j++) {
      data.add(points[i][j], 1);
    }
    for (int j = 0; j < querys[i].size(); j++) {
      int r = querys[i][j].first;
      int index = querys[i][j].second;
      ans[index] = (r - i + 1) - data.sum(r);
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD ll(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
template <typename T>

static vector<set<int>> initialize(const T &input) {
  const size_t n = input.size();
  assert((n & (n - 1)) == 0);
  vector<set<int>> data(n * 2 - 1);
  for (size_t i = 0; i < n; ++i) {
    data[i + n - 1].insert(all(input[i]));
  }
  for (ptrdiff_t i = n - 2; i >= 0; --i) {
    data[i].insert(all(data[i * 2 + 1]));
    data[i].insert(all(data[i * 2 + 2]));
  }
  return data;
}

class BottomupSegmentTree {

private:
  vector<set<int>> m_data;

public:
  template <typename T>
  explicit BottomupSegmentTree(const T &input) : m_data(initialize(input)) {}

  set<int> query(size_t l, size_t r) const {
    const size_t n = (m_data.size() + 1) / 2;
    set<int> y;
    l += (n - 1);
    r += (n - 1);
    while (l < r) {
      if ((l & 1u) == 0u) {
        y.insert(all(m_data[l]));
      }
      if ((r & 1u) == 0u) {
        y.insert(all(m_data[r - 1]));
      }
      l = l / 2;
      r = (r - 1) / 2;
    }
    return y;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  set<int> init_val;
  int num = 1;
  while (num < n) {
    num *= 2;
  }
  vector<set<int>> a(num, init_val);
  for (int i = 0; i < n; i++) {
    int c;
    cin >> c;
    a[i].insert(c);
  }
  BottomupSegmentTree st(a);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    set<int> ret = st.query(l, r + 1);
    int t = ret.size();
    printf("%d\\n", t);
  }
}