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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vl2;
const bool multiple_queries = false;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define For(i, a, b) for (ll i = (a); i < (b); ++i)
#define revrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define revFor(i, b, a) for (ll i = (b)-1; i >= (a); --i)
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(i) __builtin_popcountl((i))
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)

void print() { cout; }
void println() { cout << endl; }

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0)
    cout << " ";
  print(forward<Tail>(tail)...);
}

template <class Head, class... Tail> void println(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0)
    cout << " ";
  println(forward<Tail>(tail)...);
}

template <typename T> void read_vector(ll n, vector<T> &v, ll offset = 0) {
  v.resize(n);
  rep(i, n) {
    cin >> v[i];
    if (offset)
      v[i] += offset;
  }
}

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

const ll INF = 1LL << 60;
typedef pair<ll, ll> pl;

void solve() {
  ll n;
  cin >> n;
  vl as, bs, ans(n), cnt(n + 1, 0);
  set<pl> s, sa, sb;
  vector<queue<ll>> ra(n + 1), rb(n + 1);
  read_vector(n, as);
  read_vector(n, bs);
  rep(i, n)++ cnt[as[i]], ++cnt[bs[i]];
  rep(i, n + 1) if (cnt[i] > n) {
    println("No");
    return;
  }
  rep(i, n) ra[as[i]].push(i), rb[bs[i]].push(i);
  rep(i, n + 1) {
    if (ra[i].size())
      sa.insert({ra[i].size(), i});
    if (rb[i].size())
      sb.insert({rb[i].size(), i});
    if (cnt[i])
      s.insert({cnt[i], i});
  }

  auto update = [&](ll i1, ll i2) {
    s.erase({cnt[i1], i1}), s.erase({cnt[i2], i2});
    --cnt[i1], --cnt[i2];
    if (cnt[i1])
      s.insert({cnt[i1], i1});
    if (cnt[i2])
      s.insert({cnt[i2], i2});
    if (ra[i1].size() && rb[i2].size()) {
      sa.erase({ra[i1].size(), i1});
      sb.erase({rb[i2].size(), i2});
      ll id = ra[i1].front();
      ra[i1].pop();
      rb[i2].pop();
      if (ra[i1].size())
        sa.insert({ra[i1].size(), i1});
      if (rb[i2].size())
        sb.insert({rb[i2].size(), i2});
      ans[id] = i2;
    } else if (ra[i2].size() && rb[i1].size()) {
      sa.erase({ra[i2].size(), i2});
      sb.erase({rb[i1].size(), i1});
      ll id = ra[i2].front();
      ra[i2].pop();
      rb[i1].pop();
      if (ra[i2].size())
        sa.insert({ra[i2].size(), i2});
      if (rb[i1].size())
        sb.insert({rb[i1].size(), i1});
      ans[id] = i1;
    } else {
    }
  };

  revFor(res, n + 1, 1) {
    ll c1, c2, i1, i2, _;

    if ((*s.rbegin()).first == res) {
      tie(c1, i1) = *s.rbegin();
      tie(c2, i2) = *(++s.rbegin());
      update(i1, i2);
    } else {
      auto ita = sa.begin(), itb = sb.begin();
      if ((*ita).second == (*itb).second)
        ++itb;
      tie(_, i1) = *ita;
      tie(_, i2) = *itb;
      update(i1, i2);
    }
  }

  println("Yes");
  rep(i, n) print(ans[i], "");
  println("");
}

void test(ll num = 0, bool verbose = false) {
  rep(i, max(1LL, num)) {
    ll t = clock();
    if (verbose)
      cout << "\\n#####"
           << " " << i + 1 << " "
           << "#####"
           << " "
           << "\\n## Answer ##\\n";
    solve();
    if (verbose) {
      cout << "\\nElapsed Time:"
           << " " << 1000.0 * (clock() - t) / CLOCKS_PER_SEC << " "
           << "[ms]" << endl;
    }
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
#ifdef _LOCAL
  test(6, true);
#else
  ll t = 0;
  if (multiple_queries)
    cin >> t;
  test(t, false);
#endif
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vl2;
const bool multiple_queries = false;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define For(i, a, b) for (ll i = (a); i < (b); ++i)
#define revrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define revFor(i, b, a) for (ll i = (b)-1; i >= (a); --i)
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(i) __builtin_popcountl((i))
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)

void print() { cout; }
void println() { cout << endl; }

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0)
    cout << " ";
  print(forward<Tail>(tail)...);
}

