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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------

template <class T> class BIT {
  vector<T> data[2];
  T ZERO = 0;

public:
  BIT(int n) { init(n); }
  void init(int n) {
    for (int i = 0; i < 2; ++i) {
      data[i].assign(n + 1, ZERO);
    }
  }

  void sub_add(int p, int a, T x) {
    for (int i = a; i < (int)data[p].size(); i += (i & -i)) {
      data[p][i] = data[p][i] + x;
    }
  }

  void add(int a, int b, T x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  T sub_sum(int p, int a) {
    T res = 0;
    for (int i = a; i > 0; i -= (i & -i)) {
      res += data[p][i];
    }
    return res;
  }

  T sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }
};

int N, Q;

int main() {
  cin >> N >> Q;
  vector<int> c(N + 1);
  repe(i, 1, N) { cin >> c[i]; }
  vector<int> left(Q), right(Q), ids(Q);
  rep(i, 0, Q) {
    cin >> left[i] >> right[i];
    ids[i] = i;
  }

  sort(ids.begin(), ids.end(),
       [&](int a, int b) { return right[a] < right[b]; });

  BIT<int> bit(N);
  vector<int> prev(N + 10, 0);
  vector<int> res(Q);
  int r = 1;
  for (auto q : ids) {
    for (; r <= right[q]; ++r) {
      bit.add(prev[c[r]] + 1, r + 1, 1);
      prev[c[r]] = r;
    }
    res[q] = bit.sum(left[q], left[q] + 1);
  }
  for (auto e : res) {
    cout << e << "\\n";
  }
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------

template <class T> class BIT {
  vector<T> data[2];
  T ZERO = 0;

public:
  BIT(int n) { init(n); }
  void init(int n) {
    for (int i = 0; i < 2; ++i) {
      data[i].assign(n + 1, ZERO);
    }
  }

  void sub_add(int p, int a, T x) {
    for (int i = a; i < (int)data[p].size(); i += (i & -i)) {
      data[p][i] = data[p][i] + x;
    }
  }

  void add(int a, int b, T x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  T sub_sum(int p, int a) {
    T res = 0;
    for (int i = a; i > 0; i -= (i & -i)) {
      res += data[p][i];
    }
    return res;
  }

  T sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }
};

int N, Q;

int main() {
  cin >> N >> Q;
  vector<int> c(N + 1);
  repe(i, 1, N) { cin >> c[i]; }
  vector<int> left(Q), right(Q), ids(Q);
  rep(i, 0, Q) {
    cin >> left[i] >> right[i];
    ids[i] = i;
  }

  sort(ids.begin(), ids.end(),
       [&](int a, int b) { return right[a] < right[b]; });

  BIT<int> bit(N);
  vector<int> prev(N + 10, 0);
  vector<int> res(Q);
  for (auto q : ids) {
    int r = 1;
    for (; r <= right[q]; ++r) {
      bit.add(prev[c[r]] + 1, r + 1, 1);
      prev[c[r]] = r;
    }
    res[q] = bit.sum(left[q], left[q] + 1);
  }
  for (auto e : res) {
    cout << e << "\\n";
  }
}