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

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
using ll = long long;
using ld = long double;

#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(), (x).end()
#define uniq(x) ((x).erase(unique(all(x)), (x).end()))
#define sz(x) ((int)(x).size())

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)

#ifdef LOCAL // compile with -DLOCAL
#define debug(x)                                                               \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#define debug_vec(x)                                                           \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = ";                          \\
  rep(i, sz(x)) { cerr << x[i] << " "; }                                       \\
  cerr << endl
#define debug_mat(x)                                                           \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << endl;                  \\
  rep(i, sz(x)) {                                                              \\
    rep(j, sz(x[i])) { cerr << x[i][j] << " "; }                               \\
    cerr << endl;                                                              \\
  }                                                                            \\
  cerr << endl
#else
#define debug(x) void(0)
#define debug_vec(x) void(0)
#define debug_mat(x) void(0)
#endif

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A.at(i);
  rep(i, N) cin >> B.at(i);

  vector<int> cnt(N + 1);
  rep(i, N) {
    cnt.at(A.at(i))++;
    cnt.at(B.at(i))++;
  }
  rep(i, N + 1) {
    if (cnt.at(i) > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  vector<pair<int, pair<int, pair<int, int>>>> X(N + 1);
  rep(i, N + 1) X.at(i) = m_p(0, m_p(0, m_p(0, i)));
  rep(i, N) {
    X.at(A.at(i)).fi++;
    X.at(A.at(i)).se.fi++;
    X.at(B.at(i)).fi++;
    X.at(B.at(i)).se.se.fi++;
  }
  sort(all(X));
  reverse(all(X));

  vector<int> c, d;
  rep(i, N) {
    int tmp = X.at(i).se.se.se;
    rep(j, X.at(i).se.fi) c.e_b(tmp);
    rep(j, X.at(i).se.se.fi) d.e_b(tmp);
  }
  debug_vec(c);
  debug_vec(d);

  int r = 0;
  rep(i, N) {
    if (c.at(i) != d.at(i))
      continue;
    if (i == N - 1)
      break;
    chmax(r, i + 1);

    while (c.at(i) == d.at(r)) {
      r++;
      if (r >= N)
        break;
    }
    if (r >= N)
      break;
    swap(d.at(i), d.at(r));
  }
  int l = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (c.at(i) == d.at(i)) {
      swap(d.at(i), d.at(l));
      l++;
    }
  }

  vector<vector<int>> ans(N + 1);
  rep(i, N) ans.at(c.at(i)).e_b(d.at(i));
  rep(i, N + 1) { rep(j, sz(ans.at(i))) cout << ans.at(i).at(j) << " "; }
  cout << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
using ll = long long;
using ld = long double;

#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
#define e_b emplace_back
#define all(x) (x).begin(), (x).end()
#define uniq(x) ((x).erase(unique(all(x)), (x).end()))
#define sz(x) ((int)(x).size())

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)

#ifdef LOCAL // compile with -DLOCAL
#define debug(x)                                                               \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#define debug_vec(x)                                                           \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = ";                          \\
  rep(i, sz(x)) { cerr << x[i] << " "; }                                       \\
  cerr << endl
#define debug_mat(x)                                                           \\
  cerr << "LINE" << __LINE__ << " : " << #x << " = " << endl;                  \\
  rep(i, sz(x)) {                                                              \\
    rep(j, sz(x[i])) { cerr << x[i][j] << " "; }                               \\
    cerr << endl;                                                              \\
  }                                                                            \\
  cerr << endl
#else
#define debug(x) void(0)
#define debug_vec(x) void(0)
#define debug_mat(x) void(0)
#endif

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A.at(i);
  rep(i, N) cin >> B.at(i);

  vector<int> cnt(N + 1);
  rep(i, N) {
    cnt.at(A.at(i))++;
    cnt.at(B.at(i))++;
  }
  rep(i, N + 1) {
    if (cnt.at(i) > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  vector<pair<int, pair<int, pair<int, int>>>> X(N + 1);
  rep(i, N + 1) X.at(i) = m_p(0, m_p(0, m_p(0, i)));
  rep(i, N) {
    X.at(A.at(i)).fi++;
    X.at(A.at(i)).se.fi++;
    X.at(B.at(i)).fi++;
    X.at(B.at(i)).se.se.fi++;
  }
  sort(all(X));
  reverse(all(X));

  vector<int> c, d;
  rep(i, N) {
    int tmp = X.at(i).se.se.se;
    rep(j, X.at(i).se.fi) c.e_b(tmp);
    rep(j, X.at(i).se.se.fi) d.e_b(tmp);
  }
  debug_vec(c);
  debug_vec(d);

  int r = 0;
  rep(i, N) {
    if (c.at(i) != d.at(i))
      continue;
    if (i == N - 1)
      break;
    chmax(r, i + 1);

    while (c.at(i) == d.at(r))
      r++;
    swap(d.at(i), d.at(r));
  }
  if (c.at(N - 1) == d.at(N - 1)) {
    swap(d.at(0), d.at(N - 1));
  }

  vector<vector<int>> ans(N + 1);
  rep(i, N) ans.at(c.at(i)).e_b(d.at(i));
  rep(i, N + 1) { rep(j, sz(ans.at(i))) cout << ans.at(i).at(j) << " "; }
  cout << endl;

  return 0;
}