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

#include <bits/stdc++.h>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()

#define MOO(i, a, b) for (int i = a; i < b; i++)
#define M00(i, a) for (int i = 0; i < a; i++)
#define MOOd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define M00d(i, a) for (int i = (a)-1; i >= 0; i--)

#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define finish(x) return cout << x << '\\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\\n";
#define _ << " _ " <<

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pd;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ll> vll;

int main() {
  FAST mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  cin >> n;
  vi a(n), b(n);
  vi fa(n + 1), fb(n + 1);
  M00(i, n) cin >> a[i];
  M00(i, n) cin >> b[i];
  M00(i, n) {
    fa[a[i]]++;
    fb[b[i]]++;
  }
  vi PA(n + 1), PB(n + 1);
  M00(i, n) {
    if (i == 0)
      PA[1] = fa[1];
    else
      PA[i + 1] = PA[i] + fa[i + 1];
  }
  M00(i, n) {
    if (i == 0)
      PB[1] = fb[1];
    else
      PB[i + 1] = PB[i] + fb[i + 1];
  }

  M00(i, n + 1) {
    if (fa[i] + fb[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  // HOW
  // adj[i] = stores pairs of (num, freq) of B that map to A's i;
  vi shift(n + 1, 0);
  M00(i, n) {
    int lb = PB[i];
    int rb = PB[i + 1] - 1;
    int la = PA[i];
    int ra = PA[i + 1] - 1;
    if (rb < lb || ra < la)
      continue;

    pi ran = mp(la - rb + n, ra - lb + n);
    ran.f %= n;
    ran.s %= n;
    if (ran.f <= ran.s) {
      shift[ran.f]++;
      shift[ran.s + 1]--;
    } else {
      shift[ran.f]++;
      shift[n]--;
      shift[0]++;
      shift[ran.s + 1]--;
    }
  }
  int sum = 0;
  int ans;
  M00(i, n) {
    sum += shift[i];
    if (sum == 0) {
      ans = i;
    }
  }
  M00(i, n) { cout << b[(i - ans + n) % n] << " "; }
  cout << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()

#define MOO(i, a, b) for (int i = a; i < b; i++)
#define M00(i, a) for (int i = 0; i < a; i++)
#define MOOd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define M00d(i, a) for (int i = (a)-1; i >= 0; i--)

#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define finish(x) return cout << x << '\\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\\n";
#define _ << " _ " <<

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pd;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ll> vll;

int main() {
  FAST mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n;
  cin >> n;
  vi a(n), b(n);
  vi fa(n + 1), fb(n + 1);
  M00(i, n) cin >> a[i];
  M00(i, n) cin >> b[i];
  M00(i, n) {
    fa[a[i]]++;
    fb[b[i]]++;
  }
  M00(i, n + 1) {
    if (fa[i] + fb[i] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  // HOW
  // adj[i] = stores pairs of (num, freq) of B that map to A's i;
  vector<pi> B;
  M00(i, n + 1) {
    if (fb[i])
      B.pb(mp(fb[i], i));
  }
  sort(all(B));
  vector<vector<pi>> adj(n + 1);
  priority_queue<pi> Q;
  M00(i, n + 1) {
    if (fa[i])
      Q.push(mp(fa[i], i));
  }

  for (int i = B.size() - 1; i >= 0; i--) {
    pi BB = B[i];
    pi temp;
    bool used = false;
    while (BB.f > 0) {
      pi AA = Q.top();
      Q.pop();
      if (AA.s == BB.s) {
        used = true;
        temp = AA;
      } else {
        int delta = min(BB.f, AA.f);
        adj[AA.s].pb(mp(BB.s, delta));
        BB.f -= delta;
        AA.f -= delta;
        if (AA.f > 0)
          Q.push(AA);
      }
    }
    if (used)
      Q.push(temp);
  }
  vi index(n + 1, 0);
  M00(i, n) {
    while (adj[a[i]][index[a[i]]].s == 0)
      index[a[i]]++;
    cout << adj[a[i]][index[a[i]]].f << " ";
    adj[a[i]][index[a[i]]].s--;
  }
  cout << endl;
}