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

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

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define whole(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bit(n) (1LL << (n))
#define F first
#define S second
// functions
template <typename T> void unique(T &c) {
  c.erase(std::unique(c.begin(), c.end()), c.end());
}
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 != vec.size())
      os << " ";
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << ")";
    auto titr = itr;
    if (++titr != mp.end())
      os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << ")";
    auto titr = itr;
    if (++titr != mp.end())
      os << " ";
  }
  return os;
}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 60;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io {
  fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
  }
} fast_io_;

int l[500010], r[500010];
const int B = 500;
vector<int> idx[2010];
int ans[500010];

int main(int argc, char const *argv[]) {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  cin >> c;
  rep(i, q) cin >> l[i] >> r[i], l[i]--;
  rep(i, q) { idx[l[i] / B].pb(i); }
  vector<int> cnt(n + 1, 0);
  int m = 0;
  int li = 0, ri = 0;
  auto push = [&](int i) {
    cnt[c[i]]++;
    if (cnt[c[i]] == 1)
      m++;
  };
  auto pull = [&](int i) {
    cnt[c[i]]--;
    if (cnt[c[i]] == 0)
      m--;
  };
  rep(i, 1002) {
    sort(whole(idx[i]), [&](int j, int k) {
      if (r[j] != r[k])
        return r[j] < r[k];
      if (l[j] != l[k])
        return l[j] < l[k];
      return j < k;
    });
    for (auto e : idx[i]) {
      while (r[e] < ri) {
        ri--;
        pull(ri);
      }
      while (ri < r[e]) {
        push(ri);
        ri++;
      }
      while (li < l[e]) {
        pull(li);
        li++;
      }
      while (li - 1 >= l[e]) {
        li--;
        push(li);
      }
      ans[e] = m;
    }
  }
  rep(i, q) cout << ans[i] << "\\n";
  cout << flush;
  return 0;
}


Error: TLE

Fixed submission:

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

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define whole(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define bit(n) (1LL << (n))
#define F first
#define S second
// functions
template <typename T> void unique(T &c) {
  c.erase(std::unique(c.begin(), c.end()), c.end());
}
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 != vec.size())
      os << " ";
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end())
      os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << ")";
    auto titr = itr;
    if (++titr != mp.end())
      os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << ")";
    auto titr = itr;
    if (++titr != mp.end())
      os << " ";
  }
  return os;
}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 60;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io {
  fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
  }
} fast_io_;

template <class Monoid> class SegmentTree {
public:
  int n;
  using T = typename Monoid::value_type;
  vector<T> val;
  T identity;

  SegmentTree() {}
  SegmentTree(int n_, vector<T> v = vector<T>()) {
    n = 1;
    while (n < n_)
      n *= 2;
    identity = Monoid::identity;
    val.assign(2 * n - 1, identity);

    for (size_t i = 0; i < v.size(); i++) {
      val[i + n - 1] = v[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      val[i] = Monoid::operation(val[2 * i + 1], val[2 * i + 2]);
    }
  }
  void update(int k, const T &t) {
    k = k + n - 1;
    val[k] = t;
    while (k > 0) {
      k = (k - 1) / 2;
      val[k] = Monoid::operation(val[2 * k + 1], val[2 * k + 2]);
    }
  }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return identity;
    if (a <= l && r <= b)
      return val[k];
    T lv = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T rv = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return Monoid::operation(lv, rv);
  }
  T query(int a, int b) { return query(a, b, 0, 0, n); }
  T get(int i) { return val[i + n - 1]; }
};

template <typename T> class RSQ {
public:
  const static T identity;
  using value_type = T;

  RSQ() {}
  static T operation(T &l, T &r) { return l + r; }
};

template <typename T> const T RSQ<T>::identity = 0;

