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

#include <bits/stdc++.h>
template <typename T> class BIT {
  int sz;

public:
  std::vector<T> node;
  BIT(int sz) : sz(sz + 1), node(std::vector<T>(sz + 1, 0)) {}
  void add(int idx, T val) {
    ++idx;
    assert(0 < idx && idx < sz);
    while (idx < sz) {
      node[idx] += val;
      idx += idx & -idx;
    }
  }
  T sum(int idx) {
    ++idx;
    assert(0 < idx && idx < sz);
    T res = 0;
    while (idx > 0) {
      res += node[idx];
      idx -= idx & -idx;
    }
    return res;
  }
};
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < ((int)(v.size())); ++i) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n, q;
  cin >> n >> q;
  vi c(n);
  for (int i = 0; i < (n); ++i) {
    cin >> c[i];
  }
  vector<pair<P, int>> v(q);
  for (int i = 0; i < (q); ++i) {
    cin >> v[i].first.second >> v[i].first.first;
    v[i].second = i;
  }
  sort((v).begin(), (v).end());
  for (int i = 0; i < (q); ++i)
    swap(v[i].first.first, v[i].first.second);
  vi last(n + 1, -1);
  BIT<ll> bit(n + 1);
  int nowr = 0;
  vi ans(q);
  for (int i = 0; i < (q); ++i) {
    int nxtr = v[i].first.second;
    while (nowr < nxtr) {
      if (last[c[nowr]] != -1) {
        bit.add(last[c[nowr]], -1);
      }
      bit.add(nowr, 1);
      last[c[nowr]] = nowr;
      nowr++;
    }
    ans[v[i].second] =
        bit.sum(v[i].first.second - 1) -
        (v[i].first.first == 1 ? 0 : bit.sum(v[i].first.first - 2));
  }
  for (int i = 0; i < (q); ++i) {
    cout << ans[i] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, n) for (int i = (int)(n); i >= 0; --i)
#define RFOR(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define ALL(a) (a).begin(), (a).end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;

const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, SZ(v)) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

vi c;

struct Mo {
  vector<int> left, right, order;
  vector<bool> v;
  int width;
  int nl, nr, ptr;

  vector<int> cnt;
  int val = 0;

  Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), v(n) { cnt.resize(n); }

  void insert(int l, int r) /* [l, r) */
  {
    left.push_back(l);
    right.push_back(r);
  }

  /* ソート */
  void build() {
    order.resize(left.size());
    iota(begin(order), end(order), 0);
    sort(begin(order), end(order), [&](int a, int b) {
      if (left[a] / width != left[b] / width)
        return left[a] < left[b];
      return right[a] < right[b];
    });
  }

  /* クエリを 1 つぶんすすめて, クエリのidを返す */
  int process() {
    if (ptr == order.size())
      return (-1);
    const auto id = order[ptr];
    while (nl > left[id])
      distribute(--nl);
    while (nr < right[id])
      distribute(nr++);
    while (nl < left[id])
      distribute(nl++);
    while (nr > right[id])
      distribute(--nr);
    return (order[ptr++]);
  }

  inline void distribute(int idx) {
    v[idx].flip();
    if (v[idx])
      add(idx);
    else
      del(idx);
  }

  void add(int idx) {
    if (cnt[c[idx]] == 0)
      val++;
    cnt[c[idx]]++;
  }

  void del(int idx) {
    cnt[c[idx]]--;
    if (cnt[c[idx]] == 0)
      val--;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n, q;
  cin >> n >> q;
  c.resize(n);
  REP(i, n) { cin >> c[i]; }

  Mo mo(n);

  REP(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    mo.insert(l, r);
  }
  mo.build();

  vi ans(q);
  REP(i, q) {
    int idx = mo.process();
    ans[idx] = mo.val;
  }
  cout << ans << endl;
}