Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define INF 10e8

typedef pair<int, int> pii;
typedef pair<pii, pii> iiii;

int N, _N;
int P[400020];
priority_queue<iiii, vector<iiii>, greater<iiii>> Q;
int seg_odd[400020], seg_even[400020];
int po[400020];

void init() {
  int res = 1;
  while (N > res)
    res *= 2;
  _N = res;
  for (int i = 0; i < 2 * _N - 1; i++)
    seg_odd[i] = seg_even[i] = INF;
  return;
}

void update(int k, int x) {
  k += _N - 1;
  if (k % 2) {
    seg_even[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      seg_even[k] = min(seg_even[k * 2 + 1], seg_even[k * 2 + 2]);
    }
  } else {
    seg_odd[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      seg_odd[k] = min(seg_odd[k * 2 + 1], seg_odd[k * 2 + 2]);
    }
  }
  return;
}

int getmin_odd(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return seg_odd[k];
  int res1 = getmin_odd(a, b, k * 2 + 1, l, (l + r) / 2);
  int res2 = getmin_odd(a, b, k * 2 + 2, (l + r) / 2, r);
  return min(res1, res2);
}

int getmin_even(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return seg_even[k];
  int res1 = getmin_even(a, b, k * 2 + 1, l, (l + r) / 2);
  int res2 = getmin_even(a, b, k * 2 + 2, (l + r) / 2, r);
  return min(res1, res2);
}

void construct(int l, int r) {
  if (N <= l)
    return;
  if (r - l < 2)
    return;
  int L, R;
  if (l % 2) {
    L = getmin_odd(l, r, 0, 0, _N);
    R = getmin_even(po[L], r, 0, 0, _N);
  } else {
    L = getmin_even(l, r, 0, 0, _N);
    R = getmin_odd(po[L], r, 0, 0, _N);
  }
  Q.push(make_pair(make_pair(L, R), make_pair(l, r)));
  return;
}

int main() {
  scanf("%d", &N);
  init();
  for (int i = 0; i < N; i++) {
    scanf("%d", &P[i]);
    po[P[i]] = i;
    update(i, P[i]);
  }
  construct(0, N);
  for (int i = 0; i < N / 2; i++) {
    iiii now = Q.top();
    Q.pop();
    printf("%d %d", now.first.first, now.first.second);
    if (i != N / 2 - 1)
      printf(" ");
    construct(now.second.first, po[now.first.first]);
    construct(po[now.first.first] + 1, po[now.first.second]);
    construct(po[now.first.second] + 1, now.second.second);
  }
  printf("\n");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF 10e8

typedef pair<int, int> pii;
typedef pair<pii, pii> iiii;

int N, _N;
int P[400020];
priority_queue<iiii, vector<iiii>, greater<iiii>> Q;
int seg_odd[800020], seg_even[800020];
int po[400020];

void init() {
  int res = 1;
  while (N > res)
    res *= 2;
  _N = res;
  for (int i = 0; i < 2 * _N - 1; i++)
    seg_odd[i] = seg_even[i] = INF;
  return;
}

void update(int k, int x) {
  k += _N - 1;
  if (k % 2) {
    seg_even[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      seg_even[k] = min(seg_even[k * 2 + 1], seg_even[k * 2 + 2]);
    }
  } else {
    seg_odd[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      seg_odd[k] = min(seg_odd[k * 2 + 1], seg_odd[k * 2 + 2]);
    }
  }
  return;
}

int getmin_odd(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return seg_odd[k];
  int res1 = getmin_odd(a, b, k * 2 + 1, l, (l + r) / 2);
  int res2 = getmin_odd(a, b, k * 2 + 2, (l + r) / 2, r);
  return min(res1, res2);
}

int getmin_even(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INF;
  if (a <= l && r <= b)
    return seg_even[k];
  int res1 = getmin_even(a, b, k * 2 + 1, l, (l + r) / 2);
  int res2 = getmin_even(a, b, k * 2 + 2, (l + r) / 2, r);
  return min(res1, res2);
}

void construct(int l, int r) {
  if (N <= l)
    return;
  if (r - l < 2)
    return;
  int L, R;
  if (l % 2) {
    L = getmin_odd(l, r, 0, 0, _N);
    R = getmin_even(po[L], r, 0, 0, _N);
  } else {
    L = getmin_even(l, r, 0, 0, _N);
    R = getmin_odd(po[L], r, 0, 0, _N);
  }
  Q.push(make_pair(make_pair(L, R), make_pair(l, r)));
  return;
}

int main() {
  scanf("%d", &N);
  init();
  for (int i = 0; i < N; i++) {
    scanf("%d", &P[i]);
    po[P[i]] = i;
    update(i, P[i]);
  }
  construct(0, N);
  for (int i = 0; i < N / 2; i++) {
    iiii now = Q.top();
    Q.pop();
    printf("%d %d", now.first.first, now.first.second);
    if (i != N / 2 - 1)
      printf(" ");
    construct(now.second.first, po[now.first.first]);
    construct(po[now.first.first] + 1, po[now.first.second]);
    construct(po[now.first.second] + 1, now.second.second);
  }
  printf("\n");
  return 0;
}