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

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

struct S {
  int l, r, pos;
};

// 1-indexed
template <typename T = int> struct BIT {
  const int n;
  vector<T> v;

  BIT(int n) : n(n), v(n + 1, 0) {}

  void add(int i, int x) {
    ++i;
    while (i <= n) {
      v[i] += x;
      i += i & -i;
    }
  }
  T sum(int i) {
    ++i;
    T ret = 0;
    while (i > 0) {
      ret += v[i];
      i -= i & -i;
    }
    return ret;
  }
  T sum(int l, int r) { return sum(r) - sum(l - 1); }
};

void solve() {
  int n, q, l, r;
  cin >> n >> q;
  VI c(n);
  REP(i, 0, n) cin >> c[i];
  REP(i, 0, n)-- c[i];
  vector<S> qs(q);
  REP(i, 0, q) {
    cin >> l >> r;
    qs[i].l = --l;
    qs[i].r = --r;
    qs[i].pos = i;
  }
  sort(all(qs), [](auto &l, auto &r) {
    if (l.r == r.r)
      return l.l < r.l;
    return l.r < r.r;
  });

  VI gs(n, -1);
  VI ans(q);
  BIT<> bit(n);
  int idx = 0;
  REP(p, 0, q) {
    while (idx <= qs[p].r) {
      if (gs[c[idx]] != -1) {
        bit.add(gs[c[idx]], -1);
      }
      gs[c[idx]] = idx;
      bit.add(idx, 1);
      ++idx;
    }
    ans[qs[p].pos] = bit.sum(qs[p].l, qs[p].r);
  }

  REP(i, 0, q) { cout << ans[i] << endl; }
}

signed main() {
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void solve() {
  int n, q, l, r;
  cin >> n >> q;
  VI c(n);
  REP(i, 0, n) cin >> c[i];
  REP(i, 0, n)-- c[i];
  vector<bitset<500000>> bs((n + 7000 - 1) / 7000 + 1);
  REP(i, 0, n) { bs[i / 7000 + 1][c[i]] = true; }
  REP(iii, 0, q) {
    cin >> l >> r;
    --l;
    --r;
    // auto lb = bs[(l + 7000 - 1) / 7000+1];
    // auto rb = bs[(r - r % 7000) / 7000+1];
    bitset<500000> bits;
    REP(i, max(l, r - r % 7000), r + 1) bits[c[i]] = true;
    REP(i, l, min({n, r + 1, ((l + 7000 - 1) / 7000) * 7000}))
    bits[c[i]] = true;
    REP(i, (l + 7000 - 1) / 7000 + 1, (r - r % 7000) / 7000 + 1) {
      dump(i);
      bits |= bs[i];
    }
    cout << bits.count() << endl;
  }
}

signed main() {
  solve();

  return 0;
}