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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rep1(i, n) for (int i = 1; i <= int(n); i++)
#define per(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define per1(i, n) for (int i = int(n); i > 0; i--)
#define all(c) c.begin(), c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T, class U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T> void mkuni(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#ifdef LOCAL
#define show(x)                                                                \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream &os) { os << endl; }
template <class T, class... Args>
void dmpr(ostream &os, const T &t, const Args &...args) {
  os << t << " ~ ";
  dmpr(os, args...);
}
#define shows(...)                                                             \\
  cerr << "LINE" << __LINE__ << " : ";                                         \\
  dmpr(cerr, ##__VA_ARGS__)
#define dump(x)                                                                \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = {";                         \\
  for (auto v : x)                                                             \\
    cerr << v << ",";                                                          \\
  cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !!
  cout << fixed << setprecision(20);

  int N, Q;
  cin >> N >> Q;
  V<int> c(N);
  rep(i, N) cin >> c[i], c[i]--;
  V<int> L(Q), R(Q);
  rep(i, Q) cin >> L[i] >> R[i], L[i]--;
  V<int> idx(Q);
  iota(all(idx), 0);
  const int B = 700;
  sort(all(idx), [&](int l, int r) {
    if (L[l] / B != L[r] / B)
      return L[l] / B < L[r] / B;
    return R[l] < R[r];
  });
  V<int> ans(Q);
  int now = 0;
  V<int> cnt(N);
  auto add = [&](int i) {
    shows("add", i);
    cnt[i]++;
    if (cnt[i] == 1)
      now++;
  };
  auto erase = [&](int i) {
    shows("era", i);
    cnt[i]--;
    if (cnt[i] == 0)
      now--;
  };
  int x = 0, y = 0;
  for (int i : idx) {
    show(i);
    show(x);
    show(y);
    for (; L[i] < x;)
      add(c[--x]);
    for (; R[i] > y;)
      add(c[y++]);
    for (; L[i] > x;)
      erase(c[x++]);
    for (; R[i] < y;)
      erase(c[--y]);
    ans[i] = now;
  }
  for (int v : ans)
    cout << v << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rep1(i, n) for (int i = 1; i <= int(n); i++)
#define per(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define per1(i, n) for (int i = int(n); i > 0; i--)
#define all(c) c.begin(), c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T, class U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T> void mkuni(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#ifdef LOCAL
#define show(x)                                                                \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream &os) { os << endl; }
template <class T, class... Args>
void dmpr(ostream &os, const T &t, const Args &...args) {
  os << t << " ~ ";
  dmpr(os, args...);
}
#define shows(...)                                                             \\
  cerr << "LINE" << __LINE__ << " : ";                                         \\
  dmpr(cerr, ##__VA_ARGS__)
#define dump(x)                                                                \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = {";                         \\
  for (auto v : x)                                                             \\
    cerr << v << ",";                                                          \\
  cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

struct BIT {
  using D = int;
  int N;
  vector<D> bit;

  BIT(int n) { // nは区間幅!!!
    N = n;
    bit.assign(n + 1, 0);
  }
  BIT() {}

  D sum(int i) { // i個の和a[0]+..a[i-1]
    D s = 0;
    while (i > 0) {
      s += bit[i];
      i -= (i & -i);
    }
    return s;
  }
  void add(int i, D x) { // a[i]+=x (0-indexed)
    i++;
    while (i <= N) {
      bit[i] += x;
      i += (i & -i);
    }
  }
};

struct segtree2D {
  int N; //[0,N) * [0,N)
  vector<BIT> bit;
  vector<vector<int>> vals;

  // build with candidate points
  segtree2D(int n, vector<pair<int, int>> ps) {
    int p2 = 1;
    while (p2 < n)
      p2 *= 2;
    N = p2;
    bit.resize(N * 2);
    vals.resize(N * 2);

    int K = ps.size();

    rep(i, K) {
      int x = ps[i].fs, y = ps[i].sc;
      vals[x + N].pb(y);
    }
    for (int i = N; i < 2 * N; i++) {
      sort(all(vals[i]));
      vals[i].erase(unique(vals[i].begin(), vals[i].end()), vals[i].end());
    }
    for (int i = N - 1; i > 0; i--) {
      vals[i].resize(vals[i * 2].size() + vals[i * 2 + 1].size());
      merge(all(vals[i * 2]), all(vals[i * 2 + 1]), vals[i].begin());
      vals[i].erase(unique(vals[i].begin(), vals[i].end()), vals[i].end());
    }
    for (int i = 1; i < 2 * N; i++) {
      bit[i] = BIT(vals[i].size());
    }
  }

  void add(int x, int y, int v) { // add P(x,y)
    x += N;
    while (x) {
      int yid = lower_bound(all(vals[x]), y) - vals[x].begin();
      bit[x].add(yid, v); // if weighted change this '1'
      x /= 2;
    }
  }
  int count(int a, int b, int ya, int yb, int l, int r,
            int k) { //[a,b) * [ya,yb)
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      int yaid = lower_bound(all(vals[k]), ya) - vals[k].begin();
      int ybid = lower_bound(all(vals[k]), yb) - vals[k].begin();
      return bit[k].sum(ybid) - bit[k].sum(yaid);
    }
    return count(a, b, ya, yb, l, (l + r) / 2, k * 2) +
           count(a, b, ya, yb, (l + r) / 2, r, k * 2 + 1);
  }
  int count(int a, int b) {
    //		printf("count [0,%d)*[0,%d)\\n",a,b);
    return count(0, a, 0, b, 0, N, 1);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !!
  cout << fixed << setprecision(20);

  int N, Q;
  cin >> N >> Q;
  V<int> c(N);
  rep(i, N) cin >> c[i], c[i]--;
  using P = pair<int, int>;
  V<P> ps;
  rep(i, N) ps.pb(P(N - 1 - i, i));
  VV<int> c2is(N);
  rep(i, N) c2is[c[i]].pb(i);
  rep(i, N) {
    auto vs = c2is[i];
    rep(j, si(vs) - 1) {
      int x = vs[j], y = vs[j + 1];
      ps.pb(P(N - 1 - x, y));
    }
  }
  mkuni(ps);
  segtree2D seg(N, ps);
  rep(i, N) seg.add(N - 1 - i, i, 1);
  rep(i, N) {
    auto vs = c2is[i];
    rep(j, si(vs) - 1) {
      int x = vs[j], y = vs[j + 1];
      seg.add(N - 1 - x, y, -1);
    }
  }
  while (Q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    cout << seg.count(N - 1 - l + 1, r + 1) << endl;
  }
}