Problem p01914 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int v;
  ll w;
};

const int MOD = 1000000007;
const int _MOD = 1000000009;
double EPS = 1e-10;
const ll INF = LLONG_MAX / 2;

struct segment_tree_max {
  int n;
  vector<ll> v;
  segment_tree_max(int _n) {
    for (n = 1; n < _n; n *= 2)
      ;
    v = vector<ll>(n * 2 - 1, LLONG_MIN);
  }
  void set(int i, ll x) {
    int k = i + n - 1;
    v[k] = max(v[k], x);
    while (k > 0) {
      k = (k - 1) / 2;
      v[k] = max(v[k * 2 + 1], v[k * 2 + 2]);
    }
  }
  ll _get(int i, int j, int k, int l, int r) {
    if (r <= i || j <= l)
      return LLONG_MIN;
    if (i <= l && r <= j)
      return v[k];
    ll vl = _get(i, j, k * 2 + 1, l, (l + r) / 2);
    ll vr = _get(i, j, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
  ll get(int i, int j) { return _get(i, j, 0, 0, n); }
};

int main() {
  int N;
  cin >> N;
  vector<int> a(N), b(N);
  rep(i, N) scanf("%d%d", &a[i], &b[i]);
  vector<int> c(N * 2);
  rep(i, N) c[i] = a[i];
  rep(i, N) c[N + i] = b[i];
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
  int M = c.size();
  rep(i, N) a[i] = lower_bound(c.begin(), c.end(), a[i]) - c.begin();
  rep(i, N) b[i] = lower_bound(c.begin(), c.end(), b[i]) - c.begin();
  vector<vector<int>> v(M);
  rep(i, N) {
    v[a[i]].pb(i);
    v[b[i]].pb(~i);
  }
  segment_tree_max st1(M), st2(M);
  st1.set(0, 0);
  st2.set(0, 0);
  rep(x, M) {
    vector<i_i> unko1, unko2;
    for (int i : v[x])
      if (i >= 0) {
        int z = st2.get(0, b[i]) + 1;
        unko1.pb(i_i(b[i], z));
      } else {
        i = ~i;
        int z = st1.get(0, a[i]) + 1;
        unko2.pb(i_i(a[i], z));
      }
    for (i_i p : unko1)
      st1.set(p.first, p.second);
    for (i_i p : unko2)
      st2.set(p.first, p.second);
  }
  int ans = max(st1.get(0, M), st2.get(0, M));
  cout << ans << endl;
}

Error: 255

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<int, ll> i_ll;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int v;
  ll w;
};

const int MOD = 1000000007;
const int _MOD = 1000000009;
double EPS = 1e-10;
const ll INF = LLONG_MAX / 2;

struct segment_tree_max {
  int n;
  vector<ll> v;
  segment_tree_max(int _n) {
    for (n = 1; n < _n; n *= 2)
      ;
    v = vector<ll>(n * 2 - 1, LLONG_MIN);
  }
  void set(int i, ll x) {
    int k = i + n - 1;
    v[k] = max(v[k], x);
    while (k > 0) {
      k = (k - 1) / 2;
      v[k] = max(v[k * 2 + 1], v[k * 2 + 2]);
    }
  }
  ll _get(int i, int j, int k, int l, int r) {
    if (r <= i || j <= l)
      return LLONG_MIN;
    if (i <= l && r <= j)
      return v[k];
    ll vl = _get(i, j, k * 2 + 1, l, (l + r) / 2);
    ll vr = _get(i, j, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
  ll get(int i, int j) { return _get(i, j, 0, 0, n); }
};

int main() {
  int N;
  cin >> N;
  vector<int> a(N), b(N);
  rep(i, N) scanf("%d%d", &a[i], &b[i]);
  vector<int> c(N * 2);
  rep(i, N) c[i] = a[i];
  rep(i, N) c[N + i] = b[i];
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
  int M = c.size();
  rep(i, N) a[i] = lower_bound(c.begin(), c.end(), a[i]) - c.begin();
  rep(i, N) b[i] = lower_bound(c.begin(), c.end(), b[i]) - c.begin();
  vector<vector<int>> v(M);
  rep(i, N) {
    v[a[i]].pb(i);
    v[b[i]].pb(~i);
  }
  segment_tree_max st1(M), st2(M);
  st1.set(0, 0);
  st2.set(0, 0);
  return -1;
  rep(x, M) for (int i : v[x]) if (i >= 0) {
    int z = st1.get(0, b[i]) + 1;
    st2.set(b[i], z + 1);
  }
  else {
    i = ~i;
    int z = st2.get(0, a[i]) + 1;
    st1.set(a[i], z + 1);
  }
  int ans = max(st1.get(0, M), st2.get(0, M));
  cout << ans / 2 << endl;
}