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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  map<ll, ll> ma, mb;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    ma[a[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    cin >> b[i];
    mb[b[i]]++;
  }
  reverse(b.begin(), b.end());
  ll st = -1, ed = -1;
  for (ll i = 0; i < n; i++) {
    if (a[i] == b[i]) {
      st = i;
      while (i < n && a[i] == b[i])
        i++;
      ed = i - 1;
    }
  }
  if (st == -1) {
    cout << "Yes\\n";
    for (ll i : b)
      cout << i << " ";
    return 0;
  }
  ll p = a[st];
  ll tot = ed - st + 1;
  ll k = n - (ma[p] + mb[p] - tot);
  // cout<<st<<" "<<ed<<" ";
  if (tot > k) {
    cout << "No";
    return 0;
  }
  ll j = st;
  for (ll i = 0; i < st; i++) {
    if (a[i] == p || b[i] == p)
      break;
    swap(b[i], b[j]);
    j++;
    if (j > ed)
      break;
  }
  for (ll i = ed + 1; i < n; i++) {
    if (j > ed)
      break;
    if (a[i] == p || b[i] == p)
      continue;
    swap(b[i], b[j]);
    j++;
  }
  cout << "Yes\\n";
  for (ll i : b)
    cout << i << " ";

  cerr << "\\n\\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\\n";
  T = clock();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  map<ll, ll> ma, mb;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    ma[a[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    cin >> b[i];
    mb[b[i]]++;
  }
  vector<pair<ll, ll>> bnum;
  for (auto p : ma) {
    ll u = p.second, v = mb[p.first];
    if (u + v > n) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (auto p : mb)
    bnum.pb({p.second, p.first});
  sort(bnum.begin(), bnum.end());
  ll j = 0, k = bnum.size();
  vector<ll> nxt(k), prv(k);
  for (ll i = 1; i < k; i++)
    prv[i] = i - 1;
  prv[0] = k - 1;
  nxt[k - 1] = 0;
  for (ll i = 0; i < k - 1; i++)
    nxt[i] = i + 1;
  vector<ll> ans(n, -1);
  for (ll i = 0; i < n; i++) {
    if (bnum[j].first == 0) {
      nxt[prv[j]] = nxt[j];
      prv[nxt[j]] = prv[j];
      j = nxt[j];
    }
    while (a[i] == bnum[j].second)
      j = nxt[j];
    assert(bnum[j].first != 0);
    // while(bnum[j].first==0)
    //     j=(j+1)%k;
    ans[i] = bnum[j].second;
    --bnum[j].first;
  }
  for (ll i = 0; i < n; i++)
    cout << ans[i] << " ";

  cerr << "\\n\\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\\n";
  T = clock();
  return 0;
}