template <class Head, class... Tail> void println(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0)
    cout << " ";
  println(forward<Tail>(tail)...);
}

template <typename T> void read_vector(ll n, vector<T> &v, ll offset = 0) {
  v.resize(n);
  rep(i, n) {
    cin >> v[i];
    if (offset)
      v[i] += offset;
  }
}

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

const ll INF = 1LL << 60;
typedef pair<ll, ll> pl;

void solve() {
  ll n;
  cin >> n;
  vl as, bs, ans(n), cnt(n + 1, 0);
  set<pl> s, sa, sb;
  vector<queue<ll>> ra(n + 1), rb(n + 1);
  read_vector(n, as);
  read_vector(n, bs);
  rep(i, n)++ cnt[as[i]], ++cnt[bs[i]];
  rep(i, n + 1) if (cnt[i] > n) {
    println("No");
    return;
  }
  rep(i, n) ra[as[i]].push(i), rb[bs[i]].push(i);
  rep(i, n + 1) {
    if (ra[i].size())
      sa.insert({ra[i].size(), i});
    if (rb[i].size())
      sb.insert({rb[i].size(), i});
    if (cnt[i])
      s.insert({cnt[i], i});
  }

  auto update = [&](ll c1, ll i1, ll c2, ll i2) {
    s.erase({c1, i1}), s.erase({c2, i2});
    --c1, --c2;
    if (c1)
      s.insert({c1, i1});
    if (c2)
      s.insert({c2, i2});
    if (ra[i1].size() && rb[i2].size()) {
      sa.erase({ra[i1].size(), i1});
      sb.erase({rb[i2].size(), i2});
      ll id = ra[i1].front();
      ra[i1].pop();
      rb[i2].pop();
      if (ra[i1].size())
        sa.insert({ra[i1].size(), i1});
      if (rb[i2].size())
        sb.insert({rb[i2].size(), i2});
      ans[id] = i2;
    } else {
      sa.erase({ra[i2].size(), i2});
      sb.erase({rb[i1].size(), i1});
      ll id = ra[i2].front();
      ra[i2].pop();
      rb[i1].pop();
      if (ra[i2].size())
        sa.insert({ra[i2].size(), i2});
      if (rb[i1].size())
        sb.insert({rb[i1].size(), i1});
      ans[id] = i1;
    }
  };

  revFor(res, n + 1, 1) {
    ll c1, c2, i1, i2;

    if ((*s.rbegin()).first == res) {
      tie(c1, i1) = *s.rbegin();
      tie(c2, i2) = *(++s.rbegin());
      update(c1, i1, c2, i2);
    } else {
      ll ca, cb;
      auto ita = sa.begin(), itb = sb.begin();
      if ((*ita).second == (*itb).second)
        ++itb;
      tie(ca, i1) = *ita;
      tie(cb, i2) = *itb;
      c1 = ca + rb[i2].size();
      c2 = cb + rb[i1].size();
      update(c1, i1, c2, i2);
    }
  }

  println("Yes");
  rep(i, n) print(ans[i], "");
  println("");
}

void test(ll num = 0, bool verbose = false) {
  rep(i, max(1LL, num)) {
    ll t = clock();
    if (verbose)
      cout << "\\n#####"
           << " " << i + 1 << " "
           << "#####"
           << " "
           << "\\n## Answer ##\\n";
    solve();
    if (verbose) {
      cout << "\\nElapsed Time:"
           << " " << 1000.0 * (clock() - t) / CLOCKS_PER_SEC << " "
           << "[ms]" << endl;
    }
  }
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
#ifdef _LOCAL
  test(6, true);
#else
  ll t = 0;
  if (multiple_queries)
    cin >> t;
  test(t, false);
#endif
  return 0;
}