Problem p03641 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
///////////////////////////
// 区間最小
struct RMQ {
  ll n;
  vector<l_l> data;
  RMQ(ll vs) {
    n = 1;
    while (n < vs)
      n *= 2;
    data.resize(2 * n - 1, l_l(inf, -1));
  }
  void update(ll k, ll a) {
    k += n - 1;
    data[k] = l_l(a, k - (n - 1));
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  l_l query(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return l_l(inf, -1);
    if (a <= l && r <= b) {
      return data[k];
    } else {
      l_l vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      l_l vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
  l_l query(ll a, ll b) { return query(a, b, 0, 0, n); } //[a,b)の最小値
};
//////////////////////////////////////////////////////

#define N 200005
ll n;
ll p[N];
RMQ od(N), ev(N);
void dfs(ll l, ll r, vector<l_l> &ans) {
  if (l > r)
    return;
  if (l + 1 == r) {
    ans.pb({p[l], p[r]});
    return;
  }
  ll sl = l, sr = l + 1;
  if (l % 2 == 0) {
    sl = ev.query(l, r + 1).se;
  } else {
    sl = od.query(l, r + 1).se;
  }
  if (l % 2 == 1) {
    sr = ev.query(sl, r + 1).se;
  } else {
    sr = od.query(sl, r + 1).se;
  }
  ans.pb({p[sl], p[sr]});
  vector<l_l> v[3];
  dfs(l, sl - 1, v[0]);
  dfs(sl + 1, sr - 1, v[1]);
  dfs(sr + 1, r, v[2]);
  ll sz[3];
  rep(i, 0, 2) sz[i] = v[i].size();
  ll pos[3];
  rep(i, 0, 2) pos[i] = 0;

  while (1) {
    ll minid = -1;
    rep(i, 0, 2) {
      if (pos[i] <= sz[i] - 1) {
        if (minid == -1)
          minid = i;
        else if (v[i][pos[i]] < v[minid][pos[minid]])
          minid = i;
      }
    }
    if (minid == -1)
      break;
    ans.pb(v[minid][pos[minid]]);
    pos[minid]++;
  }
}
int main() {
  fastio cin >> n;
  rep(i, 0, n - 1) cin >> p[i];
  rep(i, 0, n - 1) {
    if (i % 2 == 0)
      ev.update(i, p[i]);
    else
      od.update(i, p[i]);
  }
  vector<l_l> ans;
  dfs(0, n - 1, ans);
  for (auto x : ans)
    cout << x.fi << " " << x.se << " ";

  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
///////////////////////////
// 区間最小
struct RMQ {
  ll n;
  vector<l_l> data;
  RMQ(ll vs) {
    n = 1;
    while (n < vs)
      n *= 2;
    data.resize(2 * n - 1, l_l(inf, -1));
  }
  void update(ll k, ll a) {
    k += n - 1;
    data[k] = l_l(a, k - (n - 1));
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  l_l query(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return l_l(inf, -1);
    if (a <= l && r <= b) {
      return data[k];
    } else {
      l_l vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      l_l vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
  l_l query(ll a, ll b) { return query(a, b, 0, 0, n); } //[a,b)の最小値
};
//////////////////////////////////////////////////////

#define N 200005
ll n;
ll p[N];
RMQ od(N), ev(N);
void dfs(ll l, ll r, vector<l_l> &ans) {
  if (l > r)
    return;
  if (l + 1 == r) {
    ans.pb({p[l], p[r]});
    return;
  }
  ll sl = l, sr = l + 1;
  if (l % 2 == 0) {
    sl = ev.query(l, r + 1).se;
  } else {
    sl = od.query(l, r + 1).se;
  }
  if (l % 2 == 1) {
    sr = ev.query(sl, r + 1).se;
  } else {
    sr = od.query(sl, r + 1).se;
  }
  ans.pb({p[sl], p[sr]});
  if (sl == l && sr == l + 1) {
    dfs(l + 2, r, ans);
    return;
  }
  if (sl == l && sr == r) {
    dfs(l + 1, r - 1, ans);
    return;
  }
  if (sl == r - 1 && sr == r) {
    dfs(l, r - 2, ans);
    return;
  }
  vector<l_l> v[3];
  dfs(l, sl - 1, v[0]);
  dfs(sl + 1, sr - 1, v[1]);
  dfs(sr + 1, r, v[2]);
  ll sz[3];
  rep(i, 0, 2) sz[i] = v[i].size();
  ll pos[3];
  rep(i, 0, 2) pos[i] = 0;

  while (1) {
    ll minid = -1;
    rep(i, 0, 2) {
      if (pos[i] <= sz[i] - 1) {
        if (minid == -1)
          minid = i;
        else if (v[i][pos[i]] < v[minid][pos[minid]])
          minid = i;
      }
    }
    if (minid == -1)
      break;
    ans.pb(v[minid][pos[minid]]);
    pos[minid]++;
  }
}
int main() {
  fastio cin >> n;
  rep(i, 0, n - 1) cin >> p[i];
  rep(i, 0, n - 1) {
    if (i % 2 == 0)
      ev.update(i, p[i]);
    else
      od.update(i, p[i]);
  }
  vector<l_l> ans;
  dfs(0, n - 1, ans);
  for (auto x : ans)
    cout << x.fi << " " << x.se << " ";

  return 0;
}