template <typename U, typename Monoid> class Compressed2DSegmentTree {
public:
  using p_t = pair<U, U>;
  using T = typename Monoid::value_type;
  T identity = Monoid::identity;
  int n;
  vector<U> xc;
  vector<vector<U>> idx;
  map<U, int> mpx;
  vector<SegmentTree<Monoid>> seg;
  Compressed2DSegmentTree() {}
  Compressed2DSegmentTree(vector<p_t> p) {
    xc.resize(p.size());
    for (size_t i = 0; i < p.size(); i++)
      xc[i] = p[i].first;
    sort(xc.begin(), xc.end());
    xc.erase(unique(xc.begin(), xc.end()), xc.end());
    for (size_t i = 0; i < xc.size(); i++)
      mpx[xc[i]] = i;

    n = 1;
    while (n < int(mpx.size()))
      n *= 2;
    idx.resize(2 * n - 1);
    seg.resize(2 * n - 1);

    for (size_t i = 0; i < p.size(); i++)
      idx[mpx[p[i].first] + n - 1].push_back(p[i].second);
    for (int i = n - 1; i < 2 * n - 1; i++) {
      sort(idx[i].begin(), idx[i].end());
      idx[i].erase(unique(idx[i].begin(), idx[i].end()), idx[i].end());
    }
    for (int i = n - 2; i >= 0; i--) {
      merge(idx[2 * i + 1].begin(), idx[2 * i + 1].end(),
            idx[2 * i + 2].begin(), idx[2 * i + 2].end(),
            back_inserter(idx[i]));
      idx[i].erase(unique(idx[i].begin(), idx[i].end()), idx[i].end());
    }
    for (int i = 0; i < 2 * n - 1; i++) {
      seg[i] = SegmentTree<Monoid>(idx[i].size());
    }
  }
  void update(U x, U y, T t) {
    int xi = mpx[x] + n - 1;
    int yi = lower_bound(idx[xi].begin(), idx[xi].end(), y) - idx[xi].begin();
    seg[xi].update(yi, t);
    while (xi > 0) {
      xi = (xi - 1) / 2;
      T lv = identity, rv = identity;
      int yli = lower_bound(idx[2 * xi + 1].begin(), idx[2 * xi + 1].end(), y) -
                idx[2 * xi + 1].begin();
      if (yli < idx[2 * xi + 1].size() && idx[2 * xi + 1][yli] == y)
        lv = seg[2 * xi + 1].get(yli);
      int yri = lower_bound(idx[2 * xi + 2].begin(), idx[2 * xi + 2].end(), y) -
                idx[2 * xi + 2].begin();
      if (yri < idx[2 * xi + 2].size() && idx[2 * xi + 2][yri] == y)
        rv = seg[2 * xi + 2].get(yri);

      int yi = lower_bound(idx[xi].begin(), idx[xi].end(), y) - idx[xi].begin();
      seg[xi].update(yi, Monoid::operation(lv, rv));
    }
  }
  T query(int sx, int tx, U sy, U ty, int k, int l, int r) {
    if (r <= sx || tx <= l)
      return identity;
    if (sx <= l && r <= tx) {
      int ssy = lower_bound(idx[k].begin(), idx[k].end(), sy) - idx[k].begin();
      int sty = lower_bound(idx[k].begin(), idx[k].end(), ty) - idx[k].begin();
      return seg[k].query(ssy, sty);
    }
    T lv = query(sx, tx, sy, ty, 2 * k + 1, l, (l + r) / 2);
    T rv = query(sx, tx, sy, ty, 2 * k + 2, (l + r) / 2, r);
    return Monoid::operation(lv, rv);
  }
  T query(U sx, U tx, U sy, U ty) {
    int msx = lower_bound(xc.begin(), xc.end(), sx) - xc.begin();
    int mtx = lower_bound(xc.begin(), xc.end(), tx) - xc.begin();
    return query(msx, mtx, sy, ty, 0, 0, n);
  }
};

int main(int argc, char const *argv[]) {
  int n, q;
  cin >> n >> q;
  vector<P> v;
  v.reserve(n);
  vector<int> c(n);
  cin >> c;
  rep(i, n) c[i]--;
  vector<int> prev(n, -1);
  rep(i, n) {
    if (prev[c[i]] >= 0)
      v.pb(prev[c[i]], i);
    prev[c[i]] = i;
  }
  Compressed2DSegmentTree<int, RSQ<int>> cst(v);
  prev.assign(n, -1);
  rep(i, n) {
    if (prev[c[i]] >= 0)
      cst.update(prev[c[i]], i, 1);
    prev[c[i]] = i;
  }
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << r - l + 1 - cst.query(l, r + 1, l, r + 1) << "\\n";
  }
  cout << flush;
  return 0;
}