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

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl;
#define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) std::cout << x << endl;
#define println(x) std::cout << x << endl;
using namespace std;
// using namespace atcoder;

// 1-indexed, sum
class BIT {
public:
  ll n;
  vector<ll> bit;
  BIT(ll n) : n(n), bit(n + 1, 0) {}

  void add(ll i, ll x) {
    if (i == 0)
      return;
    for (ll k = i; k <= n; k += (k & -k)) {
      bit[k] += x;
    }
  }

  ll sum(ll i) {
    ll s = 0;
    if (i == 0)
      return s;
    for (ll k = i; k > 0; k -= (k & -k)) {
      s += bit[k];
    }
    return s;
  }

  ll lower_bound(ll x) {
    if (x <= 0) {
      return 0;
    } else {
      ll i = 0;
      ll r = 1;
      while (r < n)
        r = r << 1;
      for (int len = r; len > 0; len = len >> 1) {
        if (i + len < n && bit[i + len] < x) {
          x -= bit[i + len];
          i += len;
        }
      }
      return i + 1;
    }
  }
};

void Main() {
  ll n, Q;
  cin >> n >> Q;

  vector<ll> c(n);
  rep(i, n) cin >> c[i];

  vector<ll> l(Q), r(Q);
  rep(i, Q) cin >> l[i] >> r[i];

  vector<tuple<ll, ll, ll>> qs;
  rep(i, Q) { qs.push_back(make_tuple(i, l[i], r[i])); }

  sort(all(qs), [](const auto &l, const auto &r) {
    return std::get<2>(l) < std::get<2>(r);
  });

  BIT bit(n + 1);
  vector<int> pos(n + 1, -1);

  ll j = 0;
  vector<ll> res(Q);
  rep(i, Q) {
    ll qi = std::get<0>(qs[i]);
    ll a = std::get<1>(qs[i]) - 1;
    ll b = std::get<2>(qs[i]);

    while (j < b) {
      if (pos[c[j]] >= 0) {
        bit.add(1 + pos[c[j]], -1);
      }
      pos[c[j]] = j;
      bit.add(1 + pos[c[j]], 1);
      j++;
    }

    ll sumB = bit.sum(b);
    ll sumA = bit.sum(a);

    ll ans = sumB - sumA;
    res[qi] = ans;
  }

  rep(i, Q) put(res[i]);
}
signed main() {
  Main();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) std::cout << ((f) ? "Yes" : "No") << endl;
#define YORN(f) std::cout << ((f) ? "YES" : "NO") << endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) std::cout << x << endl;
#define println(x) std::cout << x << endl;
using namespace std;
// using namespace atcoder;

// Segment tree
struct F { // min func
  typedef int TT;
  set<int> d;
  F() {}
  F(int x) { d.emplace(x); }
  F &operator+=(const F &f) {
    d.insert(all(f.d));
    return *this;
  }
  F operator+(const F &f) const { return F(*this) += f; }
};

struct seg {
  vector<F> d;
  int n;
  seg() {}
  seg(int mx) {
    n = 1;
    while (n < mx)
      n <<= 1;
    d = vector<F>(n << 1);
  }
  void upd(int i) { d[i] = d[i << 1] + d[i << 1 | 1]; }
  F &operator[](int i) { return d[n + i]; }
  void init() {
    for (int i = n - 1; i; --i)
      upd(i);
  }
  void add(int i, F f) {
    for (i += n, d[i] += f, i >>= 1; i; i >>= 1)
      upd(i);
  }
  void set(int i, F f) {
    for (i += n, d[i] = f, i >>= 1; i; i >>= 1)
      upd(i);
  }
  F get(int l, int r) {
    l += n;
    r += n;
    F lf, rf;
    while (l < r) {
      if (l & 1)
        lf += d[l++];
      if (r & 1)
        rf = d[--r] + rf;
      l >>= 1;
      r >>= 1;
    }
    return lf += rf;
  }
};

void Main() {
  ll n, Q;
  cin >> n >> Q;

  vector<ll> c(n);
  rep(i, n) cin >> c[i];

  vector<ll> l(Q), r(Q);
  rep(i, Q) cin >> l[i] >> r[i];

  seg t(n);

  rep(i, n) { t.set(i, F(c[i])); }

  rep(i, Q) {
    auto s = t.get(l[i] - 1, r[i]);
    put(s.d.size());
  }
}
signed main() {
  Main();
  return 0;
}