Problem p03235 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;
int n;
int p[131072];
bool mx[131072];
int c1[131072], c2[131072];
int wz1[2097152], wz2[2097152], pre1[2097152], pre2[2097152], tim1[2097152],
    tim2[2097152], num1 = 0, num2 = 0;
void add(int wz, int val1, int val2, int tim) {
  for (int i = wz; i > 0; i -= (i & (-i))) {
    if (val1 > c1[i]) {
      wz1[num1] = i;
      pre1[num1] = c1[i];
      tim1[num1] = tim;
      num1++;
      c1[i] = val1;
    }
    if (val2 > c2[i]) {
      wz2[num2] = i;
      pre2[num2] = c2[i];
      tim2[num2] = tim;
      num2++;
      c2[i] = val2;
    }
  }
}
void query(int wz, int &val1, int &val2) {
  val1 = -999999;
  val2 = 0;
  for (int i = wz; i <= n; i += (i & (-i))) {
    if (c1[i] > val1) {
      val1 = c1[i];
    }
    if (c2[i] > val2) {
      val2 = c2[i];
    }
  }
}
void del(int tim) {
  while (num1) {
    if (tim1[num1 - 1] != tim) {
      break;
    }
    num1--;
    c1[wz1[num1]] = pre1[num1];
  }
  while (num2) {
    if (tim2[num2 - 1] != tim) {
      break;
    }
    num2--;
    c2[wz2[num2]] = pre2[num2];
  }
}
bool chk2(int val, int val1, int val2) {
  if (val < 0) {
    return false;
  }
  if (val & 1) {
    return val <= val1;
  }
  return val <= val2;
}
bool chk(int numx, int numy, int mxx, int mxy, int m) {
  int val1, val2;
  query(mxx, val1, val2);
  if (chk2(numy - numx + m, val1, val2)) {
    return true;
  }
  query(mxy, val1, val2);
  return chk2(numx - numy + m, val1, val2);
}
int main() {
  int last = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    if (p[i] > last) {
      mx[i] = true;
      last = p[i];
    }
    c1[i] = -999999;
    c2[i] = 0;
  }
  int m = 0;
  for (int i = n; i >= 1; i--) {
    int val1, val2;
    query(p[i], val1, val2);
    if (mx[i]) {
      m++;
      add(p[i], val1 + 2, val2 + 2, i);
    } else {
      add(p[i], val2 + 1, val1 + 1, i);
    }
  }
  int numx = 0, numy = 0, mxx = 1, mxy = 1;
  if (!chk(numx, numy, mxx, mxy, m)) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    del(i);
    if (mx[i]) {
      m--;
    }
    int numx2 = numx, numy2 = numy, mxx2 = mxx, mxy2 = mxy;
    if (p[i] >= mxx) {
      mxx2 = p[i];
      numx2++;
    }
    if (p[i] >= mxy) {
      mxy2 = p[i];
      numy2++;
    }
    if (chk(numx2, numy, mxx2, mxy, m)) {
      putchar('0');
      numx = numx2;
      mxx = mxx2;
    } else {
      putchar('1');
      numy = numy2;
      mxy = mxy2;
    }
  }
  putchar('\n');
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int n;
int p[262144];
bool mx[262144];
int c1[262144], c2[262144];
int wz1[4194304], wz2[4194304], pre1[4194304], pre2[4194304], tim1[4194304],
    tim2[4194304], num1 = 0, num2 = 0;
void add(int wz, int val1, int val2, int tim) {
  for (int i = wz; i > 0; i -= (i & (-i))) {
    if (val1 > c1[i]) {
      wz1[num1] = i;
      pre1[num1] = c1[i];
      tim1[num1] = tim;
      num1++;
      c1[i] = val1;
    }
    if (val2 > c2[i]) {
      wz2[num2] = i;
      pre2[num2] = c2[i];
      tim2[num2] = tim;
      num2++;
      c2[i] = val2;
    }
  }
}
void query(int wz, int &val1, int &val2) {
  val1 = -999999;
  val2 = 0;
  for (int i = wz; i <= n; i += (i & (-i))) {
    if (c1[i] > val1) {
      val1 = c1[i];
    }
    if (c2[i] > val2) {
      val2 = c2[i];
    }
  }
}
void del(int tim) {
  while (num1) {
    if (tim1[num1 - 1] != tim) {
      break;
    }
    num1--;
    c1[wz1[num1]] = pre1[num1];
  }
  while (num2) {
    if (tim2[num2 - 1] != tim) {
      break;
    }
    num2--;
    c2[wz2[num2]] = pre2[num2];
  }
}
bool chk2(int val, int val1, int val2) {
  if (val < 0) {
    return false;
  }
  if (val & 1) {
    return val <= val1;
  }
  return val <= val2;
}
bool chk(int numx, int numy, int mxx, int mxy, int m) {
  int val1, val2;
  query(mxx, val1, val2);
  if (chk2(numy - numx + m, val1, val2)) {
    return true;
  }
  query(mxy, val1, val2);
  return chk2(numx - numy + m, val1, val2);
}
int main() {
  int last = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    if (p[i] > last) {
      mx[i] = true;
      last = p[i];
    }
    c1[i] = -999999;
    c2[i] = 0;
  }
  int m = 0;
  for (int i = n; i >= 1; i--) {
    int val1, val2;
    query(p[i], val1, val2);
    if (mx[i]) {
      m++;
      add(p[i], val1 + 2, val2 + 2, i);
    } else {
      add(p[i], val2 + 1, val1 + 1, i);
    }
  }
  int numx = 0, numy = 0, mxx = 1, mxy = 1;
  if (!chk(numx, numy, mxx, mxy, m)) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    del(i);
    if (mx[i]) {
      m--;
    }
    int numx2 = numx, numy2 = numy, mxx2 = mxx, mxy2 = mxy;
    if (p[i] >= mxx) {
      mxx2 = p[i];
      numx2++;
    }
    if (p[i] >= mxy) {
      mxy2 = p[i];
      numy2++;
    }
    if (chk(numx2, numy, mxx2, mxy, m)) {
      putchar('0');
      numx = numx2;
      mxx = mxx2;
    } else {
      putchar('1');
      numy = numy2;
      mxy = mxy2;
    }
  }
  putchar('\n');
  return 0;
}