Problem p03531 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define Impossible                                                             \\
  {                                                                            \\
    cout << "NO\\n";                                                            \\
    return 0;                                                                  \\
  }

using namespace std;

const int Nmax = 1 << 18;

int N, n, i, j, x;
int L[Nmax], R[Nmax], A[Nmax];
bool used[Nmax];
vector<int> open[Nmax];
priority_queue<pair<int, int>> heap;

int reversed(int x) {
  int i, y = 0;
  for (i = 0; i < n; ++i)
    y = (y << 1) | ((x & (1 << i)) > 0);
  return y;
}

int main() {
  //    freopen("input", "r", stdin);
  //    freopen("output", "w", stdout);
  cin.sync_with_stdio(false);

  cin >> n;
  N = (1 << n);

  for (i = 0; i < N; ++i)
    L[i] = 0, R[i] = N - 1;

  for (i = 0; i < N; ++i) {
    cin >> A[i];
    --A[i];
    if (A[i] == -1)
      continue;

    L[i] = R[i] = A[i];
    used[A[i]] = 1;
  }

  for (i = 0; i < N; ++i)
    for (j = 0; j < n; ++j)
      if (!(i & (1 << j))) {
        x = i ^ (1 << j);
        L[x] = max(L[x], L[i] + 1);
      }

  for (i = N - 1; i >= 0; --i)
    for (j = 0; j < n; ++j)
      if (i & (1 << j)) {
        x = i ^ (1 << j);
        R[x] = min(R[x], R[i] - 1);
      }

  for (i = 0; i < N; ++i) {
    if (L[i] > N - 1 || R[i] < 0 || L[i] > R[i])
      Impossible;
    open[R[i]].push_back(i);
  }

  for (i = N - 1; i >= 0; --i) {
    for (auto it : open[i])
      heap.push({L[it], it + (A[it] != -1 ? N : 0)});

    if (heap.empty())
      Impossible;

    if (heap.top().second < N) {
      A[heap.top().second] = i;
      if (L[heap.top().second] > i)
        Impossible;
    }

    heap.pop();
  }

  cout << "YES\\n";
  for (i = 0; i < N; ++i)
    cout << A[reversed(i)] + 1 << ' ';

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define Impossible                                                             \\
  {                                                                            \\
    cout << "NO\\n";                                                            \\
    return 0;                                                                  \\
  }

using namespace std;

const int Nmax = 1 << 18;

int N, n, i, j, x;
int L[Nmax], R[Nmax], A[Nmax];
bool used[Nmax];
vector<int> open[Nmax];
priority_queue<pair<int, int>> heap;

int reversed(int x) {
  int i, y = 0;
  for (i = 0; i < n; ++i)
    y = (y << 1) | ((x & (1 << i)) > 0);
  return y;
}

int main() {
  //    freopen("input", "r", stdin);
  //   freopen("output", "w", stdout);
  cin.sync_with_stdio(false);

  cin >> n;
  N = (1 << n);

  for (i = 0; i < N; ++i)
    L[i] = 0, R[i] = N - 1;

  for (i = 0; i < N; ++i) {
    cin >> A[i];
    --A[i];
    if (A[i] == -1)
      continue;

    L[i] = R[i] = A[i];
    used[A[i]] = 1;
  }

  for (i = 0; i < N; ++i)
    for (j = 0; j < n; ++j)
      if (!(i & (1 << j))) {
        x = i ^ (1 << j);
        L[x] = max(L[x], L[i] + 1);
      }

  for (i = N - 1; i >= 0; --i)
    for (j = 0; j < n; ++j)
      if (i & (1 << j)) {
        x = i ^ (1 << j);
        R[x] = min(R[x], R[i] - 1);
      }

  for (i = 0; i < N; ++i)
    open[R[i]].push_back(i);

  for (i = N - 1; i >= 0; --i) {
    for (auto it : open[i])
      heap.push({L[it], it + (A[it] != -1 ? N : 0)});

    if (heap.empty())
      Impossible;

    if (heap.top().second < N)
      A[heap.top().second] = i;
    heap.pop();
  }

  cout << "YES\\n";
  for (i = 0; i < N; ++i)
    cout << A[reversed(i)] + 1 << ' ';

  return 0;
}