Problem p02920 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma g++ optimize(2)
using namespace std;
int n, sum = 1;
int tot, root;
struct node {
  int l, r;
  int dat, val;
  int size;
} f[200010];
void update(int p) { f[p].size = f[f[p].l].size + f[f[p].r].size + 1; }
int New(int val) {
  f[++tot].val = val;
  f[tot].dat = rand();
  f[tot].size = 1;
  return tot;
}
void split(int p, int val, int &x, int &y) {
  if (p == 0) {
    x = 0, y = 0;
    return;
  }
  if (f[p].val <= val) {
    x = p;
    split(f[p].r, val, f[p].r, y);
  } else {
    y = p;
    split(f[p].l, val, x, f[p].l);
  }
  update(p);
}
int merge(int a, int b) {
  if (!a || !b)
    return a + b;
  if (f[a].dat < f[b].dat) {
    f[a].r = merge(f[a].r, b);
    update(a);
    return a;
  } else {
    f[b].l = merge(a, f[b].l);
    update(b);
    return b;
  }
}
void insert(int val) {
  int x, y;
  split(root, val, x, y);
  root = merge(merge(x, New(val)), y);
}
void remove(int val) {
  int x, y, z;
  split(root, val, x, z);
  split(x, val - 1, x, y);
  y = merge(f[y].l, f[y].r);
  root = merge(merge(x, y), z);
}
int getRank(int val) {
  int x, y;
  split(root, val - 1, x, y);
  int ans = f[x].size + 1;
  root = merge(x, y);
  return ans;
}
int getVal(int p, int rank) {
  while (p) {
    if (f[f[p].l].size >= rank)
      p = f[p].l;
    else if (f[f[p].l].size + 1 == rank)
      return f[p].val;
    else
      rank -= f[f[p].l].size + 1, p = f[p].r;
  }
}
int getPre(int val) {
  int x, y;
  split(root, val - 1, x, y);
  int ans = getVal(x, f[x].size);
  root = merge(x, y);
  return ans;
}
int getNext(int val) {
  int x, y;
  split(root, val, x, y);
  int ans = getVal(y, 1);
  root = merge(x, y);
  return ans;
}
int a[2000010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= pow(2, n); i++) {
    int x;
    scanf("%d", &x);
    insert(x);
  }
  insert(-1);
  a[1] = getPre(1999999999);
  remove(a[1]);
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= pow(2, i); j++) {
      int x = getPre(a[j]);
      remove(x);
      if (x == -1) {
        cout << "No" << endl;
        return 0;
      }
      tot++, a[tot] = x;
    }
    sort(a + 1, a + tot + 1, greater<int>());
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma g++ optimize(2)
using namespace std;
int n, sum = 1;
int tot, root;
struct node {
  int l, r;
  int dat, val;
  int size;
} f[2000010];
void update(int p) { f[p].size = f[f[p].l].size + f[f[p].r].size + 1; }
int New(int val) {
  f[++tot].val = val;
  f[tot].dat = rand();
  f[tot].size = 1;
  return tot;
}
void split(int p, int val, int &x, int &y) {
  if (p == 0) {
    x = 0, y = 0;
    return;
  }
  if (f[p].val <= val) {
    x = p;
    split(f[p].r, val, f[p].r, y);
  } else {
    y = p;
    split(f[p].l, val, x, f[p].l);
  }
  update(p);
}
int merge(int a, int b) {
  if (!a || !b)
    return a + b;
  if (f[a].dat < f[b].dat) {
    f[a].r = merge(f[a].r, b);
    update(a);
    return a;
  } else {
    f[b].l = merge(a, f[b].l);
    update(b);
    return b;
  }
}
void insert(int val) {
  int x, y;
  split(root, val, x, y);
  root = merge(merge(x, New(val)), y);
}
void remove(int val) {
  int x, y, z;
  split(root, val, x, z);
  split(x, val - 1, x, y);
  y = merge(f[y].l, f[y].r);
  root = merge(merge(x, y), z);
}
int getRank(int val) {
  int x, y;
  split(root, val - 1, x, y);
  int ans = f[x].size + 1;
  root = merge(x, y);
  return ans;
}
int getVal(int p, int rank) {
  while (p) {
    if (f[f[p].l].size >= rank)
      p = f[p].l;
    else if (f[f[p].l].size + 1 == rank)
      return f[p].val;
    else
      rank -= f[f[p].l].size + 1, p = f[p].r;
  }
}
int getPre(int val) {
  int x, y;
  split(root, val - 1, x, y);
  int ans = getVal(x, f[x].size);
  root = merge(x, y);
  return ans;
}
int getNext(int val) {
  int x, y;
  split(root, val, x, y);
  int ans = getVal(y, 1);
  root = merge(x, y);
  return ans;
}
int a[2000010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= pow(2, n); i++) {
    int x;
    scanf("%d", &x);
    insert(x);
  }
  insert(-1);
  a[1] = getPre(1999999999);
  remove(a[1]);
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= pow(2, i); j++) {
      int x = getPre(a[j]);
      remove(x);
      if (x == -1) {
        cout << "No" << endl;
        return 0;
      }
      tot++, a[tot] = x;
    }
    sort(a + 1, a + tot + 1, greater<int>());
  }
  cout << "Yes" << endl;
  return 0;
}