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

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define Clear(a) a = decltype(a)()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define vec vector
typedef long long ll;
typedef pair<ll, ll> P;
// const ll big=998244353;
const ll big = 1000000007LL;
const ll INF = 1e18;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  else
    return y;
}
ll expm(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (expm(x, y - 1) * x) % big;
  ll t = expm(x, y / 2);
  return (t * t) % big;
}
ll exp(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || y == 0)
    return x;
  if (y % 2 == 1)
    return exp(x, y - 1) * x;
  ll t = exp(x, y / 2);
  return t * t;
}
ll inv(ll x) { return expm(x, big - 2); }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  map<int, int> acnt, bcnt;
  rep(i, n) {
    cin >> a[i];
    acnt[a[i]]++;
  }
  rep(i, n) {
    cin >> b[i];
    bcnt[b[i]]++;
  }
  for (auto p : acnt) {
    if (bcnt[p.first] + p.second > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  reverse(all(b));
  int non = -1, nonidx = -1, nonlastidx = n - 1;
  rep(i, n) if (a[i] == b[i]) non = b[i];

  if (non == -1)
    goto skip;

  rep(i, n) {
    if (nonidx == -1 && non == b[i]) {
      nonidx = i;
    } else if (nonidx != -1 && b[i] != non) {
      nonlastidx = i - 1;
      break;
    }
  }
  {
    int idx = 0;
    rep2(i, nonidx, nonlastidx + 2) {
      if (a[i] != b[i])
        continue;
      if (nonidx == idx)
        idx = nonlastidx + 2;
      swap(b[idx], b[i]);
      idx++;
    }

    int j = n - 1;
    rep(i, n) {
      if (a[i] == b[i]) {
        swap(b[i], b[j]);
        j--;
      }
    }
  }
skip:;
  rep(i, n) cout << b[i] << " \\n"[i + 1 == n];
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define Clear(a) a = decltype(a)()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define vec vector
typedef long long ll;
typedef pair<ll, ll> P;
// const ll big=998244353;
const ll big = 1000000007LL;
const ll INF = 1e18;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll max(ll x, ll y) {
  if (x > y)
    return x;
  else
    return y;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  else
    return y;
}
ll expm(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || x == 0)
    return x;
  if (y % 2 == 1)
    return (expm(x, y - 1) * x) % big;
  ll t = expm(x, y / 2);
  return (t * t) % big;
}
ll exp(ll x, ll y) {
  if (y == 0)
    return 1; // 0^0=1
  if (x == 1 || y == 0)
    return x;
  if (y % 2 == 1)
    return exp(x, y - 1) * x;
  ll t = exp(x, y / 2);
  return t * t;
}
ll inv(ll x) { return expm(x, big - 2); }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(10);
  /*--------------------------------*/

  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  map<ll, int> cnt;
  rep(i, n) { cnt[a[i]]++; }
  map<ll, int> cnt2;
  rep(i, n) { cnt2[b[i]]++; }
  for (auto p : cnt) {
    if (p.second + cnt2[p.first] > n) {
      cout << "No" << endl;
      return 0;
    }
  }

  // あとは構成の仕方を考える
  /*
  bの中で数が多いやつから順に、aの中で多いところへ配置するのがよさそう...?
  */
  vector<ll> ans(n, -1);
  vector<P> bs, as;
  for (auto p : cnt)
    as.push_back({p.second, p.first});
  for (auto p : cnt)
    bs.push_back({p.second, p.first});
  sort(as.rbegin(), as.rend());
  sort(bs.rbegin(), bs.rend());

  vector<bool> already(n, false);
  int idx = 0;
  priority_queue<int> skipidx;
  rep(i, bs.size()) {
    ll nowcnt = bs[i].first, nowval = bs[i].second;
    if (skipidx.size()) {
      int lastidx = idx;
      idx = skipidx.top(), skipidx.pop();
      skipidx.push(lastidx);
    }
    while (nowcnt > 0) {
      // asの上から見ていく
      if (as[idx].second == nowval) {
        skipidx.push(idx);
        idx++;
        continue;
      }
      if (as[idx].first >= nowcnt) {
        as[idx].first -= nowcnt;
        // idx++はしない
        int k = distance(a.begin(),
                         lower_bound(a.begin(), a.end(), as[idx].second));
        rep2(j, k, k + nowcnt) {
          if (already[j]) {
            nowcnt++;
            continue;
          }
          ans[j] = nowval;
          already[j] = true;
        }
        nowcnt = 0;
      } else {
        nowcnt -= as[idx].first;
        int k = distance(a.begin(),
                         lower_bound(a.begin(), a.end(), as[idx].second));
        rep2(j, k, k + as[idx].first) {
          if (already[j]) {
            as[idx].first++;
            continue;
          }
          ans[j] = nowval;
          already[j] = true;
        }
        idx++;
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n) cout << ans[i] << " \\n"[i + 1 == n];
}