Problem p03235 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ET cout << "\n"
#define MEM(i, j) memset(i, j, sizeof i)
#define ALL(v) v.begin(), v.end()
#define DB(a, s, e)                                                            \
  {                                                                            \
    for (int i = s; i < e; ++i)                                                \
      cerr << a[i] << " ";                                                     \
    ET;                                                                        \
  }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int arr[200005], seg[2][200005];
bitset<200005> high;
string ans;

void modify(int x, int l, int r, int rt, int t, int v) {
  if (l == r)
    return seg[t][rt] = v, void();
  int m = l + r >> 1;
  if (x <= m)
    modify(x, l, m, rt << 1, t, v);
  else
    modify(x, m + 1, r, rt << 1 | 1, t, v);
  seg[t][rt] = max(seg[t][rt << 1], seg[t][rt << 1 | 1]);
}

int query(int L, int R, int l, int r, int rt, int t) {
  if (L <= l && R >= r)
    return seg[t][rt];
  int m = l + r >> 1;
  if (R <= m)
    return query(L, R, l, m, rt << 1, t);
  if (L > m)
    return query(L, R, m + 1, r, rt << 1 | 1, t);
  return max(query(L, R, l, m, rt << 1, t),
             query(L, R, m + 1, r, rt << 1 | 1, t));
}

int main() {
  jizz int n, mx = 0, a, b, ma, mb, cnt;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i], high[i] = (arr[i] > mx), mx = max(mx, arr[i]);
  for (int i = n; i >= 1; --i) {
    a = query(arr[i] + 1, n + 1, 1, n + 1, 1, 0) + high[i] + 1,
    b = query(arr[i] + 1, n + 1, 1, n + 1, 1, 1);
    if (b & 1)
      b += high[i] + 1, modify(arr[i], 1, n + 1, 1, b & 1, b);
    modify(arr[i], 1, n + 1, 1, a & 1, a);
  }
  cnt = high.count();
  if (seg[cnt & 1][1] < cnt)
    return cout << "-1\n", 0;
  a = b = ma = mb = 0;
  for (int i = 1; i <= n; ++i) {
    modify(arr[i], 1, n + 1, 1, 0, 0), modify(arr[i], 1, n + 1, 1, 1, 0);
    cnt -= high[i];
    int t = a - b + cnt + (arr[i] > ma);
    if (t >= 0 && query(mb + 1, n + 1, 1, n + 1, 1, t & 1) >= t)
      a += (arr[i] > ma), ma = max(ma, arr[i]), ans.pb('0');
    else if (t = b - a + cnt - (arr[i] > ma),
             t >= 0 &&
                 query(max(ma, arr[i]) + 1, n + 1, 1, n + 1, 1, t & 1) >= t)
      a += (arr[i] > ma), ma = max(ma, arr[i]), ans.pb('0');
    else
      b += (arr[i] > mb), mb = max(mb, arr[i]), ans.pb('1');
  }
  cout << ans << "\n";
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ET cout << "\n"
#define MEM(i, j) memset(i, j, sizeof i)
#define ALL(v) v.begin(), v.end()
#define DB(a, s, e)                                                            \
  {                                                                            \
    for (int i = s; i < e; ++i)                                                \
      cerr << a[i] << " ";                                                     \
    ET;                                                                        \
  }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int arr[200005], seg[2][800005];
bitset<200005> high;
string ans;

void modify(int x, int l, int r, int rt, int t, int v) {
  if (l == r)
    return seg[t][rt] = v, void();
  int m = l + r >> 1;
  if (x <= m)
    modify(x, l, m, rt << 1, t, v);
  else
    modify(x, m + 1, r, rt << 1 | 1, t, v);
  seg[t][rt] = max(seg[t][rt << 1], seg[t][rt << 1 | 1]);
}

int query(int L, int R, int l, int r, int rt, int t) {
  if (L <= l && R >= r)
    return seg[t][rt];
  int m = l + r >> 1;
  if (R <= m)
    return query(L, R, l, m, rt << 1, t);
  if (L > m)
    return query(L, R, m + 1, r, rt << 1 | 1, t);
  return max(query(L, R, l, m, rt << 1, t),
             query(L, R, m + 1, r, rt << 1 | 1, t));
}

int main() {
  jizz int n, mx = 0, a, b, ma, mb, cnt;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i], high[i] = (arr[i] > mx), mx = max(mx, arr[i]);
  for (int i = n; i >= 1; --i) {
    a = query(arr[i] + 1, n + 1, 1, n + 1, 1, 0) + high[i] + 1,
    b = query(arr[i] + 1, n + 1, 1, n + 1, 1, 1);
    if (b & 1)
      b += high[i] + 1, modify(arr[i], 1, n + 1, 1, b & 1, b);
    modify(arr[i], 1, n + 1, 1, a & 1, a);
  }
  cnt = high.count();
  if (seg[cnt & 1][1] < cnt)
    return cout << "-1\n", 0;
  a = b = ma = mb = 0;
  for (int i = 1; i <= n; ++i) {
    modify(arr[i], 1, n + 1, 1, 0, 0), modify(arr[i], 1, n + 1, 1, 1, 0);
    cnt -= high[i];
    int t = a - b + cnt + (arr[i] > ma);
    if (t >= 0 && query(mb + 1, n + 1, 1, n + 1, 1, t & 1) >= t)
      a += (arr[i] > ma), ma = max(ma, arr[i]), ans.pb('0');
    else if (t = b - a + cnt - (arr[i] > ma),
             t >= 0 &&
                 query(max(ma, arr[i]) + 1, n + 1, 1, n + 1, 1, t & 1) >= t)
      a += (arr[i] > ma), ma = max(ma, arr[i]), ans.pb('0');
    else
      b += (arr[i] > mb), mb = max(mb, arr[i]), ans.pb('1');
  }
  cout << ans << "\n";
}