Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int N = 100010;
const int INF = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const int MOD = 1000000007;
const double eps = 1e-7;
void douout(double x) { printf("%lf\n", x + 0.0000000001); }
template <class T> void print(T a) {
  cout << a << endl;
  exit(0);
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void upd(T &a, T b) { (a += b) %= MOD; }
template <class T> void mul(T &a, T b) { a = (ll)a * b % MOD; }

int p[N], f[N][20], Log[N];
int n;

int min(int x, int y) { return p[x] < p[y] ? x : y; }

int find(int x, int y) {
  int z = Log[y - x + 1];
  return min(f[x][z], f[y - (1 << z) + 1][z]);
}

struct node {
  int l, r, x, y;
  node(const int a = 0, const int b = 0)
      : l(a), r(b), x(find(a, b)), y(find(x + 1, b + 1)) {}
};

bool operator<(node a, node b) { return p[a.x] > p[b.x]; }

priority_queue<node> q;

signed main() {
  //	file("test") ;
  scanf("%d", &n);
  p[0] = n + 1;
  Log[0] = -1;
  rep(i, 1, n) {
    scanf("%d", &p[i]);
    f[i][0] = f[i][1] = i;
    Log[i] = Log[i >> 1] + 1;
  }
  per(i, n, 1) for (int j = 2; i + (1 << j) - 1 <= n + 1; j++) f[i][j] =
      min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
  q.push((node){1, n});
  while (!q.empty()) {
    node a = q.top();
    q.pop();
    printf("%d %d ", p[a.x], p[a.y]);
    if (a.l < a.x)
      q.push((node){a.l, a.x - 1});
    if (a.x + 1 < a.y)
      q.push((node){a.x + 1, a.y - 1});
    if (a.y < a.r)
      q.push((node){a.y + 1, a.r});
  }
  enter;
  return 0;
}

/*
写代码时请注意：
        1.ll？数组大小，边界？数据范围？
        2.精度？
        3.特判？
        4.至少做一些
思考提醒：
        1.最大值最小->二分？
        2.可以贪心么？不行dp可以么
        3.可以优化么
        4.维护区间用什么数据结构？
        5.统计方案是用dp？模了么？
        6.逆向思维？
*/


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int N = 200010;
const int INF = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const int MOD = 1000000007;
const double eps = 1e-7;
void douout(double x) { printf("%lf\n", x + 0.0000000001); }
template <class T> void print(T a) {
  cout << a << endl;
  exit(0);
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void upd(T &a, T b) { (a += b) %= MOD; }
template <class T> void mul(T &a, T b) { a = (ll)a * b % MOD; }

int p[N], f[N][20], Log[N];
int n;

int min(int x, int y) { return p[x] < p[y] ? x : y; }

int find(int x, int y) {
  int z = Log[y - x + 1];
  return min(f[x][z], f[y - (1 << z) + 1][z]);
}

struct node {
  int l, r, x, y;
  node(const int a = 0, const int b = 0)
      : l(a), r(b), x(find(a, b)), y(find(x + 1, b + 1)) {}
};

bool operator<(node a, node b) { return p[a.x] > p[b.x]; }

priority_queue<node> q;

signed main() {
  //	file("test") ;
  scanf("%d", &n);
  p[0] = n + 1;
  Log[0] = -1;
  rep(i, 1, n) {
    scanf("%d", &p[i]);
    f[i][0] = f[i][1] = i;
    Log[i] = Log[i >> 1] + 1;
  }
  per(i, n, 1) for (int j = 2; i + (1 << j) - 1 <= n + 1; j++) f[i][j] =
      min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
  q.push((node){1, n});
  while (!q.empty()) {
    node a = q.top();
    q.pop();
    printf("%d %d ", p[a.x], p[a.y]);
    if (a.l < a.x)
      q.push((node){a.l, a.x - 1});
    if (a.x + 1 < a.y)
      q.push((node){a.x + 1, a.y - 1});
    if (a.y < a.r)
      q.push((node){a.y + 1, a.r});
  }
  enter;
  return 0;
}

/*
写代码时请注意：
        1.ll？数组大小，边界？数据范围？
        2.精度？
        3.特判？
        4.至少做一些
思考提醒：
        1.最大值最小->二分？
        2.可以贪心么？不行dp可以么
        3.可以优化么
        4.维护区间用什么数据结构？
        5.统计方案是用dp？模了么？
        6.逆向思维？
*/