Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const long long MOD = 1000000007LL;
long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); }
long long fast_exp(long long base, long long exp) {
  long long tot = 1;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = tot * base % MOD;
    base = base * base % MOD;
  }
  return tot;
}

int n;

int interval = 500;
pair<int, int> buckets[500][2];
int arr[200005];

void update(int value, int position) {

  arr[position] = value;

  int parity = position % 2;

  int left = position / interval * interval;
  int right = min(n, left + interval);

  buckets[left / interval][parity] = make_pair(1e9, 1e9);

  for (int pos = left; pos < right; ++pos) {
    if (pos % 2 == parity)
      buckets[pos / interval][parity] =
          min(buckets[pos / interval][parity], make_pair(arr[pos], pos));
  }
}

pair<int, int> query(int left, int right, int parity) {

  pair<int, int> best = make_pair(1e9, 1e9);

  for (; left % interval > 0 && left < right; ++left)
    if (left % 2 == parity)
      best = min(best, make_pair(arr[left], left));

  while (left + interval < right) {
    best = min(best, buckets[left / interval][parity]);
    left += interval;
  }

  for (; left < right; ++left)
    if (left % 2 == parity)
      best = min(best, make_pair(arr[left], left));

  return best;
}

void init() {
  for (int i = 0; i < n; ++i)
    update(arr[i], i);
}

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n;

  for (int i = 0; i < n; ++i)
    cin >> arr[i];

  init();

  priority_queue<vector<int>> pq;

  pair<int, int> ree1 = query(0, n, 0);
  pair<int, int> ree2 = query(ree1.second, n, 1);

  pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, 0, n});

  while (!pq.empty()) {

    vector<int> state = pq.top();
    pq.pop();
    int l = state[2], r = state[3], pl = state[4], pr = state[5];

    cout << -state[0] << " " << -state[1] << " ";

    // left side
    if (pl < l) {
      ree1 = query(pl, l, pl % 2);
      ree2 = query(ree1.second, l, 1 - pl % 2);
      pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, pl, l});
    }

    // center side
    if (l + 1 < r) {
      ree1 = query(l + 1, r, 1 - l % 2);
      ree2 = query(ree1.second, r, l % 2);
      pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, l + 1, r});
    }

    // right side
    if (r + 1 < pr) {
      ree1 = query(r + 1, pr, r % 2);
      ree2 = query(ree1.second, pr, 1 - r % 2);
      pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, r + 1, pr});
    }
  }

  return 0;
}


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

using namespace std;

const long long MOD = 1000000007LL;
long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); }
long long fast_exp(long long base, long long exp) {
  long long tot = 1;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = tot * base % MOD;
    base = base * base % MOD;
  }
  return tot;
}

int n;

int interval = 500;
pair<int, int> buckets[500][2];
int arr[200005];

void update(int value, int position) {

  arr[position] = value;

  int parity = position % 2;

  int left = position / interval * interval;
  int right = min(n, left + interval);

  buckets[left / interval][parity] = make_pair(1e9, 1e9);

  for (int pos = left; pos < right; ++pos) {
    if (pos % 2 == parity)
      buckets[pos / interval][parity] =
          min(buckets[pos / interval][parity], make_pair(arr[pos], pos));
  }
}

pair<int, int> query(int left, int right, int parity) {

  pair<int, int> best = make_pair(1e9, 1e9);

  for (; left % interval > 0 && left < right; ++left)
    if (left % 2 == parity)
      best = min(best, make_pair(arr[left], left));

  while (left + interval < right) {
    best = min(best, buckets[left / interval][parity]);
    left += interval;
  }

  for (; left < right; ++left)
    if (left % 2 == parity)
      best = min(best, make_pair(arr[left], left));

  return best;
}

void init() {
  for (int i = 0; i < n; ++i)
    update(arr[i], i);
}

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n;

  for (int i = 0; i < n; ++i)
    cin >> arr[i];

  init();

  priority_queue<vector<int>> pq;

  pair<int, int> ree1 = query(0, n, 0);
  pair<int, int> ree2 = query(ree1.second, n, 1);

  pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, 0, n});

  while (!pq.empty()) {

    vector<int> state = pq.top();
    pq.pop();
    int l = state[2], r = state[3], pl = state[4], pr = state[5];

    cout << -state[0] << " " << -state[1] << " ";

    // left side
    if (pl < l) {
      ree1 = query(pl, l, pl % 2);
      ree2 = query(ree1.second, l, 1 - pl % 2);
      pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, pl, l});
    }

    // center side
    if (l + 1 < r) {
      ree1 = query(l + 1, r, 1 - l % 2);
      ree2 = query(ree1.second, r, l % 2);
      pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, l + 1, r});
    }

    // right side
    if (r + 1 < pr) {
      ree1 = query(r + 1, pr, 1 - r % 2);
      ree2 = query(ree1.second, pr, r % 2);
      pq.push({-ree1.first, -ree2.first, ree1.second, ree2.second, r + 1, pr});
    }
  }

  return 0;
}