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

#include <bits/stdc++.h>
typedef long long ll;
typedef std::pair<ll, ll> P;

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;

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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

struct BIT {
  typedef ll input_t;

  vector<input_t> bit_;
  const long long n_;

  BIT(long long n) : n_(n) { bit_.resize(n + 1LL, 0); }

  input_t sum(int i) {
    input_t s = 0;
    while (i > 0) {
      s += bit_[i];
      i -= i & (-i);
    }
    return s;
  }

  void add(int i, input_t x) {
    while (i <= n_) {
      bit_[i] += x;
      i += (i & (-i));
    }
  }
};

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> cs(n + 1);
  rep(i, n) cin >> cs[i + 1];
  vector<ll> pos(n + 1, -1);
  vector<P> range(q);
  rep(i, q) { cin >> range[i].first >> range[i].second; }
  vector<vector<int>> qs(n + 1);
  rep(i, q) { qs[range[i].second].push_back(i); }
  BIT bit(n + 10);
  vector<ll> ans(q);
  ll rightSum = 0;
  for (int i = 1; i <= n; i++) {
    auto c = cs[i];
    if (pos[c] >= 0) {
      bit.add(pos[c], -1);
      rightSum--;
    }
    pos[c] = i;
    bit.add(i, 1);
    rightSum++;
    ll sum = rightSum;
    for (auto query : qs[i]) {
      auto index = query;
      auto left = range[index].first;
      if (left > 1) {
        ans[index] = sum - bit.sum(left - 1);
      } else {
        ans[index] = sum;
      }
    }
  }
  rep(i, q) { cout << ans[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef std::pair<ll, ll> P;

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;

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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

struct BIT {
  typedef ll input_t;

  vector<input_t> bit_;
  const long long n_;

  BIT(long long n) : n_(n) { bit_.resize(n + 1LL, 0); }

  input_t sum(int i) {
    input_t s = 0;
    while (i > 0) {
      s += bit_[i];
      i -= i & (-i);
    }
    return s;
  }

  void add(int i, input_t x) {
    while (i <= n_) {
      bit_[i] += x;
      i += (i & (-i));
    }
  }
};

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> cs(n + 1);
  rep(i, n) cin >> cs[i + 1];
  vector<ll> pos(n, -1);
  vector<P> range(q);
  rep(i, q) { cin >> range[i].first >> range[i].second; }
  vector<vector<int>> qs(n + 1);
  rep(i, q) { qs[range[i].second].push_back(i); }
  BIT bit(n + 10);
  vector<ll> ans(q);
  ll rightSum = 0;
  for (int i = 1; i <= n; i++) {
    auto c = cs[i];
    if (pos[c] >= 0) {
      bit.add(pos[c], -1);
      rightSum--;
    }
    pos[c] = i;
    bit.add(i, 1);
    rightSum++;
    ll sum = rightSum;
    for (auto query : qs[i]) {
      auto index = query;
      auto left = range[index].first;
      if (left > 1) {
        ans[index] = sum - bit.sum(left - 1);
      } else {
        ans[index] = sum;
      }
    }
  }
  rep(i, q) { cout << ans[i] << endl; }
  return 0;
}