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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> a, b;

int main() {
  int n;
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (auto &x : a) {
    cin >> x;
  }
  for (auto &x : b) {
    cin >> x;
  }
  reverse(b.begin(), b.end());

  int l = 0, r = 0;
  while (l < n && a[l] != b[l]) {
    l++;
    r++;
  }
  while (r < n && a[r] == b[r]) {
    r++;
  }
  for (int i = 0; i < n && l != r; i++)
    if (a[i] != a[l] && b[i] != b[l])
      swap(b[l++], b[i]);

  if (l != r) {
    cout << "No" << '\\n';
    return 0;
  }
  cout << "Yes" << '\\n';
  for (auto &x : b)
    cout << x << ' ';
  cout << '\\n';
  cin.ignore(2);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

struct StVal {
  int max1 = -1, max2 = -1;
  int m1id = -1, m2id = -1;
  StVal() {}
  StVal(const int _v, const int i) : max1(_v), m1id(i) {}
  StVal(const StVal &v1, const StVal &v2) {
    if (v1.max1 >= v2.max1) {
      max1 = v1.max1;
      m1id = v1.m1id;
      if (v1.max2 > v2.max1) {
        max2 = v1.max2;
        m2id = v1.m2id;
      } else {
        max2 = v2.max1;
        m2id = v2.m1id;
      }
    } else {
      max1 = v2.max1;
      m1id = v2.m1id;
      if (v1.max1 >= v2.max2) {
        max2 = v1.max1;
        m2id = v1.m1id;
      } else {
        max2 = v2.max2;
        m2id = v2.m2id;
      }
    }
  }
};

struct SegTree {
  int n;
  vector<StVal> st;
  vector<int> leaves;

  SegTree(const int n, vector<int> &v) : n(n), leaves(n) {
    init(v, 1, 0, n - 1);
  }

  void init(vector<int> &v, const int si, const int lo, const int hi) {
    if (lo == hi) {
      if (si >= (int)st.size())
        st.resize(si + 1);
      st[si] = StVal(v[lo], lo);
      leaves[lo] = si;
    } else {
      const int mid = (lo + hi) >> 1;
      init(v, si << 1, lo, mid);
      init(v, si << 1 | 1, mid + 1, hi);
      st[si] = StVal(st[si << 1], st[si << 1 | 1]);
    }
  }

  StVal query() { return query(0, n - 1, 1, 0, n - 1); }
  StVal query(const int l, const int r, const int si, const int lo,
              const int hi) {
    if (l <= lo && hi <= r)
      return st[si];

    const int mid = (lo + hi) >> 1;
    if (r <= mid)
      return query(l, r, si << 1, lo, mid);
    if (mid < l)
      return query(l, r, si << 1 | 1, mid + 1, hi);
    return StVal(query(l, r, si << 1, lo, mid),
                 query(l, r, si << 1 | 1, mid + 1, hi));
  }

  void update(const int i) {
    int si = leaves[i];
    st[si] = StVal(st[si].max1 - 1, st[si].m1id);
    for (si >>= 1; si; si >>= 1)
      st[si] = StVal(st[si << 1], st[si << 1 | 1]);
  }
};

vector<int> a, b, freq, freqA, freqB;

int main() {
  int n;
  cin >> n;
  a.resize(n);
  b.resize(n);
  freq.resize(n + 1);
  freqA.resize(n + 1);
  freqB.resize(n + 1);
  for (auto &x : a) {
    cin >> x;
    freqA[x]++;
    freq[x]++;
  }
  for (auto &x : b) {
    cin >> x;
    freqB[x]++;
    freq[x]++;
  }
  if (*max_element(freq.begin(), freq.end()) > n) {
    cout << "No" << '\\n';
    return 0;
  }
  cout << "Yes" << '\\n';
  SegTree stA(n + 1, freqA), stB(n + 1, freqB);
  vector<int> idxA(n + 1, 1 << 30);
  for (int i = 0; i < n; i++) {
    idxA[a[i]] = min(idxA[a[i]], i);
  }
  // fill(b.begin(), b.end(), 0);
  for (int i = 0; i < n; i++) {
    StVal v1 = stA.query(), v2 = stB.query();
    if (v1.max1 > v2.max1) {
      int ma = v2.max1, id = v2.m1id;
      if (id == v1.m1id) {
        ma = v2.max2;
        id = v2.m2id;
      }
      b[idxA[v1.m1id]++] = id;
      stA.update(v1.m1id);
      stB.update(id);
    } else {
      int ma = v1.max1, id = v1.m1id;
      if (id == v2.m1id) {
        ma = v1.max2;
        id = v1.m2id;
      }
      b[idxA[id]++] = v2.m1id;
      stA.update(id);
      stB.update(v2.m1id);
    }
    // for (auto &x : b)cout << x << ' '; cout << '\\n';
  }
  for (auto &x : b) {
    cout << x << ' ';
  }
  cout << '\\n';
  cin.ignore(2);
  return 0;
}