Problem p02809 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

template <size_t n, class T> struct heap {
  size_t sz;
  T e;
  T a[n + 1];
  bool v[n + 1];
  size_t hp[n + 1], idx[n + 1];
  explicit heap(T e = numeric_limits<T>::max())
      : sz{}, e(e), a(), v(), hp(), idx() {
    fill(v + 1, v + 1 + n, false);
    fill(a + 1, a + 1 + n, e);
    fill(hp + 1, hp + 1 + n, -1u);
    fill(idx + 1, idx + 1 + n, -1u);
  }
  void prioritize(size_t i, T x) {
    size_t s = idx[i];
    if (a[i] < x) {
      while ((s << 1u) <= sz) {
        if ((s << 1u) == sz) {
          if (x > a[hp[s << 1u]]) {
            hp[s] = hp[s << 1u];
            idx[hp[s]] = s;
            s <<= 1u;
          }
          break;
        }
        if (x <= min(a[hp[s << 1u]], a[hp[(s << 1u) ^ 1u]]))
          break;
        if ((s << 1u) < sz && a[hp[s << 1u]] > a[hp[(s << 1u) ^ 1u]]) {
          hp[s] = hp[(s << 1u) ^ 1u];
          idx[hp[s]] = s;
          s <<= 1u;
          s ^= 1u;
        } else {
          hp[s] = hp[s << 1u];
          idx[hp[s]] = s;
          s <<= 1u;
        }
      }
    } else {
      while (s > 1 && x < a[hp[s >> 1u]]) {
        hp[s] = hp[s >> 1u];
        idx[hp[s]] = s;
        s >>= 1u;
      }
    }
    a[i] = x;
    hp[s] = i;
    idx[i] = s;
  }
  void Push(size_t i, T x) {
    if (!v[i]) {
      v[i] = true;
      ++sz;
      hp[sz] = i;
      idx[i] = sz;
    }
    prioritize(i, x);
  }
  void push(size_t i, T x) { Push(i + 1, x); }
  void pop() {
    const size_t t = hp[1];
    if (sz == 1) {
      hp[1] = -1u;
      a[t] = e;
      idx[t] = -1u;
      --sz;
      return;
    }
    idx[t] = -1u;
    idx[hp[sz]] = 1;
    hp[1] = hp[sz];
    hp[sz] = -1u;
    T x = a[hp[1]];
    a[hp[1]] = a[t];
    a[t] = e;
    --sz;
    v[t] = false;
    prioritize(hp[1], x);
  }
  T pri(size_t i) { return a[i + 1]; }
  T top() { return hp[1] - 1; }
  void erase(size_t i) {
    prioritize(i, numeric_limits<T>::min());
    pop();
  }
  bool find(size_t i) { return v[i + 1]; }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  istream &in(cin);
  ostream &out(cout);
  int n;
  in >> n;
  if (n == 2)
    return cout << -1 << endl, 0;
  heap<100000, int> hp;
  vector<int> a(n), c(n);
  for (auto &i : a)
    in >> i, --c[--i];
  fr(i, n) hp.push(i, c[i]);
  vector<int> ans(n - 3);
  set<int> st;
  fr(i, n) st.insert(i);
  int ng = -1;
  fr(i, n - 3) {
    int j = hp.top();
    if (-hp.pri(j) == n - 1 - i) {
      ans[i] = j;
      hp.pop();
      hp.push(a[j], hp.pri(j) + 1);
      st.erase(j);
      ng = a[j];
    } else {
      auto it = st.begin();
      j = *it;
      ++it;
      int J = *it;
      if (j == ng)
        j = J;
      st.erase(j);
      hp.erase(j);
      ng = a[j];
      ans[i] = j;
      if (hp.find(a[j]))
        hp.push(a[j], hp.pri(a[j]) + 1);
    }
  }
  auto it = st.begin();
  vector<int> p(3), P = {0, 1, 2};
  fr(i, 3) p[i] = *(it++);
  do {
    if (p[P[0]] == ng)
      continue;
    if (a[p[P[0]]] == p[P[1]])
      continue;
    if (a[p[P[1]]] == p[P[2]])
      continue;
    fr(i, 3) ans.emplace_back(p[P[i]]);
    break;
  } while (next_permutation(P.begin(), P.end()));
  fr(i, n) cout << ans[i] + 1 << " ";
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fr(i, n) for (int i = 0; i < (n); ++i)
#define Fr(i, n) for (int i = 1; i <= (n); ++i)
#define ifr(i, n) for (int i = (n)-1; i >= 0; --i)
#define iFr(i, n) for (int i = (n); i > 0; --i)

template <size_t n, class T> struct heap {
  size_t sz;
  T e;
  T a[n + 1];
  bool v[n + 1];
  size_t hp[n + 1], idx[n + 1];
  explicit heap(T e = numeric_limits<T>::max())
      : sz{}, e(e), a(), v(), hp(), idx() {
    fill(v + 1, v + 1 + n, false);
    fill(a + 1, a + 1 + n, e);
    fill(hp + 1, hp + 1 + n, -1u);
    fill(idx + 1, idx + 1 + n, -1u);
  }
  void prioritize(size_t i, T x) {
    size_t s = idx[i];
    if (a[i] < x) {
      while ((s << 1u) <= sz) {
        if ((s << 1u) == sz) {
          if (x > a[hp[s << 1u]]) {
            hp[s] = hp[s << 1u];
            idx[hp[s]] = s;
            s <<= 1u;
          }
          break;
        }
        if (x <= min(a[hp[s << 1u]], a[hp[(s << 1u) ^ 1u]]))
          break;
        if ((s << 1u) < sz && a[hp[s << 1u]] > a[hp[(s << 1u) ^ 1u]]) {
          hp[s] = hp[(s << 1u) ^ 1u];
          idx[hp[s]] = s;
          s <<= 1u;
          s ^= 1u;
        } else {
          hp[s] = hp[s << 1u];
          idx[hp[s]] = s;
          s <<= 1u;
        }
      }
    } else {
      while (s > 1 && x < a[hp[s >> 1u]]) {
        hp[s] = hp[s >> 1u];
        idx[hp[s]] = s;
        s >>= 1u;
      }
    }
    a[i] = x;
    hp[s] = i;
    idx[i] = s;
  }
  void Push(size_t i, T x) {
    if (!v[i]) {
      v[i] = true;
      ++sz;
      hp[sz] = i;
      idx[i] = sz;
    }
    prioritize(i, x);
  }
  void push(size_t i, T x) { Push(i + 1, x); }
  void pop() {
    const size_t t = hp[1];
    if (sz == 1) {
      hp[1] = -1u;
      a[t] = e;
      idx[t] = -1u;
      --sz;
      return;
    }
    idx[t] = -1u;
    idx[hp[sz]] = 1;
    hp[1] = hp[sz];
    hp[sz] = -1u;
    T x = a[hp[1]];
    a[hp[1]] = a[t];
    a[t] = e;
    --sz;
    v[t] = false;
    prioritize(hp[1], x);
  }
  T pri(size_t i) { return a[i + 1]; }
  T top() { return hp[1] - 1; }
  void erase(size_t i) {
    if (!find(i))
      return;
    prioritize(i + 1, numeric_limits<T>::min());
    pop();
  }
  bool find(size_t i) { return v[i + 1]; }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  istream &in(cin);
  ostream &out(cout);
  int n;
  in >> n;
  if (n == 2)
    return cout << -1 << endl, 0;
  heap<100000, int> hp;
  vector<int> a(n), c(n);
  for (auto &i : a)
    in >> i, --c[--i];
  fr(i, n) hp.push(i, c[i]);
  vector<int> ans(n - 3);
  set<int> st;
  fr(i, n) st.insert(i);
  int ng = -1;
  fr(i, n - 3) {
    int j = hp.top();
    if (-hp.pri(j) == n - 1 - i) {
      ans[i] = j;
      hp.pop();
      hp.push(a[j], hp.pri(j) + 1);
      st.erase(j);
      ng = a[j];
    } else {
      auto it = st.begin();
      j = *it;
      ++it;
      int J = *it;
      if (j == ng)
        j = J;
      st.erase(j);
      hp.erase(j);
      ng = a[j];
      ans[i] = j;
      if (hp.find(a[j]))
        hp.push(a[j], hp.pri(a[j]) + 1);
    }
  }
  auto it = st.begin();
  vector<int> p(3), P = {0, 1, 2};
  fr(i, 3) p[i] = *(it++);
  do {
    if (p[P[0]] == ng)
      continue;
    if (a[p[P[0]]] == p[P[1]])
      continue;
    if (a[p[P[1]]] == p[P[2]])
      continue;
    fr(i, 3) ans.emplace_back(p[P[i]]);
    break;
  } while (next_permutation(P.begin(), P.end()));
  fr(i, n) cout << ans[i] + 1 << " ";
  cout << endl;
}