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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
using namespace std;

#define int long long

const int CN = 4e5 + 5;

int read() {
  int s = 0, ne = 1;
  char c = getchar();
  while (c < '0' || c > '9')
    ne = c == '-' ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9')
    s = (s << 1) + (s << 3) + c - '0', c = getchar();
  return s * ne;
}

struct Splay {
#define ls ch[o][0]
#define rs ch[o][1]
  int val[CN], fa[CN], ch[CN][2], cnt[CN], sz[CN], rt, tot;
  inline int get(int o) { return o == ch[fa[o]][1]; }
  inline void maintain(int o) { sz[o] = sz[ls] + sz[rs] + cnt[o]; }
  inline void make(int f, int v) {
    val[++tot] = v;
    cnt[tot] = sz[tot] = 1;
    fa[tot] = f;
    if (f)
      ch[f][v > val[f]] = tot;
    maintain(f);
  }
  void rotate(int o) {
    int p = fa[o], gp = fa[fa[o]], chk = get(o);
    ch[p][chk] = ch[o][chk ^ 1];
    fa[ch[o][chk ^ 1]] = p;
    ch[o][chk ^ 1] = p;
    fa[p] = o;
    fa[o] = gp;
    if (gp)
      ch[gp][p == ch[gp][1]] = o;
    maintain(p);
    maintain(o);
  }
  void splay(int o, int tgp = 0) {
    for (int f = fa[o]; f = fa[o], f != tgp; rotate(o))
      if (fa[f] != tgp)
        rotate(get(f) == get(o) ? f : o);
    if (tgp == 0)
      rt = o;
  }
  void ins(int o, int f, int v) {
    if (!o)
      return (void)(make(f, v), rt = tot);
    if (val[o] == v)
      return (void)(sz[o]++, cnt[o]++, rt = o);
    ins(v < val[o] ? ls : rs, o, v);
    maintain(o);
  }
  void ins(int v) {
    ins(rt, 0, v);
    splay(rt);
  }
  int rank(int o, int v) {
    return (v == val[o]
                ? (rt = o, sz[ls] + 1)
                : (v < val[o] ? rank(ls, v) : rank(rs, v) + sz[ls] + cnt[o]));
  }
  int rank(int v) {
    ins(v);
    int t = rank(rt, v);
    splay(rt);
    erase(v);
    return t - 1;
  }
  int select(int o, int v) {
    return (sz[ls] + 1 <= v && v <= sz[ls] + cnt[o]
            ? rt = o,
            val[o]
            : (v <= sz[ls] ? select(ls, v) : select(rs, v - sz[ls] - cnt[o])));
  }
  int select(int v) {
    int t = select(rt, v + 1);
    splay(rt);
    return t;
  }
  int succ(int v) {
    ins(v);
    int o = ch[rt][1];
    while (ls)
      o = ls;
    splay(o);
    int t = val[rt];
    erase(v);
    return t;
  }
  int pred(int v) {
    ins(v);
    int o = ch[rt][0];
    while (rs)
      o = rs;
    splay(o);
    int t = val[rt];
    erase(v);
    return t;
  }
  void erase(int L, int R) {
    int pre, suc;
    rank(rt, L);
    splay(rt);
    pre = ch[rt][0];
    while (ch[pre][1])
      pre = ch[pre][1];
    rank(rt, R);
    splay(rt);
    suc = ch[rt][1];
    while (ch[suc][0])
      suc = ch[suc][0];
    splay(pre);
    splay(suc, rt);
    int &tgt = ch[ch[rt][1]][0];
    if (cnt[tgt] > 1)
      cnt[tgt]--, sz[tgt]--;
    else
      tgt = 0;
    maintain(ch[rt][1]);
    maintain(rt);
  }
  void erase(int v) { erase(v, v); }
  Splay() {
    ins(LLONG_MAX);
    ins(LLONG_MIN);
  }
} t;

int n, a[CN], b[CN];
int cnt[CN];

set<int, greater<int>> S;

bool RECK() {
  for (int i = 1; i <= n; i++) {
    int now = a[i];
    int k = t.succ(now);
    if (k != LLONG_MAX)
      b[i] = k, t.erase(k);
    else {
      k = t.pred(now);
      if (k != LLONG_MIN)
        b[i] = k, t.erase(k);
      else
        return false;
    }
  }
  return true;
}

