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

#include <bits/stdc++.h>
using namespace std;
template <class C> constexpr int sz(const C &c) { return int(c.size()); }
using ll = long long;
constexpr const char nl = '\\n', sp = ' ';

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // freopen("err.txt", "w", stderr);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  for (auto &&a : A)
    cin >> a;
  for (auto &&b : B)
    cin >> b;
  int mx = INT_MIN;
  for (int i = 0; i < N; i++)
    mx = max(mx, int(upper_bound(B.begin(), B.end(), A[i]) - B.begin() - i));
  if (mx < 0)
    mx += N;
  rotate(B.begin(), B.begin() + mx, B.end());
  for (int i = 0; i < N; i++)
    if (A[i] == B[i]) {
      cout << "No" << nl;
      return 0;
    }
  cout << "Yes" << nl;
  for (int i = 0; i < N; i++)
    cout << B[i] << " \\n"[i == N - 1];
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class C> constexpr int sz(const C &c) { return int(c.size()); }
using ll = long long;
constexpr const char nl = '\\n', sp = ' ';

template <class C> struct SegmentTreeBottomUp {
  using Data = typename C::Data;
  using Lazy = typename C::Lazy;
  int N;
  vector<Data> TR;
  template <class F>
  SegmentTreeBottomUp(int N, F f) : N(N), TR(N * 2, C::qdef()) {
    generate(TR.begin() + N, TR.end(), f);
    for (int i = N - 1; i > 0; i--)
      TR[i] = C::merge(TR[i * 2], TR[i * 2 + 1]);
  }
  template <class It>
  SegmentTreeBottomUp(It st, It en)
      : SegmentTreeBottomUp(en - st, [&] { return *st++; }) {}
  SegmentTreeBottomUp(int N, const Data &vdef)
      : SegmentTreeBottomUp(N, [&] { return vdef; }) {}
  void update(int i, const Lazy &v) {
    for (i += N, TR[i] = C::applyLazy(TR[i], v); i /= 2;)
      TR[i] = C::merge(TR[i * 2], TR[i * 2 + 1]);
  }
  Data query(int l, int r) {
    Data ql = C::qdef(), qr = C::qdef();
    for (l += N, r += N; l <= r; l /= 2, r /= 2) {
      if (l % 2)
        ql = C::merge(ql, TR[l++]);
      if (!(r % 2))
        qr = C::merge(TR[r--], qr);
    }
    return C::merge(ql, qr);
  }
};

struct C {
  using Data = pair<int, int>;
  using Lazy = int;
  static Data qdef() { return make_pair(0, INT_MAX); }
  static Data merge(const Data &l, const Data &r) { return max(l, r); }
  static Data applyLazy(const Data &l, const Lazy &r) {
    return make_pair(l.first + r, l.second);
  }
};

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // freopen("err.txt", "w", stderr);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  vector<int> A(N), B(N);
  vector<int> freq(N, 0);
  for (auto &&a : A) {
    cin >> a;
    freq[--a]++;
  }
  vector<pair<int, int>> D(N);
  for (int i = 0; i < N; i++)
    D[i] = make_pair(0, i);
  for (auto &&b : B) {
    cin >> b;
    D[--b].first++;
  }
  SegmentTreeBottomUp<C> ST(D.begin(), D.end());
  vector<int> P(N);
  iota(P.begin(), P.end(), 0);
  sort(P.begin(), P.end(),
       [&](int a, int b) { return freq[P[a]] > freq[P[b]]; });
  auto query = [&](int l, int r) {
    if (l > r)
      return make_pair(0, INT_MAX);
    else
      return ST.query(l, r);
  };
  for (int i : P) {
    int a = A[i];
    pair<int, int> q = C::merge(query(0, a - 1), query(a + 1, N - 1));
    if (q.first == 0) {
      cout << "No" << nl;
      return 0;
    }
    B[i] = q.second;
    ST.update(q.second, -1);
  }
  for (int i = 0; i < N; i++)
    assert(A[i] != B[i]);
  cout << "Yes" << nl;
  for (int i = 0; i < N; i++)
    cout << B[i] + 1 << " \\n"[i == N - 1];
  return 0;
}