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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

int n;
pii p[125252];

const int N = 1 << 17;
map<int, int> seg[2 * N];

int query(int x, int y, int a, int b, int k) {
  if (b <= x)
    return 0;
  else if (x <= a) {
    return seg[k].upper_bound(y)->second;
  } else {
    int m = (a + b) / 2;
    return max(query(x, y, a, m, 2 * k + 1), query(x, y, m, b, 2 * k + 2));
  }
}
void update(int x, int y, int v) {
  x += N - 1;
  x = 2 * x + 1;
  while (x != 0) {
    x = (x - 1) / 2;
    if (seg[x].lower_bound(y)->second >= v)
      continue;
    seg[x][y] = v;
    auto it = seg[x].lower_bound(y);
    while (it != seg[x].begin()) {
      it--;
      if (it->second <= v) {
        seg[x].erase(it++);
      } else {
        break;
      }
    }
  }
}

int main() {
  scanf("%d", &n);
  set<int> Z;
  REP(i, n) {
    int a, b;
    scanf("%d%d", &a, &b);
    p[i] = pii(a + b, a);
    Z.insert(b);
  }
  // zaatu
  Z.insert(2e9);
  Z.insert(-2e9);
  map<int, int> M;
  {
    int it = 0;
    for (int x : Z) {
      M[x] = it++;
    }
  }
  // seg init
  REP(i, 2 * N - 1) seg[i][2e9] = 0;
  // calc
  int ans = 0;
  sort(p, p + n);
  reverse(p, p + n);
  REP(i, n) {
    int x = p[i].second;
    int y = p[i].first - x;
    int xx = M.upper_bound(x)->second;
    int yy = M[y];
    int v = query(xx, y, 0, N, 0);
    CHMAX(ans, v + 1);
    update(yy, x, v + 1);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \\
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x) < EPS)
#define EQ(a, b) (abs(a - b) < EPS)
typedef complex<Real> P;

int n;
pii p[125252];
pii yo[125252];
vi g[125252];
int inn[125252];

int dp[125252];

int main() {
  scanf("%d", &n);
  REP(i, n) {
    int a, b;
    scanf("%d%d", &a, &b);
    p[i] = pii(a, b);
    yo[i] = pii(a + b, i);
  }
  sort(yo, yo + n);
  // uso kaihou
  REP(i, n) {
    int x = upper_bound(yo, yo + n, pii(p[i].first + p[i].second, -1)) - yo;
    FOR(j, x, min(x + 500, n)) {
      int to = yo[j].second;
      if (p[to].first > p[i].second && p[to].second > p[i].first) {
        g[i].push_back(to);
        inn[to]++;
      }
    }
  }

  queue<int> Q;
  REP(i, n) if (inn[i] == 0) Q.push(i);
  int ans = 0;
  while (!Q.empty()) {
    int po = Q.front();
    Q.pop();
    for (int to : g[po]) {
      CHMAX(dp[to], dp[po] + 1);
      CHMAX(ans, dp[po] + 1);
      inn[to]--;
      if (inn[to] == 0)
        Q.push(to);
    }
  }
  printf("%d\\n", ans + 1);
  return 0;
}