Problem p03641 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define lson l, mid, i << 1
#define rson mid + 1, r, i << 1 | 1
#define PII pair<int, int>
#define Size(x) (int)(x.size())
using namespace std;
typedef long long LL;
// const int MOD = 1e9+7;
const auto INF = 0x3f3f3f3f;
const int N = 2e5 + 5;
struct node {
  int L, R;
  int l, r;
  bool operator<(const struct node &u) const {
    if (l == u.l)
      return r > u.r;
    return l > u.l;
  }
};

priority_queue<node> pri;
int dis[N], a[N];
int sum[2][N << 2], id[N];
vector<PII> answer;

void pustup(int i) {
  sum[0][i] = min(sum[0][i << 1], sum[0][i << 1 | 1]);
  sum[1][i] = min(sum[1][i << 1], sum[1][i << 1 | 1]);
}

void build(int l, int r, int i) {
  if (l == r) {
    int x;
    scanf("%d", &x);
    a[l] = x;
    dis[x] = l;
    sum[l & 1][i] = x;
    sum[!(l & 1)][i] = INF;
    id[x] = (l & 1);
    return;
  }
  int mid = (l + r) >> 1;
  build(lson);
  build(rson);
  pustup(i);
}

int query(int id, int L, int R, int l, int r, int i) {
  if (L <= l && r <= R) {
    return sum[!id][i];
  }
  int ans = INF, mid = (l + r) >> 1;
  if (L <= mid)
    ans = query(id, L, R, lson);
  if (R > mid)
    ans = min(ans, query(id, L, R, rson));
  return ans;
}

int n;

void solve(int L, int R) {
  if (L > R)
    return;
  int i = INF;
  for (int j = L; j <= R; j++) {
    int x = R - j + 1;
    if (i > a[j] && x % 2 == 0)
      i = a[j];
  }
  int x = query(id[i], dis[i] + 1, R, 1, n, 1);
  struct node temp;
  temp.L = L, temp.R = R;
  temp.l = i;
  temp.r = x;
  pri.push(temp);
}

int main() {
  scanf("%d", &n);
  build(1, n, 1);
  solve(1, n);
  while (!pri.empty()) {
    struct node temp = pri.top();
    pri.pop();
    answer.push_back({temp.l, temp.r});
    //        printf("L=%d R=%d l=%d r=%d\n", temp.L, temp.R, temp.l, temp.r);
    solve(temp.L, dis[temp.l] - 1);
    solve(dis[temp.l] + 1, dis[temp.r] - 1);
    solve(dis[temp.r] + 1, temp.R);
  }

  for (int i = 0; i < Size(answer); i++) {
    printf("%d %d%c", answer[i].first, answer[i].second,
           i == Size(answer) - 1 ? '\n' : ' ');
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define lson l, mid, i << 1
#define rson mid + 1, r, i << 1 | 1
#define PII pair<int, int>
#define Size(x) (int)(x.size())
using namespace std;
typedef long long LL;
// const int MOD = 1e9+7;
const auto INF = 0x3f3f3f3f;
const int N = 2e5 + 5;
struct node {
  int L, R;
  int l, r;
  bool operator<(const struct node &u) const {
    if (l == u.l)
      return r > u.r;
    return l > u.l;
  }
};

priority_queue<node> pri;
int dis[N], a[N];
int sum[2][N << 2], id[N];
vector<PII> answer;

void pustup(int i) {
  sum[0][i] = min(sum[0][i << 1], sum[0][i << 1 | 1]);
  sum[1][i] = min(sum[1][i << 1], sum[1][i << 1 | 1]);
}

void build(int l, int r, int i) {
  if (l == r) {
    int x;
    scanf("%d", &x);
    a[l] = x;
    dis[x] = l;
    sum[l & 1][i] = x;
    sum[!(l & 1)][i] = INF;
    id[x] = (l & 1);
    return;
  }
  int mid = (l + r) >> 1;
  build(lson);
  build(rson);
  pustup(i);
}

int query(int id, int L, int R, int l, int r, int i) {
  if (L <= l && r <= R) {
    return sum[!id][i];
  }
  int ans = INF, mid = (l + r) >> 1;
  if (L <= mid)
    ans = query(id, L, R, lson);
  if (R > mid)
    ans = min(ans, query(id, L, R, rson));
  return ans;
}

int n;

void solve(int L, int R) {
  if (L > R)
    return;
  int i = query(id[a[R]], L, R, 1, n, 1);
  int x = query(id[i], dis[i] + 1, R, 1, n, 1);
  struct node temp;
  temp.L = L, temp.R = R;
  temp.l = i;
  temp.r = x;
  pri.push(temp);
}

int main() {
  scanf("%d", &n);
  build(1, n, 1);
  solve(1, n);
  while (!pri.empty()) {
    struct node temp = pri.top();
    pri.pop();
    answer.push_back({temp.l, temp.r});
    //        printf("L=%d R=%d l=%d r=%d\n", temp.L, temp.R, temp.l, temp.r);
    solve(temp.L, dis[temp.l] - 1);
    solve(dis[temp.l] + 1, dis[temp.r] - 1);
    solve(dis[temp.r] + 1, temp.R);
  }

  for (int i = 0; i < Size(answer); i++) {
    printf("%d %d%c", answer[i].first, answer[i].second,
           i == Size(answer) - 1 ? '\n' : ' ');
  }
}