Problem p03235 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int n, p[200010];
int f[2][200010], ismx[200010];
struct segtree {
#define mid ((l + r) >> 1)
  int rt[200010], ls[3000010], rs[3000010], mx[3000010], cnt;
  il vd update(int &x, int l, int r, const int &p, const int &d) {
    ++cnt;
    ls[cnt] = ls[x], rs[cnt] = rs[x], mx[cnt] = mx[x];
    x = cnt;
    if (l == r) {
      mx[x] = d;
      return;
    }
    if (d > mx[x])
      mx[x] = d;
    if (p <= mid)
      update(ls[x], l, mid, p, d);
    else
      update(rs[x], mid + 1, r, p, d);
  }
  il int query(int x, int l, int r, const int &L) {
    if (L <= l)
      return mx[x];
    if (L <= mid)
      return std::max(query(ls[x], l, mid, L), query(rs[x], mid + 1, r, L));
    else
      return query(rs[x], mid + 1, r, L);
  }
#undef mid
} T0, T1;
char ans1[200010], ans2[200010];
il int Query(int c, int l, int p) {
  if (l > n)
    return c ? -1e9 : 0;
  if (!c)
    return T0.query(T0.rt[l], 1, n, p);
  else
    return T1.query(T1.rt[l], 1, n, p);
}
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  n = gi();
  for (int i = 1; i <= n; ++i)
    p[i] = gi();
  int la1 = 0, lb1 = 0, la2 = 0, lb2 = 0, mxa1 = 0, mxa2 = 0, mxb1 = 0,
      mxb2 = 0, rest = 0;
  for (int i = 1, mx = 0; i <= n; ++i)
    if (p[i] > mx)
      mx = p[i], ismx[i] = 1, ++rest;
  memset(T1.mx, -63, sizeof T1.mx);
  for (int i = n; i; --i) {
    T0.rt[i] = T0.rt[i + 1];
    T1.rt[i] = T1.rt[i + 1];
    if (ismx[i])
      f[0][i] = T0.query(T0.rt[i], 1, n, p[i]) + 2,
      f[1][i] = T1.query(T1.rt[i], 1, n, p[i]) + 2;
    else
      f[0][i] = T1.query(T1.rt[i], 1, n, p[i]) + 1,
      f[1][i] = T0.query(T0.rt[i], 1, n, p[i]) + 1;
    T0.update(T0.rt[i], 1, n, p[i], f[0][i]);
    T1.update(T1.rt[i], 1, n, p[i], f[1][i]);
  }
  auto adda1 = [&](int x) {
    if (x > mxa1)
      ++la1, mxa1 = x;
  };
  auto adda2 = [&](int x) {
    if (x > mxa2)
      ++la2, mxa2 = x;
  };
  auto addb1 = [&](int x) {
    if (x > mxb1)
      ++lb1, mxb1 = x;
  };
  auto addb2 = [&](int x) {
    if (x > mxb2)
      ++lb2, mxb2 = x;
  };
  for (int i = 1; i <= n; ++i) {
    int c = la1 + rest - lb1 + (p[i] > mxa1) - ismx[i];
    if (c >= 0 && Query(c & 1, i + 1, mxb1) >= c)
      ans1[i] = '0', adda1(p[i]);
    else
      ans1[i] = '1', addb1(p[i]);
    c = la2 + rest - lb2 - (p[i] > mxb2) - ismx[i];
    if (c >= 0 && Query(c & 1, i + 1, std::max(p[i], mxb2)) >= c)
      ans2[i] = '0', addb2(p[i]);
    else
      ans2[i] = '1', adda2(p[i]);
    if (ismx[i])
      --rest;
  }
  if (la1 == lb1 && la2 == lb2) {
    if (strcmp(ans1 + 1, ans2 + 1) < 0)
      printf("%s", ans1 + 1);
    else
      printf("%s", ans2 + 1);
  } else if (la1 == lb1)
    printf("%s", ans1 + 1);
  else if (la2 == lb2)
    printf("%s", ans2 + 1);
  else
    puts("-1");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int n, p[200010];
