Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define ll long long
#define ull unsigned long long
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define mst(ss, b) memset(ss, b, sizeof(ss));
#ifdef Cyberdebut
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
typedef pair<int, int> pii;
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
ll pow_mod(ll x, ll n, ll mod) {
  ll ret = 1LL;
  while (n) {
    if (n & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}
const int inf = 0x3f3f3f3f;
const ll INF = (1LL << 62) - 1;
const ll mod = 1e9 + 7;
const int N = 1e5 + 5;

int n, a[N], pos[N];

int dp[2][N][31], lg[N]; // dp[i][j]从i开始，长度为2^j的一段元素中的最小值
// 初始化RMQ,  b数组下标从1开始，从0开始简单修改
void rmq_init() {
  for (int i = 2; i <= n; i++)
    if (i & (i - 1))
      lg[i] = lg[i - 1];
    else
      lg[i] = lg[i - 1] + 1;
  for (int k = 0; k < 2; k++)
    for (int i = 1; i <= n; i++)
      dp[k][i][0] = (i % 2 == k) ? a[i] : n + 1;
  for (int k = 0; k < 2; k++)
    for (int j = 1; (1 << j) <= n; j++)
      for (int i = 1; i + (1 << j) - 1 <= n; i++)
        dp[k][i][j] = min(dp[k][i][j - 1], dp[k][i + (1 << (j - 1))][j - 1]);
}
// 查询最大值 ,中间部分是交叉的
int rmq(int x, int l, int r) {
  int k = lg[r - l + 1];
  return min(dp[x][l][k], dp[x][r - (1 << k) + 1][k]);
}

pair<int, int> calc(int l, int r) {
  int x = rmq(l & 1, l, r);
  int y = rmq((pos[x] + 1) & 1, pos[x] + 1, r);
  return {-x, -y};
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), pos[a[i]] = i;
  rmq_init();

  priority_queue<pair<pii, pii>> pq;
  pq.push({calc(1, n), {1, n}});
  while (pq.size()) {
    auto now = pq.top();
    pq.pop();
    int x = -now.ff.ff, y = -now.ff.ss;
    printf("%d %d ", x, y);
    int l = now.ss.ff, r = now.ss.ss;
    x = pos[x], y = pos[y];
    if (l < x - 1)
      pq.push({calc(l, x - 1), {l, x - 1}});
    if (x + 1 < y - 1)
      pq.push({calc(x + 1, y - 1), {x + 1, y - 1}});
    if (y + 1 < r)
      pq.push({calc(y + 1, r), {y + 1, r}});
  }
  puts("");
  return 0;
}

// lower_bound()
// __builtin_popcount()
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// const double eps=1e-12;
// #pragma comment(linker,"/STACK:102400000,102400000")


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define ll long long
#define ull unsigned long long
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define mst(ss, b) memset(ss, b, sizeof(ss));
#ifdef Cyberdebut
#define dbg(x) cerr << #x << " = " << x << endl
#else
#define dbg(x)
#endif
typedef pair<int, int> pii;
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
ll pow_mod(ll x, ll n, ll mod) {
  ll ret = 1LL;
  while (n) {
    if (n & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}
const int inf = 0x3f3f3f3f;
const ll INF = (1LL << 62) - 1;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;

int n, a[N], pos[N];

int dp[2][N][31], lg[N]; // dp[i][j]从i开始，长度为2^j的一段元素中的最小值
// 初始化RMQ,  b数组下标从1开始，从0开始简单修改
void rmq_init() {
  for (int i = 2; i <= n; i++)
    if (i & (i - 1))
      lg[i] = lg[i - 1];
    else
      lg[i] = lg[i - 1] + 1;
  for (int k = 0; k < 2; k++)
    for (int i = 1; i <= n; i++)
      dp[k][i][0] = (i % 2 == k) ? a[i] : n + 1;
  for (int k = 0; k < 2; k++)
    for (int j = 1; (1 << j) <= n; j++)
      for (int i = 1; i + (1 << j) - 1 <= n; i++)
        dp[k][i][j] = min(dp[k][i][j - 1], dp[k][i + (1 << (j - 1))][j - 1]);
}
// 查询最大值 ,中间部分是交叉的
int rmq(int x, int l, int r) {
  int k = lg[r - l + 1];
  return min(dp[x][l][k], dp[x][r - (1 << k) + 1][k]);
}

pair<int, int> calc(int l, int r) {
  int x = rmq(l & 1, l, r);
  int y = rmq((pos[x] + 1) & 1, pos[x] + 1, r);
  return {-x, -y};
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), pos[a[i]] = i;
  rmq_init();

  priority_queue<pair<pii, pii>> pq;
  pq.push({calc(1, n), {1, n}});
  while (pq.size()) {
    auto now = pq.top();
    pq.pop();
    int x = -now.ff.ff, y = -now.ff.ss;
    printf("%d %d ", x, y);
    int l = now.ss.ff, r = now.ss.ss;
    x = pos[x], y = pos[y];
    if (l < x - 1)
      pq.push({calc(l, x - 1), {l, x - 1}});
    if (x + 1 < y - 1)
      pq.push({calc(x + 1, y - 1), {x + 1, y - 1}});
    if (y + 1 < r)
      pq.push({calc(y + 1, r), {y + 1, r}});
  }
  puts("");
  return 0;
}

// lower_bound()
// __builtin_popcount()
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// const double eps=1e-12;
// #pragma comment(linker,"/STACK:102400000,102400000")