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

#include <bits/stdc++.h>
#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 all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
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 << "sz = " << vc.size() << endl << "[";
  for (const T &v : vc)
    o << v << ",";
  o << "]";
  return o;
}

int inf = 1e9;
struct segtree {
  static const int N = 1 << 18;
  int seg[N * 2];
  segtree() { rep(i, N * 2) seg[i] = 0; }
  segtree(vector<int> &vi) {
    rep(i, N * 2) seg[i] = 0;
    rep(i, vi.size()) seg[N + i] = vi[i];
    for (int i = N - 1; i > 0; i--)
      seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
  }

  void update(int x, int v) {
    x += N;
    chmax(seg[x], v);
    x /= 2;
    while (x) {
      seg[x] = max(seg[x * 2], seg[x * 2 + 1]);
      x /= 2;
    }
  }
  int getmax(int a, int b, int l = 0, int r = N, int k = 1) {
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return seg[k];
    return max(getmax(a, b, l, (l + r) / 2, k * 2),
               getmax(a, b, (l + r) / 2, r, k * 2 + 1));
  }
} seg[2];

vector<int> xs;
const int MN = 100000;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int a[MN], b[MN], c[MN];
int main() {
  int N;
  cin >> N;
  vector<PP> vp;
  rep(i, N) cin >> a[i] >> b[i];
  rep(i, N) xs.pb(a[i]), xs.pb(b[i]);
  sort(all(xs));
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  rep(i, N) a[i] = lower_bound(all(xs), a[i]) - xs.begin(),
            b[i] = lower_bound(all(xs), b[i]) - xs.begin();
  rep(i, N) {
    vp.pb(PP(P(a[i], -b[i]), 0));
    vp.pb(PP(P(b[i], -a[i]), 1));
  }
  sort(all(vp));

  int K = xs.size();
  int ans = 0;

  vector<P> upd[2];
  rep(i, N * 2) {
    PP pp = vp[i];
    int x = pp.fs.fs, y = -pp.fs.sc, c = pp.sc;
    int mx = seg[c].getmax(0, y) + 1;
    chmax(ans, mx);
    //		seg[1-c].update(y,mx);
    upd[1 - c].pb(P(y, mx));
    if (i == N * 2 - 1 || x != vp[i + 1].fs.fs) {
      rep(t, 2) {
        for (P p : upd[t])
          seg[t].update(p.fs, p.sc);
        upd[t].clear();
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#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 all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
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 << "sz = " << vc.size() << endl << "[";
  for (const T &v : vc)
    o << v << ",";
  o << "]";
  return o;
}

int inf = 1e9;
struct segtree {
  static const int N = 1 << 18;
  int seg[N * 2];
  segtree() { rep(i, N * 2) seg[i] = 0; }
  segtree(vector<int> &vi) {
    rep(i, N * 2) seg[i] = 0;
    rep(i, vi.size()) seg[N + i] = vi[i];
    for (int i = N - 1; i > 0; i--)
      seg[i] = max(seg[i * 2], seg[i * 2 + 1]);
  }

  void update(int x, int v) {
    x += N;
    chmax(seg[x], v);
    x /= 2;
    while (x) {
      seg[x] = max(seg[x * 2], seg[x * 2 + 1]);
      x /= 2;
    }
  }
  int getmax(int a, int b, int l = 0, int r = N, int k = 1) {
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return seg[k];
    return max(getmax(a, b, l, (l + r) / 2, k * 2),
               getmax(a, b, (l + r) / 2, r, k * 2 + 1));
  }
} seg[2];

vector<int> xs;
const int MN = 100000;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int a[MN], b[MN], c[MN];
int main() {
  int N;
  cin >> N;
  vector<PP> vp;
  rep(i, N) cin >> a[i] >> b[i];
  rep(i, N) xs.pb(a[i]), xs.pb(b[i]);
  sort(all(xs));
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  rep(i, N) a[i] = lower_bound(all(xs), a[i]) - xs.begin(),
            b[i] = lower_bound(all(xs), b[i]) - xs.begin();
  rep(i, N) {
    vp.pb(PP(P(a[i], -b[i]), 0));
    vp.pb(PP(P(b[i], -a[i]), 1));
  }
  sort(all(vp));

  int K = xs.size();
  int ans = 0;

  vector<P> upd[2];
  rep(i, N * 2) {
    PP pp = vp[i];
    int x = pp.fs.fs, y = -pp.fs.sc, c = pp.sc;
    int mx = seg[c].getmax(0, y) + 1;
    chmax(ans, mx);
    //		seg[1-c].update(y,mx);
    upd[1 - c].pb(P(y, mx));
    if (i == N * 2 - 1 || x != vp[i + 1].fs.fs) {
      rep(t, 2) {
        for (P p : upd[t])
          seg[t].update(p.fs, p.sc);
      }
    }
  }
  cout << ans << endl;
}