Problem p02282 (C++)

Buggy submission (status: Runtime Error):
/*! if g++ -g alds_1_7_d.cpp -o alds_1_7_d.out; then ./alds_1_7_d.out <
 * alds_1_7_d.test; fi
 */

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

vector<int> pre(0);
vector<int> ipre(0);
vector<int> mid(0);
vector<int> imid(0);

class node {
public:
  int id, left, right;
  node() : id(-1), left(-1), right(-1) {}
};

vector<node> tree(0);

int doit(int pl, int pr, int ml, int mr) {
  if (pl == pr)
    return -1;
  int root_id = pre[pl];

  int ml1 = ml;
  int mr1 = imid[root_id];
  int ml2 = mr1 + 1;
  int mr2 = mr;

  int pl1 = pl + 1;
  int pr1 = pl1 + mr1 - ml1;
  int pl2 = pr1;
  int pr2 = pr;

  node &nd = tree[root_id - 1];
  nd.id = root_id;
  nd.left = doit(pl1, pr1, ml1, mr1);
  nd.right = doit(pl2, pr2, ml2, mr2);
  return root_id;
}

vector<int> ret(0);

void post(int root) {
  node &nd = tree[root - 1];
  if (nd.left > 0)
    post(nd.left);
  if (nd.right > 0)
    post(nd.right);
  ret.push_back(nd.id);
}

int main() {
  int n;
  cin >> n;
  pre.resize(n);
  mid.resize(n);
  ipre.resize(n);
  imid.resize(n);
  tree.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> pre[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> mid[i];
  }
  for (int i = 0; i < n; i++) {
    ipre[pre[i]] = i;
    imid[mid[i]] = i;
  }
  int root_id = doit(0, n, 0, n);
  post(root_id);
  for (int i = 0; i < n - 1; i++) {
    cout << ret[i] << " ";
  }
  cout << ret.back() << endl;

  return 0;
}

Fixed submission:
/*! if g++ -g alds_1_7_d.cpp -o alds_1_7_d.out; then ./alds_1_7_d.out <
 * alds_1_7_d.test; fi
 */

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

vector<int> pre(0);
vector<int> ipre(0);
vector<int> mid(0);
vector<int> imid(0);

class node {
public:
  int id, left, right;
  node() : id(-1), left(-1), right(-1) {}
};

vector<node> tree(0);

int doit(int pl, int pr, int ml, int mr) {
  if (pl == pr)
    return -1;
  int root_id = pre[pl];

  int ml1 = ml;
  int mr1 = imid[root_id];
  int ml2 = mr1 + 1;
  int mr2 = mr;

  int pl1 = pl + 1;
  int pr1 = pl1 + mr1 - ml1;
  int pl2 = pr1;
  int pr2 = pr;

  node &nd = tree[root_id - 1];
  nd.id = root_id;
  nd.left = doit(pl1, pr1, ml1, mr1);
  nd.right = doit(pl2, pr2, ml2, mr2);
  return root_id;
}

vector<int> ret(0);

void post(int root) {
  node &nd = tree[root - 1];
  if (nd.left > 0)
    post(nd.left);
  if (nd.right > 0)
    post(nd.right);
  ret.push_back(nd.id);
}

int main() {
  int n;
  cin >> n;
  pre.resize(n);
  mid.resize(n);
  ipre.resize(n + 1);
  imid.resize(n + 1);
  tree.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> pre[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> mid[i];
  }
  for (int i = 0; i < n; i++) {
    ipre[pre[i]] = i;
    imid[mid[i]] = i;
  }
  int root_id = doit(0, n, 0, n);
  post(root_id);
  for (int i = 0; i < n - 1; i++) {
    cout << ret[i] << " ";
  }
  cout << ret.back() << endl;

  return 0;
}