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

// https://atcoder.jp/contests/abc174/tasks/abc174_f

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define ITR(c, it) for (auto it = c.begin(); it != c.end(); it++)

#define DEBUG 1
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a)                                                                \\
  REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\\n" : " ")
#define DUMP2D(b)                                                              \\
  REP(_j, b.size()) DUMP(b[_j]);                                               \\
  cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif

//------------------------------------------------------------------------------
template <typename T> class BinaryIndexedTree {
public:
  BinaryIndexedTree() : _n(0) {}
  BinaryIndexedTree(int n) : _n(n), data(n) {}

  void add(int p, T x) {
    assert(0 <= p && p < _n);
    p++;
    while (p <= _n) {
      data[p - 1] += x;
      p += p & -p;
    }
  }

  T sum(int l, int r) {
    assert(0 <= l && l <= r && r <= _n);
    return sum(r) - sum(l);
  }

private:
  int _n;
  vector<T> data;

  T sum(int r) {
    T s = 0;
    while (r > 0) {
      s += data[r - 1];
      r -= r & -r;
    }
    return s;
  }
};

//------------------------------------------------------------------------------
const int N_MAX = 5 * 1e5;
const int Q_MAX = 5 * 1e5;

int N, Q;
vector<int> c;
vector<int> l;
vector<int> r;

map<int, vector<int>> qs;
map<int, int> mp;
BinaryIndexedTree<int> bit;
vector<int> ans;

int main() {
  cin >> N >> Q;
  c = vector<int>(N);
  REP(i, N) cin >> c[i];
  l = vector<int>(Q);
  r = vector<int>(Q);
  REP(i, Q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  ans = vector<int>(Q, 0);
  REP(i, Q) qs[r[i]].push_back(i);
  bit = BinaryIndexedTree<int>(N);

  REP(i, N) {
    if (mp.count(c[i]))
      bit.add(mp[c[i]], -1);
    mp[c[i]] = i;
    bit.add(mp[c[i]], 1);
    ;
    for (int j : qs[i])
      ans[j] = bit.sum(l[j], i + 1);
  }

  REP(i, Q) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc174/tasks/abc174_f

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define ITR(c, it) for (auto it = c.begin(); it != c.end(); it++)

#define DEBUG 1
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a)                                                                \\
  REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\\n" : " ")
#define DUMP2D(b)                                                              \\
  REP(_j, b.size()) DUMP(b[_j]);                                               \\
  cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif

const int N_MAX = 5 * 1e5;
const int Q_MAX = 5 * 1e5;

int N, Q;
vector<int> c;
vector<int> l;
vector<int> r;

map<int, vector<int>> qs;
map<int, int> mp;
bitset<N_MAX> bs;
vector<int> ans;

int main() {
  cin >> N >> Q;
  c = vector<int>(N);
  REP(i, N) cin >> c[i];
  l = vector<int>(Q);
  r = vector<int>(Q);
  REP(i, Q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }

  ans = vector<int>(Q, 0);
  REP(i, Q) qs[r[i]].push_back(i);

  REP(i, N) {
    if (mp.count(c[i]))
      bs.reset(mp[c[i]]);
    mp[c[i]] = i;
    bs.set(i);
    // cout << i << ":" << endl;
    // ITR(mp, it) cout << " " << it->first << ":" << it->second;
    // cout << endl;
    // cout << " " << bs << endl;
    for (int j : qs[i]) {
      // cout << j << ":" << l[j] << "-" << i << endl;
      ans[j] = (bs >> l[j]).count();
    }
  }

  REP(i, Q) cout << ans[i] << endl;
}