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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define oo 1000000000000000000
#define forr(i, n) for (ll i = 0; i < n; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define pb push_back
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

const ll MOD = 1e9 + 7;

ll valueOfIndex(ordered_set &s, ll i) { return *(s.find_by_order(i)); }
ll indexOfValue(ordered_set &s, ll x) { return s.order_of_key(x); }

ll add(ll a, ll b, ll p = MOD) {
  a %= p;
  b %= p;
  return (a + b + p) % p;
}
ll mul(ll a, ll b, ll p = MOD) {
  a %= p;
  b %= p;
  return (a * b + p) % p;
} // __int128

ll power(ll x, ll n, ll p = MOD) {
  if (x == 0)
    return 0;
  if (n == 0 || x == 1)
    return 1LL;
  ll r = (power(x, n / 2, p)) % p;
  if (n & 1)
    return mul(mul(r, r, p), x, p);
  else
    return mul(r, r, p);
}

void __sol() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v(n), p, q, r;
  forr(i, n) cin >> v[i].first;
  forr(i, n) cin >> v[n - i - 1].second;
  int i = 0, j = 0, k = 0;
  while (i < n && v[i].first != v[i].second)
    p.pb(v[i]), i++;
  while (i < n && v[i].first == v[i].second)
    q.pb(v[i]), i++;
  while (i < n && v[i].first != v[i].second)
    r.pb(v[i]), i++;
  i = 0;
  while (i < p.size() && j < q.size()) {
    if (p[i].first != q[j].second && p[i].second != q[j].first) {
      swap(p[i].second, q[j].second);
      i++, j++;
    } else
      i++;
  }
  while (k < r.size() && j < q.size()) {
    if (r[k].first != q[j].second && r[k].second != q[j].first) {
      swap(r[k].second, q[j].second);
      k++, j++;
    } else
      k++;
  }
  for (auto &i : p) {
    if (i.first == i.second) {
      cout << "No\\n";
      return;
    }
  }
  for (auto &i : q) {
    if (i.first == i.second) {
      cout << "No\\n";
      return;
    }
  }
  for (auto &i : r) {
    if (i.first == i.second) {
      cout << "No\\n";
      return;
    }
  }
  cout << "Yes\\n";

  for (auto &i : p)
    cout << i.second << " ";
  for (auto &i : q)
    cout << i.second << " ";
  for (auto &i : r)
    cout << i.second << " ";

  return;
}

int main() {
  fastio ll tc = 1; // cin >> tc;
  while (tc--)
    __sol();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define oo 1000000000000000000
#define forr(i, n) for (ll i = 0; i < n; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define eb emplace_back
#define pb push_back
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

using namespace __gnu_pbds;
using namespace std;

const ll MOD = 1e9 + 7;

ll valueOfIndex(ordered_set &s, ll i) { return *(s.find_by_order(i)); }
ll indexOfValue(ordered_set &s, ll x) { return s.order_of_key(x); }

ll add(ll a, ll b, ll p = MOD) {
  a %= p;
  b %= p;
  return (a + b + p) % p;
}
ll mul(ll a, ll b, ll p = MOD) {
  a %= p;
  b %= p;
  return (a * b + p) % p;
} // __int128

ll power(ll x, ll n, ll p = MOD) {
  if (x == 0)
    return 0;
  if (n == 0 || x == 1)
    return 1LL;
  ll r = (power(x, n / 2, p)) % p;
  if (n & 1)
    return mul(mul(r, r, p), x, p);
  else
    return mul(r, r, p);
}

void __sol() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v(n), p, q, r;
  forr(i, n) cin >> v[i].first;
  forr(i, n) cin >> v[n - i - 1].second;
  int i = 0, j = 0, k = 0;
  while (i < n && v[i].first != v[i].second)
    p.pb(v[i]), i++;
  while (i < n && v[i].first == v[i].second)
    q.pb(v[i]), i++;
  while (i < n && v[i].first != v[i].second)
    r.pb(v[i]), i++;
  i = p.size() - 1, k = q.size() - 1;

  while (i >= 0 && j < q.size()) {
    if (p[i].first != q[j].second) {
      swap(p[i].second, q[j].second);
      i--, j++;
    } else
      i--;
  }
  while (k >= 0 && j < q.size()) {
    if (r[k].first != q[j].second) {
      swap(r[k].second, q[j].second);
      k--, j++;
    } else
      k--;
  }
  for (auto &i : p) {
    if (i.first == i.second) {
      cout << "No\\n";
      return;
    }
  }
  for (auto &i : q) {
    if (i.first == i.second) {
      cout << "No\\n";
      return;
    }
  }
  for (auto &i : r) {
    if (i.first == i.second) {
      cout << "No\\n";
      return;
    }
  }
  cout << "Yes\\n";

  for (auto &i : p)
    cout << i.second << " ";
  for (auto &i : q)
    cout << i.second << " ";
  for (auto &i : r)
    cout << i.second << " ";
  cout << "\\n";

  return;
}

int main() {
  fastio ll tc = 1; // cin >> tc;
  while (tc--)
    __sol();
  return 0;
}