signed main() {
  // freopen("_in.in", "r", stdin);

  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    b[i] = read(), S.insert(b[i]), t.ins(b[i]), cnt[b[i]]++;

  bool flag = true;
  for (int i = 1; i <= n && flag; i++) {
    bool has = false;
    for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
      int val = *it;
      if (val ^ a[i]) {
        b[i] = val, has = true, cnt[val]--;
        if (!cnt[val])
          S.erase(it);
        break;
      }
    }
    flag &= has;
  }

  if (!flag && !RECK())
    puts("No");
  else {
    puts("Yes");
    for (int i = 1; i <= n; i++)
      printf("%lld ", b[i]);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
using namespace std;

#define int long long

const int CN = 2e5 + 5;

int read() {
  int s = 0, ne = 1;
  char c = getchar();
  while (c < '0' || c > '9')
    ne = c == '-' ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9')
    s = (s << 1) + (s << 3) + c - '0', c = getchar();
  return s * ne;
}

struct Splay {
#define ls ch[o][0]
#define rs ch[o][1]
  int val[CN], fa[CN], ch[CN][2], cnt[CN], sz[CN], rt, tot;
  inline int get(int o) { return o == ch[fa[o]][1]; }
  inline void maintain(int o) { sz[o] = sz[ls] + sz[rs] + cnt[o]; }
  inline void make(int f, int v) {
    val[++tot] = v;
    cnt[tot] = sz[tot] = 1;
    fa[tot] = f;
    if (f)
      ch[f][v > val[f]] = tot;
    maintain(f);
  }
  void rotate(int o) {
    int p = fa[o], gp = fa[fa[o]], chk = get(o);
    ch[p][chk] = ch[o][chk ^ 1];
    fa[ch[o][chk ^ 1]] = p;
    ch[o][chk ^ 1] = p;
    fa[p] = o;
    fa[o] = gp;
    if (gp)
      ch[gp][p == ch[gp][1]] = o;
    maintain(p);
    maintain(o);
  }
  void splay(int o, int tgp = 0) {
    for (int f = fa[o]; f = fa[o], f != tgp; rotate(o))
      if (fa[f] != tgp)
        rotate(get(f) == get(o) ? f : o);
    if (tgp == 0)
      rt = o;
  }
  void ins(int o, int f, int v) {
    if (!o)
      return (void)(make(f, v), rt = tot);
    if (val[o] == v)
      return (void)(sz[o]++, cnt[o]++, rt = o);
    ins(v < val[o] ? ls : rs, o, v);
    maintain(o);
  }
  void ins(int v) {
    ins(rt, 0, v);
    splay(rt);
  }
  int rank(int o, int v) {
    return (v == val[o]
                ? (rt = o, sz[ls] + 1)
                : (v < val[o] ? rank(ls, v) : rank(rs, v) + sz[ls] + cnt[o]));
  }
  int rank(int v) {
    ins(v);
    int t = rank(rt, v);
    splay(rt);
    erase(v);
    return t - 1;
  }
  int select(int o, int v) {
    return (sz[ls] + 1 <= v && v <= sz[ls] + cnt[o]
            ? rt = o,
            val[o]
            : (v <= sz[ls] ? select(ls, v) : select(rs, v - sz[ls] - cnt[o])));
  }
  int select(int v) {
    int t = select(rt, v + 1);
    splay(rt);
    return t;
  }
  int succ(int v) {
    ins(v);
    int o = ch[rt][1];
    while (ls)
      o = ls;
    splay(o);
    int t = val[rt];
    erase(v);
    return t;
  }
  int pred(int v) {
    ins(v);
    int o = ch[rt][0];
    while (rs)
      o = rs;
    splay(o);
    int t = val[rt];
    erase(v);
    return t;
  }
  void erase(int L, int R) {
    int pre, suc;
    rank(rt, L);
    splay(rt);
    pre = ch[rt][0];
    while (ch[pre][1])
      pre = ch[pre][1];
    rank(rt, R);
    splay(rt);
    suc = ch[rt][1];
    while (ch[suc][0])
      suc = ch[suc][0];
    splay(pre);
    splay(suc, rt);
    int &tgt = ch[ch[rt][1]][0];
    if (cnt[tgt] > 1)
      cnt[tgt]--, sz[tgt]--;
    else
      tgt = 0;
    maintain(ch[rt][1]);
    maintain(rt);
  }
  void erase(int v) { erase(v, v); }
  Splay() {
    ins(LLONG_MAX);
    ins(LLONG_MIN);
  }
} t;

int n, a[CN], b[CN];
int cnt[CN];

set<int, greater<int>> S;

bool RECK() {
  for (int i = 1; i <= n; i++) {
    int now = a[i];
    int k = t.succ(now);
    if (k != LLONG_MAX)
      b[i] = k, t.erase(k);
    else {
      k = t.pred(now);
      if (k != LLONG_MIN)
        b[i] = k, t.erase(k);
      else
        return false;
    }
  }
  return true;
}

signed main() {
  // freopen("_in.in", "r", stdin);

  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    b[i] = read(), S.insert(b[i]), t.ins(b[i]), cnt[b[i]]++;

  bool flag = true;
  for (int i = 1; i <= n && flag; i++) {
    bool has = false;
    for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
      int val = *it;
      if (val ^ a[i]) {
        b[i] = val, has = true, cnt[val]--;
        if (!cnt[val])
          S.erase(it);
        break;
      }
    }
    flag &= has;
  }

  if (!flag && !RECK())
    puts("No");
  else {
    puts("Yes");
    for (int i = 1; i <= n; i++)
      printf("%d ", b[i]);
  }
}