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

#include "bits/stdc++.h"
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using ll = long long int;

const int mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

  int n;
  cin >> n;
  map<int, int> m;
  vector<int> a(n), b(n);
  for (auto &i : a) {
    cin >> i;
    ++m[i];
  }
  for (auto &i : b) {
    cin >> i;
    ++m[i];
  }
  reverse(begin(b), end(b));
  set<int> bad;
  set<int> vals;
  int intersect = -1;
  int l = 0, r = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i])
      continue;
    bad.insert(i);
    l = l == -1 ? i : l;
    r = i;
    intersect = a[i];
  }
  assert(r - l + 1 >= (int)bad.size());
  set<int> active;
  for (int i = 0; i < n; ++i) {
    if (a[i] == intersect || b[i] == intersect)
      continue;
    active.insert(i);
  }
  for (auto i : bad) {
    if (active.empty())
      return cout << "No\\n", 0;
    auto u = *active.begin();
    swap(b[i], b[u]);
    active.erase(u);
  }
  cout << "Yes\\n";
  for (auto x : b)
    cout << x << ' ';
  cout << '\\n';
}

// Suppose a[i] = b[i] and a[j] = b[j], i < j
// i < j -> a[i] <= a[j], b[i] >= b[j]
// a[i] + x = b[i] - y for some x, y >= 0
// x + y = 0
// x = y = 0

// Suppose intersection is with x
// c1x in a, c2x in b, c3x common
// c1x + c2x <= n
// n - c1x - c2x + c3x free spots
// Claim: c3x <= n - c1x - c2x + c3x
// 0 <= n-c1x-c2x
// True
// Why WA?????

Error: WA

Fixed submission:

#include "bits/stdc++.h"
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
using ll = long long int;

const int mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

  int n;
  cin >> n;
  map<int, int> m;
  vector<int> a(n), b(n);
  for (auto &i : a) {
    cin >> i;
    ++m[i];
  }
  for (auto &i : b) {
    cin >> i;
    ++m[i];
  }
  reverse(begin(b), end(b));
  set<int> bad;
  int intersect = -1;
  int l = 0, r = -1;
  for (int i = 0; i < n; ++i) {
    if (a[i] != b[i])
      continue;
    bad.insert(i);
    l = l == -1 ? i : l;
    r = i;
    intersect = a[i];
  }
  assert(r - l + 1 == (int)bad.size());
  set<int> active;
  for (int i = 0; i < n; ++i) {
    if (a[i] == intersect || b[i] == intersect)
      continue;
    active.insert(i);
  }
  for (auto i : bad) {
    if (active.empty())
      return cout << "No\\n", 0;
    auto u = *active.begin();
    swap(b[i], b[u]);
    active.erase(u);
  }
  cout << "Yes\\n";
  for (auto x : b)
    cout << x << ' ';
  cout << '\\n';
}

// Suppose a[i] = b[i] and a[j] = b[j], i < j
// i < j -> a[i] <= a[j], b[i] >= b[j]
// a[i] + x = b[i] - y for some x, y >= 0
// x + y = 0
// x = y = 0

// Suppose intersection is with x
// c1x in a, c2x in b, c3x common
// c1x + c2x <= n
// n - c1x - c2x + c3x free spots
// Claim: c3x <= n - c1x - c2x + c3x
// 0 <= n-c1x-c2x
// True
// Why WA?????