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

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
// Complexity: sum:O(logn), add:O(logn)
template <typename T> struct BinaryIndexedTree {
  int sz;
  vector<T> data;
  BinaryIndexedTree(int sz) : sz(sz), data(sz + 1) {}
  T sum(int k) {
    // [0, k]
    T res = 0;
    for (k++; k > 0; k -= k & -k)
      res += data[k];
    return res;
  }
  T sum(int l, int r) {
    // [l, r)
    return sum(r - 1) - sum(l - 1);
  }
  void add(int k, T x) {
    for (k++; k <= sz; k += k & -k)
      data[k] += x;
  }
};
#pragma endregion library

int main() {
  int n, q;
  cin >> n >> q;
  vi c(n);
  rep(i, n) cin >> c[i];
  vi prv(n + 1, -1);
  vector<pi> query;
  vector<map<int, int>> ans(n + 1);
  BinaryIndexedTree<int> bt(n + 1);
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    query.emplace_back(l, r);
    ans[r].emplace(l, 0);
  }
  rep(i, n) {
    int r = i + 1;
    if (prv[c[i]] != -1) {
      bt.add(prv[c[i]], 1);
    }
    prv[c[i]] = i;
    for (auto it = ans[r].begin(); it != ans[r].end(); it++) {
      int l = it->first;
      int val = r - l + 1 - bt.sum(l - 1, r);
      it->second = val;
    }
  }
  rep(i, q) {
    int l, r;
    tie(l, r) = query[i];
    cout << ans[r][l] << '\\n';
  }
}


Error: TLE

Fixed submission:

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
  preprocess() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} ____;
#pragma endregion head

#pragma region library
#pragma endregion library

int main() {
  int n, q;
  cin >> n >> q;
  vi c(n);
  rep(i, n) cin >> c[i];
  vector<set<int>> st(n + 1);
  vi prv(n + 1, -1);
  rep(i, n) {
    st[i + 1] = st[i];
    if (prv[c[i]] != -1) {
      st[i + 1].erase(prv[c[i]]);
    }
    st[i + 1].emplace(i);
    prv[c[i]] = i;
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    int big = distance(st[r].begin(), st[r].lower_bound(r));
    int small = distance(st[r].begin(), st[r].lower_bound(l - 1));
    cout << big - small << '\\n';
  }
}