Problem p03235 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int l = 1 << 20;
char buf[l], *s, *t, c;
inline void gc() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, l, stdin);
    c = s == t ? EOF : *s++;
  } else
    c = *s++;
}
template <class IT> inline void gi(IT &x) {
  x = 0;
  gc();
  while (c < '0' || c > '9')
    gc();
  while ('0' <= c && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    gc();
  }
}
char buf0[20];
int a;
template <class IT> inline void pi(IT x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  do
    buf0[++a] = x % 10 + 48;
  while (x /= 10);
  while (a)
    putchar(buf0[a--]);
  putchar('\n');
}
}; // namespace io
using io::gi;
using io::pi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define pque priority_queue
#define rep(i, l, r) for (i = (l); i <= (r); ++i)
#define per(i, l, r) for (i = (l); i >= (r); --i)
#define REP(i, l, r) for (i = (l); i < (r); ++i)
#define PER(i, l, r) for (i = (l); i > (r); --i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define fi first
#define se second
template <class IT> inline void cmax(IT &a, IT b) {
  if (a < b)
    a = b;
}
template <class IT> inline void cmin(IT &a, IT b) {
  if (b < a)
    a = b;
}
const int N = 200005;
struct node {
  pii v;
  int lc, rc;
} tre[N * 18];
int tot;
inline pii aww(pii a, int b) {
  return (b & 1) ? mp(max(a.fi, a.se + b), max(a.se, a.fi + b))
                 : mp(a.fi + b, a.se + b);
}
inline bool get(pii a, int b) {
  return b >= 0 && ((b & 1) ? (b <= a.se) : (b <= a.fi));
}
inline pii pmax(pii a, const pii &b) {
  cmax(a.fi, b.fi);
  cmax(a.se, b.se);
  return a;
}
#define mid ((l + r) >> 1)
#define ls tre[o].lc, l, mid
#define rs tre[o].rc, mid + 1, r
void add(int O, int &o, int l, int r, int x, pii y) {
  tre[o = ++tot].v = pmax(tre[O].v, y);
  if (l == r)
    return;
  if (x <= mid) {
    tre[o].rc = tre[O].rc;
    add(tre[O].lc, ls, x, y);
  } else {
    tre[o].lc = tre[O].lc;
    add(tre[O].rc, rs, x, y);
  }
}
pii que(int o, int l, int r, int x) {
  if (l == r)
    return tre[o].v;
  if (x <= mid)
    return pmax(que(ls, x), tre[tre[o].rc].v);
  return que(rs, x);
}
int rt[N], a[N], h[N], s[N];
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  tre[0].v.se = -(N << 1 | 1);
  int n, i, cx = 0, mx = 0, cy = 0, my = 0, C, M, b;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &a[i]);
    if (a[i] > mx) {
      mx = a[i];
      h[i] = 1;
    }
  }
  mx = 0;
  per(i, n, 1) add(rt[i + 1], rt[i], 1, n, a[i],
                   aww(que(rt[i + 1], 1, n, a[i]), 1 + h[i]));
  per(i, n, 1) s[i] = h[i] + s[i + 1];
  if (!get(tre[rt[1]].v, s[1])) {
    puts("-1");
    return 0;
  }
  rep(i, 1, n) {
    C = cx + (a[i] > mx);
    M = max(mx, a[i]);
    // printf("Y (%d,%d)
    // %d\n",que(rt[i+1],1,n,my).fi,que(rt[i+1],1,n,my).se,C-cy+s[i+1]);
    // printf("X (%d,%d) %d\n",que(rt[i+1],1,n,M ).fi,que(rt[i+1],1,n,M
    // ).se,cy-C+s[i+1]);
    if (get(que(rt[i + 1], 1, n, my), C - cy + s[i + 1]) ||
        get(que(rt[i + 1], 1, n, M), cy - C + s[i + 1])) {
      cx = C;
      mx = M;
      putchar('0');
    } else {
      cy += (a[i] > my);
      cmax(my, a[i]);
      putchar('1');
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int l = 1 << 20;
char buf[l], *s, *t, c;
inline void gc() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, l, stdin);
    c = s == t ? EOF : *s++;
  } else
    c = *s++;
}
template <class IT> inline void gi(IT &x) {
  x = 0;
  gc();
  while (c < '0' || c > '9')
    gc();
  while ('0' <= c && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    gc();
  }
}
char buf0[20];
int a;
template <class IT> inline void pi(IT x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  do
    buf0[++a] = x % 10 + 48;
  while (x /= 10);
  while (a)
    putchar(buf0[a--]);
  putchar('\n');
}
}; // namespace io
using io::gi;
using io::pi;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define pque priority_queue
#define rep(i, l, r) for (i = (l); i <= (r); ++i)
#define per(i, l, r) for (i = (l); i >= (r); --i)
#define REP(i, l, r) for (i = (l); i < (r); ++i)
#define PER(i, l, r) for (i = (l); i > (r); --i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define fi first
#define se second
template <class IT> inline void cmax(IT &a, IT b) {
  if (a < b)
    a = b;
}
template <class IT> inline void cmin(IT &a, IT b) {
  if (b < a)
    a = b;
}
const int N = 200005;
struct node {
  pii v;
  int lc, rc;
} tre[N * 19];
int tot;
inline pii aww(pii a, int b) {
  return (b & 1) ? mp(max(a.fi, a.se + b), max(a.se, a.fi + b))
                 : mp(a.fi + b, a.se + b);
}
inline bool get(pii a, int b) {
  return b >= 0 && ((b & 1) ? (b <= a.se) : (b <= a.fi));
}
inline pii pmax(pii a, const pii &b) {
  cmax(a.fi, b.fi);
  cmax(a.se, b.se);
  return a;
}
#define mid ((l + r) >> 1)
#define ls tre[o].lc, l, mid
#define rs tre[o].rc, mid + 1, r
void add(int O, int &o, int l, int r, int x, pii y) {
  tre[o = ++tot].v = pmax(tre[O].v, y);
  if (l == r)
    return;
  if (x <= mid) {
    tre[o].rc = tre[O].rc;
    add(tre[O].lc, ls, x, y);
  } else {
    tre[o].lc = tre[O].lc;
    add(tre[O].rc, rs, x, y);
  }
}
pii que(int o, int l, int r, int x) {
  if (l == r)
    return tre[o].v;
  if (x <= mid)
    return pmax(que(ls, x), tre[tre[o].rc].v);
  return que(rs, x);
}
int rt[N], a[N], h[N], s[N];
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  tre[0].v.se = -(N << 1 | 1);
  int n, i, cx = 0, mx = 0, cy = 0, my = 0, C, M, b;
  scanf("%d", &n);
  rep(i, 1, n) {
    scanf("%d", &a[i]);
    if (a[i] > mx) {
      mx = a[i];
      h[i] = 1;
    }
  }
  mx = 0;
  per(i, n, 1) add(rt[i + 1], rt[i], 1, n, a[i],
                   aww(que(rt[i + 1], 1, n, a[i]), 1 + h[i]));
  per(i, n, 1) s[i] = h[i] + s[i + 1];
  if (!get(tre[rt[1]].v, s[1])) {
    puts("-1");
    return 0;
  }
  rep(i, 1, n) {
    C = cx + (a[i] > mx);
    M = max(mx, a[i]);
    // printf("Y (%d,%d)
    // %d\n",que(rt[i+1],1,n,my).fi,que(rt[i+1],1,n,my).se,C-cy+s[i+1]);
    // printf("X (%d,%d) %d\n",que(rt[i+1],1,n,M ).fi,que(rt[i+1],1,n,M
    // ).se,cy-C+s[i+1]);
    if (get(que(rt[i + 1], 1, n, my), C - cy + s[i + 1]) ||
        get(que(rt[i + 1], 1, n, M), cy - C + s[i + 1])) {
      cx = C;
      mx = M;
      putchar('0');
    } else {
      cy += (a[i] > my);
      cmax(my, a[i]);
      putchar('1');
    }
  }
  return 0;
}