Problem p03235 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define fir first
#define sec second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int MAXN = 1e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
// inline char gc(){return getchar();}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int n, a[MAXN], val[MAXN], cnt[MAXN], Mx;
char s[MAXN];
struct SegTree {
#define ls x << 1
#define rs x << 1 | 1
  int Mx[MAXN << 2];
  inline void pushup(int x) { Mx[x] = max(Mx[ls], Mx[rs]); }
  void Change(int x, int l, int r, int p, int v) {
    if (l == r) {
      Mx[x] = v;
      return;
    }
    int mid = l + r >> 1;
    if (p <= mid)
      Change(ls, l, mid, p, v);
    else
      Change(rs, mid + 1, r, p, v);
    pushup(x);
  }
  int Ask(int x, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr)
      return Mx[x];
    int mid = l + r >> 1;
    if (qr <= mid)
      return Ask(ls, l, mid, ql, qr);
    else if (ql > mid)
      return Ask(rs, mid + 1, r, ql, qr);
    else
      return max(Ask(ls, l, mid, ql, mid), Ask(rs, mid + 1, r, mid + 1, qr));
  }
  void Build(int x, int l, int r) {
    Mx[x] = 1 - 1e9;
    if (l == r)
      return;
    int mid = l + r >> 1;
    Build(ls, l, mid), Build(rs, mid + 1, r);
  }
} Odd, Even;
inline bool chk(int mx, int Q) {
  if (Q < 0)
    return 0;
  return Q & 1 ? Odd.Ask(1, 1, n, mx, n) >= Q : Even.Ask(1, 1, n, mx, n) >= Q;
}
int main() {
  n = read();
  Rep(i, 1, n) a[i] = read(), a[i] > Mx ? Mx = a[i], val[i] = 2 : val[i] = 1;
  Odd.Build(1, 1, n);
  Red(i, n, 1) {
    int Mx1 = Odd.Ask(1, 1, n, a[i], n), Mx0 = Even.Ask(1, 1, n, a[i], n);
    if (val[i] == 1)
      Odd.Change(1, 1, n, a[i], Mx0 + 1), Even.Change(1, 1, n, a[i], Mx1 + 1);
    else
      Odd.Change(1, 1, n, a[i], Mx1 + 2), Even.Change(1, 1, n, a[i], Mx0 + 2);
  }
  Red(i, n, 1) cnt[i] = cnt[i + 1] + val[i] - 1;
  int cntX = 0, cntY = 0, mxX = 0, mxY = 0;
  Rep(i, 1, n) {
    Odd.Change(1, 1, n, a[i], 1 - 1e9), Even.Change(1, 1, n, a[i], 0);
    if (chk(mxY, cntX + cnt[i + 1] - cntY + (a[i] > mxX)))
      s[i] = '0', cntX += a[i] > mxX, mxX = max(mxX, a[i]);
    else if (chk(max(mxX, a[i]), cntY + cnt[i + 1] - cntX - (a[i] > mxX)))
      s[i] = '0', cntX += a[i] > mxX, mxX = max(mxX, a[i]);
    else
      s[i] = '1', cntY += a[i] > mxY, mxY = max(mxY, a[i]);
  }
  if (cntX == cntY)
    cout << s + 1 << '\n';
  else
    puts("-1");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define fir first
#define sec second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int MAXN = 2e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
// inline char gc(){return getchar();}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int n, a[MAXN], val[MAXN], cnt[MAXN], Mx;
char s[MAXN];
struct SegTree {
#define ls x << 1
#define rs x << 1 | 1
  int Mx[MAXN << 2];
  inline void pushup(int x) { Mx[x] = max(Mx[ls], Mx[rs]); }
  void Change(int x, int l, int r, int p, int v) {
    if (l == r) {
      Mx[x] = v;
      return;
    }
    int mid = l + r >> 1;
    if (p <= mid)
      Change(ls, l, mid, p, v);
    else
      Change(rs, mid + 1, r, p, v);
    pushup(x);
  }
  int Ask(int x, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr)
      return Mx[x];
    int mid = l + r >> 1;
    if (qr <= mid)
      return Ask(ls, l, mid, ql, qr);
    else if (ql > mid)
      return Ask(rs, mid + 1, r, ql, qr);
    else
      return max(Ask(ls, l, mid, ql, mid), Ask(rs, mid + 1, r, mid + 1, qr));
  }
  void Build(int x, int l, int r) {
    Mx[x] = 1 - 1e9;
    if (l == r)
      return;
    int mid = l + r >> 1;
    Build(ls, l, mid), Build(rs, mid + 1, r);
  }
} Odd, Even;
inline bool chk(int mx, int Q) {
  if (Q < 0)
    return 0;
  return Q & 1 ? Odd.Ask(1, 1, n, mx, n) >= Q : Even.Ask(1, 1, n, mx, n) >= Q;
}
int main() {
  n = read();
  Rep(i, 1, n) a[i] = read(), a[i] > Mx ? Mx = a[i], val[i] = 2 : val[i] = 1;
  Odd.Build(1, 1, n);
  Red(i, n, 1) {
    int Mx1 = Odd.Ask(1, 1, n, a[i], n), Mx0 = Even.Ask(1, 1, n, a[i], n);
    if (val[i] == 1)
      Odd.Change(1, 1, n, a[i], Mx0 + 1), Even.Change(1, 1, n, a[i], Mx1 + 1);
    else
      Odd.Change(1, 1, n, a[i], Mx1 + 2), Even.Change(1, 1, n, a[i], Mx0 + 2);
  }
  Red(i, n, 1) cnt[i] = cnt[i + 1] + val[i] - 1;
  int cntX = 0, cntY = 0, mxX = 0, mxY = 0;
  Rep(i, 1, n) {
    Odd.Change(1, 1, n, a[i], 1 - 1e9), Even.Change(1, 1, n, a[i], 0);
    if (chk(mxY, cntX + cnt[i + 1] - cntY + (a[i] > mxX)))
      s[i] = '0', cntX += a[i] > mxX, mxX = max(mxX, a[i]);
    else if (chk(max(mxX, a[i]), cntY + cnt[i + 1] - cntX - (a[i] > mxX)))
      s[i] = '0', cntX += a[i] > mxX, mxX = max(mxX, a[i]);
    else
      s[i] = '1', cntY += a[i] > mxY, mxY = max(mxY, a[i]);
  }
  if (cntX == cntY)
    cout << s + 1 << '\n';
  else
    puts("-1");
  return 0;
}