Problem p03641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define f(a, b) ((1 << (a)) * (b))

int n, p[200005], pp[2][200005], /* x[19][200005],*/ y[2][19][200005],
    z[200005], at[200005], ma[200005], ta;
vector<int> to[200005];
priority_queue<int, vector<int>, greater<int>> q;
bool fi = 1;

/*
int f1(int a, int b, int c, int d) {
        if (f(c, d) == a && f(c, d + 1) == b)
                return x[c][d];
        c--, d = d * 2 + 1;
        if (b <= f(c, d))
                return f1(a, b, c, d - 1);
        if (a >= f(c, d))
                return f1(a, b, c, d);
        int aa = f1(a, f(c, d), c, d - 1);
        int bb = f1(f(c, d), b, c, d);
        return p[aa] <= p[bb] ? aa : bb;
}
*/

int f2(int a, int b, int c, int d) {
  if (f(c, d) == a && f(c, d + 1) == b)
    return y[ta][c][d];
  c--, d = d * 2 + 1;
  if (b <= f(c, d))
    return f2(a, b, c, d - 1);
  if (a >= f(c, d))
    return f2(a, b, c, d);
  int aa = f2(a, f(c, d), c, d - 1);
  int bb = f2(f(c, d), b, c, d);
  return pp[ta][aa] <= pp[ta][bb] ? aa : bb;
}

int go(int a, int b) {
  ta = a & 1;
  int tb = f2(a, b - 1, 18, 0);
  ta = 1 - ta;
  int tc = f2(a + 1, b, 18, 0);
  z[tb] = tc;
  if (tb != a)
    to[tb].push_back(go(a, tb));
  if (tb + 1 != tc)
    to[tb].push_back(go(tb + 1, tc));
  if (tc + 1 != b)
    to[tb].push_back(go(tc + 1, b));
  return tb;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    ta = i & 1;
    scanf("%d", p + i);
    ma[p[i]] = i;
    pp[ta][i] = p[i];
    pp[1 - ta][i] = INT_MAX;
    // x[0][i] = i;
    y[0][0][i] = i;
    y[1][0][i] = i;
    at[i] = i;
  }
  for (int i = 1; i < 19; i++)
    for (int j = 0; f(i, j) < n; j++)
      if (f(i - 1, j * 2 + 1) < n) {
        // x[i][j] = (p[x[i - 1][j * 2]] <= p[x[i - 1][j * 2 + 1]] ? x[i - 1][j
        // * 2] : x[i - 1][j * 2 + 1]);
        y[0][i][j] = (pp[0][y[0][i - 1][j * 2]] <= pp[0][y[0][i - 1][j * 2 + 1]]
                          ? y[0][i - 1][j * 2]
                          : y[0][i - 1][j * 2 + 1]);
        y[1][i][j] = (pp[1][y[1][i - 1][j * 2]] <= pp[1][y[1][i - 1][j * 2 + 1]]
                          ? y[1][i - 1][j * 2]
                          : y[1][i - 1][j * 2 + 1]);
      } else {
        // x[i][j] = x[i - 1][j * 2];
        y[0][i][j] = y[0][i - 1][j * 2];
        y[1][i][j] = y[1][i - 1][j * 2];
      }
  q.push(p[go(0, n)]);
  while (!q.empty()) {
    ta = ma[q.top()];
    q.pop();
    for (int i = 0; i < to[ta].size(); i++)
      q.push(p[to[ta][i]]);
    if (fi)
      fi = 0;
    else
      printf(" ");
    printf("%d %d", p[ta], p[z[ta]]);
  }
  printf("\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define f(a, b) ((1 << (a)) * (b))

int n, p[200005], pp[2][200005], /* x[19][200005],*/ y[2][19][200005],
    z[200005], at[200005], ma[200005], ta;
vector<int> to[200005];
priority_queue<int, vector<int>, greater<int>> q;
bool fi = 1;

/*
int f1(int a, int b, int c, int d) {
        if (f(c, d) == a && f(c, d + 1) == b)
                return x[c][d];
        c--, d = d * 2 + 1;
        if (b <= f(c, d))
                return f1(a, b, c, d - 1);
        if (a >= f(c, d))
                return f1(a, b, c, d);
        int aa = f1(a, f(c, d), c, d - 1);
        int bb = f1(f(c, d), b, c, d);
        return p[aa] <= p[bb] ? aa : bb;
}
*/

int f2(int a, int b, int c, int d) {
  if (f(c, d) == a && f(c, d + 1) == b)
    return y[ta][c][d];
  c--, d = d * 2 + 1;
  if (b <= f(c, d))
    return f2(a, b, c, d - 1);
  if (a >= f(c, d))
    return f2(a, b, c, d);
  int aa = f2(a, f(c, d), c, d - 1);
  int bb = f2(f(c, d), b, c, d);
  return pp[ta][aa] <= pp[ta][bb] ? aa : bb;
}

int go(int a, int b) {
  ta = a & 1;
  int tb = f2(a, b - 1, 18, 0);
  ta = 1 - ta;
  int tc = f2(tb + 1, b, 18, 0);
  z[tb] = tc;
  if (tb != a)
    to[tb].push_back(go(a, tb));
  if (tb + 1 != tc)
    to[tb].push_back(go(tb + 1, tc));
  if (tc + 1 != b)
    to[tb].push_back(go(tc + 1, b));
  return tb;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    ta = i & 1;
    scanf("%d", p + i);
    ma[p[i]] = i;
    pp[ta][i] = p[i];
    pp[1 - ta][i] = INT_MAX;
    // x[0][i] = i;
    y[0][0][i] = i;
    y[1][0][i] = i;
    at[i] = i;
  }
  for (int i = 1; i < 19; i++)
    for (int j = 0; f(i, j) < n; j++)
      if (f(i - 1, j * 2 + 1) < n) {
        // x[i][j] = (p[x[i - 1][j * 2]] <= p[x[i - 1][j * 2 + 1]] ? x[i - 1][j
        // * 2] : x[i - 1][j * 2 + 1]);
        y[0][i][j] = (pp[0][y[0][i - 1][j * 2]] <= pp[0][y[0][i - 1][j * 2 + 1]]
                          ? y[0][i - 1][j * 2]
                          : y[0][i - 1][j * 2 + 1]);
        y[1][i][j] = (pp[1][y[1][i - 1][j * 2]] <= pp[1][y[1][i - 1][j * 2 + 1]]
                          ? y[1][i - 1][j * 2]
                          : y[1][i - 1][j * 2 + 1]);
      } else {
        // x[i][j] = x[i - 1][j * 2];
        y[0][i][j] = y[0][i - 1][j * 2];
        y[1][i][j] = y[1][i - 1][j * 2];
      }
  q.push(p[go(0, n)]);
  while (!q.empty()) {
    ta = ma[q.top()];
    q.pop();
    for (int i = 0; i < to[ta].size(); i++)
      q.push(p[to[ta][i]]);
    if (fi)
      fi = 0;
    else
      printf(" ");
    printf("%d %d", p[ta], p[z[ta]]);
  }
  printf("\n");
  return 0;
}