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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\\n'
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                                                         \\
  do {                                                                         \\
    ostringstream os;                                                          \\
    os << setw(n) << setfill('0') << s;                                        \\
    s = os.str();                                                              \\
  } while (0);
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
#define INIT                                                                   \\
  std::cin.tie(nullptr);                                                       \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cout << std::fixed << std::setprecision(10);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T> using Pq = priority_queue<T>;
template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T> vector<T> make_vec(size_t a, T n) {
  return vector<T>(a, n);
}
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

signed main() {
  // INIT;
  cin >> N;
  vi a(N), b(N);
  cin >> a >> b;
  vi an(N + 1), bn(N + 1);
  REP(i, N) {
    an[a[i]]++;
    bn[b[i]]++;
  }
  Pq<pair<pi, int>> ab;
  queue<int> va;
  queue<int> vb;
  REP(i, N + 1) {
    if (an[i] + bn[i] > N) {
      cout << "No" << endl;
      return 0;
    }
    if (an[i] > 0 && bn[i] > 0)
      ab.push({{1, an[i] + bn[i]}, i});
    else if (an[i] > 0 || bn[i] > 0)
      ab.push({{0, an[i] + bn[i]}, i});
  }
  cout << "Yes" << endl;
  vector<pi> res;
  while (!ab.empty()) {
    auto now = ab.top();
    dump(now);
    if (now.first.first) {
      ab.pop();
      auto nxt = ab.top();
      while (an[now.second] > 0) {
        if (!nxt.first.first) {
          REP(i, N + 1) {
            if (an[now.second] == 0)
              break;
            while (an[now.second] > 0 && bn[i] > 0 && now.second != i) {
              bn[i]--;
              an[now.second]--;
              res.push_back({now.second, i});
            }
          }
          break;
        }
        if (bn[nxt.second] > 0) {
          an[now.second]--;
          bn[nxt.second]--;
          res.push_back({now.second, nxt.second});
        } else {
          ab.pop();
          ab.push({{0, an[nxt.second] + bn[nxt.second]}, nxt.second});
          nxt = ab.top();
        }
      }
      // ab.push({{0, an[nxt.second] + bn[nxt.second]}, nxt.second});
      ab.push({{0, an[now.second] + bn[now.second]}, now.second});
      dump(0, an[now.second] + bn[now.second], now.second);
    } else {
      dump(res);
      vi aa, bb;
      while (!ab.empty()) {
        now = ab.top();
        dump(now);
        REP(i, an[now.second]) { aa.push_back(now.second); }
        REP(i, bn[now.second]) { bb.push_back(now.second); }
        ab.pop();
      }
      dump(aa, bb);
      REP(i, aa.size()) { res.push_back({aa[i], bb[i]}); }
    }
  }
  sort(ALL(res));
  dump(res);
  REP(i, N) {
    if (res[i].first == res[i].second) {
      REP(j, N) { cout << res[j].first << " "; }
      cout << endl;
      REP(j, N) { cout << res[j].second << " "; }
      cout << endl;
      return 1;
    }
  }
  REP(i, N)
  cout << res[i].second << " ";
  cout << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\\n'
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                                                         \\
  do {                                                                         \\
    ostringstream os;                                                          \\
    os << setw(n) << setfill('0') << s;                                        \\
    s = os.str();                                                              \\
  } while (0);
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
#define INIT                                                                   \\
  std::cin.tie(nullptr);                                                       \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cout << std::fixed << std::setprecision(10);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T> using Pq = priority_queue<T>;
template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T> vector<T> make_vec(size_t a, T n) {
  return vector<T>(a, n);
}
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

signed main() {
  // INIT;
  cin >> N;
  vi a(N), b(N);
  cin >> a >> b;
  vi an(N + 1), bn(N + 1);
  REP(i, N) {
    an[a[i]]++;
    bn[b[i]]++;
  }
  Pq<pair<int, int>> apq, bpq;
  REP(i, N + 1) {
    if (an[i] + bn[i] > N) {
      cout << "No" << endl;
      return 0;
    }
    if (an[i] > 0)
      apq.push({an[i], i});
    if (bn[i] > 0)
      bpq.push({bn[i], i});
  }
  cout << "Yes" << endl;
  vector<pi> res;
  while (!apq.empty()) {
    auto atop = apq.top();
    auto btop = bpq.top();
    bool flag = (atop.second == btop.second);
    auto tmp = btop;
    if (flag) {
      bpq.pop();
      btop = bpq.top();
    }
    while (atop.first > 0) {
      atop.first--;
      btop.first--;
      res.push_back({atop.second, btop.second});
      if (btop.first == 0 && atop.first > 0) {
        bpq.pop();
        btop = bpq.top();
      }
    }
    if (flag) {
      bpq.push(tmp);
    }
    apq.pop();
  }
  sort(ALL(res));
  REP(i, N) { cout << res[i].second << " "; }
  cout << endl;
}