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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

struct BIT {
  using Val = int;
  int n;
  vector<Val> tree;
  BIT(int n) : n(n), tree(n + 1) {}
  void fill(Val val) { REP(i, n + 1) tree[i] = (i & -i) * val; }
  void add(int idx, Val val) {
    for (int x = idx + 1; x <= n; x += x & -x)
      tree[x] += val;
  }
  // sum[0, idx]
  Val range(int idx) {
    Val sum = 0;
    for (int x = idx + 1; x > 0; x -= x & -x)
      sum += tree[x];
    return sum;
  }
  // sum[a, b]
  Val range(int a, int b) { return range(b) - range(a - 1); }
};

struct Query {
  int L, R;
  int id;
};

int main2() {
  int N = nextLong();
  int Q = nextLong();
  vector<ll> c(N);
  REP(i, N) c[i] = nextLong();

  vector<Query> qs(Q);
  REP(i, Q) {
    int L = nextLong() - 1;
    int R = nextLong();
    qs[i] = (Query){L, R, i};
  }

  sort(ALL(qs), [](const Query &a, const Query &b) { return a.R < b.R; });

  vector<int> ans(Q);
  BIT bit(N + 5);

  vector<int> last(N + 10, -1);
  int pos = 0;
  for (auto q : qs) {
    while (pos < q.R) {
      if (last[c[pos]] != -1)
        bit.add(last[c[pos]], -1);
      last[c[pos]] = pos;
      bit.add(last[c[pos]], +1);
      pos++;
    }
    ans[q.id] = bit.range(q.L, q.R - 1);
  }
  REP(i, Q) cout << ans[i] << '\\n';

  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

using P = pair<int, int>;

struct Query {
  int L, R;
  int id;
};

int main2() {
  int N = nextLong();
  int Q = nextLong();
  vector<ll> c(N);
  REP(i, N) c[i] = nextLong();

  vector<Query> qs(Q);
  REP(i, Q) {
    int L = nextLong() - 1;
    int R = nextLong();
    qs[i] = (Query){L, R, i};
  }

  const int B = sqrt(N);

  sort(ALL(qs), [&](const Query &a, const Query &b) {
    if (a.L / B != b.L / B)
      return a.L / B < b.L / B;
    return a.R < b.R;
  });
  vector<int> ans(Q);

  map<int, int> count;
  auto add = [&](int c, int v) {
    count[c] += v;
    if (count[c] == 0) {
      count.erase(c);
    }
  };

  // pv(ALL(c));

  int a = 0, b = 0;
  for (auto q : qs) {
    // cout << "q=" << q.L << " " << q.R << " id=" << q.id << endl;
    while (q.L < a) {
      add(c[a - 1], 1);
      a--;
    }
    while (b < q.R) {
      add(c[b], 1);
      b++;
    }
    while (a < q.L) {
      add(c[a], -1);
      a++;
    }
    while (q.R < b) {
      add(c[b - 1], -1);
      b--;
    }
    ans[q.id] = count.size();
    // pv(ALL(count));
  }

  REP(i, Q) cout << ans[i] << '\\n';

  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}