Problem p03235 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int n;
int nu[200200];
struct SEG {
  int ma[200200];
  void build(int rt, int l, int r) {
    ma[rt] = -inf;
    if (l == r)
      return;
    int mid = (l + r) / 2;
    build(rt * 2, l, mid);
    build(rt * 2 + 1, mid + 1, r);
  }
  void change(int rt, int l, int r, int p, int v) {
    if (l == r) {
      ma[rt] = v;
      return;
    }
    int mid = (l + r) / 2;
    if (mid >= p)
      change(rt * 2, l, mid, p, v);
    else
      change(rt * 2 + 1, mid + 1, r, p, v);
    ma[rt] = max(ma[rt * 2], ma[rt * 2 + 1]);
  }
  int query(int rt, int l, int r, int st, int ed) {
    if (l > ed || r < st)
      return -inf;
    if (l >= st && r <= ed)
      return ma[rt];
    int mid = (l + r) / 2;
    return max(query(rt * 2, l, mid, st, ed),
               query(rt * 2 + 1, mid + 1, r, st, ed));
  }
} seg[2];
int val[200200];
int cnt[200200];
char ans[200200];
bool check(int v, int c) {
  if (c < 0)
    return 0;
  return seg[c & 1].query(1, 1, n, v, n) >= c;
}
int main() {
  scanf("%d", &n);
  int mx = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &nu[i]);
    val[i] = 1;
    if (nu[i] > mx)
      val[i]++, mx = nu[i];
  }
  seg[1].build(1, 1, n);
  int m0, m1;
  for (int i = n; i; --i) {
    cnt[i] = cnt[i + 1] + val[i] - 1;
    m0 = seg[0].query(1, 1, n, nu[i], n);
    m1 = seg[1].query(1, 1, n, nu[i], n);
    if (val[i] == 2) {
      seg[0].change(1, 1, n, nu[i], m0 + 2);
      seg[1].change(1, 1, n, nu[i], m1 + 2);
    } else {
      seg[0].change(1, 1, n, nu[i], m1 + 1);
      seg[1].change(1, 1, n, nu[i], m0 + 1);
    }
  }
  int lx = 0, ly = 0;
  mx = 0;
  int my = 0;
  for (int i = 1; i <= n; ++i) {
    seg[0].change(1, 1, n, nu[i], 0);
    seg[1].change(1, 1, n, nu[i], -inf);
    if (check(my, lx + (nu[i] > mx) + cnt[i + 1] - ly))
      ans[i] = '0';
    else if (check(max(mx, nu[i]), ly + cnt[i + 1] - lx - (nu[i] > mx)))
      ans[i] = '0';
    else
      ans[i] = '1';
    if (ans[i] == '0')
      lx += (nu[i] > mx), mx = max(mx, nu[i]);
    else
      ly += (nu[i] > my), my = max(my, nu[i]);
  }
  if (lx != ly) {
    puts("-1");
    return 0;
  }
  printf("%s\n", ans + 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int n;
int nu[200200];
struct SEG {
  int ma[2000200];
  void build(int rt, int l, int r) {
    ma[rt] = -inf;
    if (l == r)
      return;
    int mid = (l + r) / 2;
    build(rt * 2, l, mid);
    build(rt * 2 + 1, mid + 1, r);
  }
  void change(int rt, int l, int r, int p, int v) {
    if (l == r) {
      ma[rt] = v;
      return;
    }
    int mid = (l + r) / 2;
    if (mid >= p)
      change(rt * 2, l, mid, p, v);
    else
      change(rt * 2 + 1, mid + 1, r, p, v);
    ma[rt] = max(ma[rt * 2], ma[rt * 2 + 1]);
  }
  int query(int rt, int l, int r, int st, int ed) {
    if (l > ed || r < st)
      return -inf;
    if (l >= st && r <= ed)
      return ma[rt];
    int mid = (l + r) / 2;
    return max(query(rt * 2, l, mid, st, ed),
               query(rt * 2 + 1, mid + 1, r, st, ed));
  }
} seg[2];
int val[200200];
int cnt[200200];
char ans[200200];
bool check(int v, int c) {
  if (c < 0)
    return 0;
  return seg[c & 1].query(1, 1, n, v, n) >= c;
}
int main() {
  scanf("%d", &n);
  int mx = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &nu[i]);
    val[i] = 1;
    if (nu[i] > mx)
      val[i]++, mx = nu[i];
  }
  seg[1].build(1, 1, n);
  int m0, m1;
  for (int i = n; i; --i) {
    cnt[i] = cnt[i + 1] + val[i] - 1;
    m0 = seg[0].query(1, 1, n, nu[i], n);
    m1 = seg[1].query(1, 1, n, nu[i], n);
    if (val[i] == 2) {
      seg[0].change(1, 1, n, nu[i], m0 + 2);
      seg[1].change(1, 1, n, nu[i], m1 + 2);
    } else {
      seg[0].change(1, 1, n, nu[i], m1 + 1);
      seg[1].change(1, 1, n, nu[i], m0 + 1);
    }
  }
  int lx = 0, ly = 0;
  mx = 0;
  int my = 0;
  for (int i = 1; i <= n; ++i) {
    seg[0].change(1, 1, n, nu[i], 0);
    seg[1].change(1, 1, n, nu[i], -inf);
    if (check(my, lx + (nu[i] > mx) + cnt[i + 1] - ly))
      ans[i] = '0';
    else if (check(max(mx, nu[i]), ly + cnt[i + 1] - lx - (nu[i] > mx)))
      ans[i] = '0';
    else
      ans[i] = '1';
    if (ans[i] == '0')
      lx += (nu[i] > mx), mx = max(mx, nu[i]);
    else
      ly += (nu[i] > my), my = max(my, nu[i]);
  }
  if (lx != ly) {
    puts("-1");
    return 0;
  }
  printf("%s\n", ans + 1);
  return 0;
}