int f[2][200010], ismx[200010];
struct segtree {
#define mid ((l + r) >> 1)
  int rt[200010], ls[6000010], rs[6000010], mx[6000010], cnt;
  il vd update(int &x, int l, int r, const int &p, const int &d) {
    ++cnt;
    ls[cnt] = ls[x], rs[cnt] = rs[x], mx[cnt] = mx[x];
    x = cnt;
    if (l == r) {
      mx[x] = d;
      return;
    }
    if (d > mx[x])
      mx[x] = d;
    if (p <= mid)
      update(ls[x], l, mid, p, d);
    else
      update(rs[x], mid + 1, r, p, d);
  }
  il int query(int x, int l, int r, const int &L) {
    if (L <= l)
      return mx[x];
    if (L <= mid)
      return std::max(query(ls[x], l, mid, L), query(rs[x], mid + 1, r, L));
    else
      return query(rs[x], mid + 1, r, L);
  }
#undef mid
} T0, T1;
char ans1[200010], ans2[200010];
il int Query(int c, int l, int p) {
  if (l > n)
    return c ? -1e9 : 0;
  if (!c)
    return T0.query(T0.rt[l], 1, n, p);
  else
    return T1.query(T1.rt[l], 1, n, p);
}
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  n = gi();
  for (int i = 1; i <= n; ++i)
    p[i] = gi();
  int la1 = 0, lb1 = 0, la2 = 0, lb2 = 0, mxa1 = 0, mxa2 = 0, mxb1 = 0,
      mxb2 = 0, rest = 0;
  for (int i = 1, mx = 0; i <= n; ++i)
    if (p[i] > mx)
      mx = p[i], ismx[i] = 1, ++rest;
  memset(T1.mx, -63, sizeof T1.mx);
  for (int i = n; i; --i) {
    T0.rt[i] = T0.rt[i + 1];
    T1.rt[i] = T1.rt[i + 1];
    if (ismx[i])
      f[0][i] = T0.query(T0.rt[i], 1, n, p[i]) + 2,
      f[1][i] = T1.query(T1.rt[i], 1, n, p[i]) + 2;
    else
      f[0][i] = T1.query(T1.rt[i], 1, n, p[i]) + 1,
      f[1][i] = T0.query(T0.rt[i], 1, n, p[i]) + 1;
    T0.update(T0.rt[i], 1, n, p[i], f[0][i]);
    T1.update(T1.rt[i], 1, n, p[i], f[1][i]);
  }
  auto adda1 = [&](int x) {
    if (x > mxa1)
      ++la1, mxa1 = x;
  };
  auto adda2 = [&](int x) {
    if (x > mxa2)
      ++la2, mxa2 = x;
  };
  auto addb1 = [&](int x) {
    if (x > mxb1)
      ++lb1, mxb1 = x;
  };
  auto addb2 = [&](int x) {
    if (x > mxb2)
      ++lb2, mxb2 = x;
  };
  for (int i = 1; i <= n; ++i) {
    int c = la1 + rest - lb1 + (p[i] > mxa1) - ismx[i];
    if (c >= 0 && Query(c & 1, i + 1, mxb1) >= c)
      ans1[i] = '0', adda1(p[i]);
    else
      ans1[i] = '1', addb1(p[i]);
    c = la2 + rest - lb2 - (p[i] > mxb2) - ismx[i];
    if (c >= 0 && Query(c & 1, i + 1, std::max(p[i], mxb2)) >= c)
      ans2[i] = '0', addb2(p[i]);
    else
      ans2[i] = '1', adda2(p[i]);
    if (ismx[i])
      --rest;
  }
  if (la1 == lb1 && la2 == lb2) {
    if (strcmp(ans1 + 1, ans2 + 1) < 0)
      printf("%s", ans1 + 1);
    else
      printf("%s", ans2 + 1);
  } else if (la1 == lb1)
    printf("%s", ans1 + 1);
  else if (la2 == lb2)
    printf("%s", ans2 + 1);
  else
    puts("-1");
  return 0;
}