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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
struct init {
  init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} init_;

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll cnt(vl &V, ll a) { return upper_bound(all(V), a) - lower_bound(all(V), a); }

int main() {
  ll N;
  cin >> N;
  vl A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  bool isok = true;
  rep(i, N) {
    ll acnt = cnt(A, B[i]), bcnt = cnt(B, B[i]);
    if (acnt + bcnt > N) {
      isok = false;
      break;
    }
  }

  if (isok) {
    vl ans(N);

    using P = pair<ll, ll>;
    priority_queue<P> pq;
    ll nx = 0;
    rep(i, N) {
      if (i < nx)
        continue;
      pq.emplace(cnt(B, B[i]) + cnt(A, B[i]), B[i]);
      nx = upper_bound(all(B), B[i]) - B.begin();
    }

    map<ll, ll> mp, dd;
    rep(i, N) mp[A[i]]++;

    rep(i, N) {
      auto [c1, v1] = pq.top();
      pq.pop();
      if (A[i] != v1) {
        ans[i] = v1, c1--;
      } else {
        auto [c2, v2] = pq.top();
        pq.pop();
        ans[i] = v2, c2--;
        if (dd[v2] > 0)
          mp[v2] -= dd[v2], c2 -= dd[v2], dd[v2] = 0;
        if (c2 > mp[v2])
          pq.emplace(c2, v2);
      }
      if (dd[v1] > 0)
        mp[v1] -= dd[v1], c1 -= dd[v1], dd[v1] = 0;
      if (c1 > mp[v1])
        pq.emplace(c1, v1);

      dd[A[i]]++;
    }

    cout << "Yes" << '\\n';
    rep(i, N) cout << ans[i] << (i < N - 1 ? " " : "\\n");
  } else {
    cout << "No" << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
struct init {
  init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} init_;

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll cnt(vl &V, ll a) { return upper_bound(all(V), a) - lower_bound(all(V), a); }

int main() {
  ll N;
  cin >> N;
  vl A(N), B(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];

  bool isok = true;
  rep(i, N) {
    ll acnt = cnt(A, B[i]), bcnt = cnt(B, B[i]);
    if (acnt + bcnt > N) {
      isok = false;
      break;
    }
  }

  if (isok) {
    vl ans(N);

    using P = pair<ll, ll>;
    priority_queue<P, vc<P>, function<bool(P, P)>> pq([](P a, P b) {
      if (a.first == b.first)
        return a.second < b.second;
      else
        return a.first < b.first;
    });
    ll nx = 0;
    rep(i, N) {
      if (i < nx)
        continue;
      pq.emplace(cnt(B, B[i]) + cnt(A, B[i]), B[i]);
      nx = upper_bound(all(B), B[i]) - B.begin();
    }

    map<ll, ll> mp;
    rep(i, N) mp[A[i]]++;

    rep(i, N) {
      auto [c1, v1] = pq.top();
      pq.pop();
      if (A[i] != v1) {
        ans[i] = v1, c1--;
        if (c1 > mp[v1])
          pq.emplace(c1, v1);
      } else {
        auto [c2, v2] = pq.top();
        pq.pop();
        ans[i] = v2, c2--;
        if (c2 > mp[v2])
          pq.emplace(c2, v2);
        if (c1 > mp[v1])
          pq.emplace(c1, v1);
      }
    }

    cout << "Yes" << '\\n';
    rep(i, N) cout << ans[i] << (i < N - 1 ? " " : "\\n");
  } else {
    cout << "No" << '\\n';
  }
}