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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <tr1/unordered_map>
#include <vector>
#define mo 1000000007
#define num(x) (x >= '0' && x <= '9')
typedef unsigned long long ull;
typedef long long ll;
using namespace std::tr1;
using namespace std;
int read() {
  int p = 0, q = 1;
  char ch = getchar();
  while (!num(ch))
    (ch == '-' ? q = -1 : 0), ch = getchar();
  while (num(ch))
    p = p * 10 + ch - '0', ch = getchar();
  return p * q;
}
int power(ll a, int b) {
  ll c = 1;
  while (b) {
    if (b & 1)
      c = c * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return c;
}
#define N 200005
int n, a[N], b[N], f[N], g[N];
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), f[a[i]]++;
  for (int i = 1; i <= n; i++)
    b[i] = read(), g[b[i]]++;
  for (int i = 1; i <= n; i++)
    if (f[i] + g[i] > n) {
      puts("No");
      return 0;
    }
  puts("Yes");
  for (int i = 1; i <= n / 2; i++)
    swap(b[i], b[n - i + 1]);
  int x = 0, le = n + 1, ri = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] == b[i])
      x = a[i];
  for (int i = 1; i <= n; i++)
    if (a[i] == x || b[i] == x)
      le = min(le, i), ri = max(ri, i);
  for (int i = 1, j = 1; i <= n; i++)
    if (a[i] == b[i]) {
      if (le <= j && j <= ri)
        j = ri + 1;
      swap(b[j++], b[i]);
    }
  for (int i = 1; i <= n; i++)
    printf("%d%s", b[i], i == n ? "\\n" : " ");
  return 0;
}
/*
5
2 2 3 3 3
1 1 2 2 3
*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <tr1/unordered_map>
#include <vector>
#define mo 1000000007
#define num(x) (x >= '0' && x <= '9')
typedef unsigned long long ull;
typedef long long ll;
using namespace std::tr1;
using namespace std;
int read() {
  int p = 0, q = 1;
  char ch = getchar();
  while (!num(ch))
    (ch == '-' ? q = -1 : 0), ch = getchar();
  while (num(ch))
    p = p * 10 + ch - '0', ch = getchar();
  return p * q;
}
int power(ll a, int b) {
  ll c = 1;
  while (b) {
    if (b & 1)
      c = c * a % mo;
    a = a * a % mo;
    b >>= 1;
  }
  return c;
}
#define N 200005
int n, a[N], b[N], f[N], g[N], ans[N], flag[N];
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read(), f[a[i]]++;
  for (int i = 1; i <= n; i++)
    b[i] = read(), g[b[i]]++;
  for (int i = 1; i <= n; i++)
    if (f[i] + g[i] > n) {
      puts("No");
      return 0;
    }
  int last = 0;
  for (int i = 1, j = 1, k = 1; i <= n && k <= n; last = ++i) {
    if (!f[b[i]])
      continue;
    while (j <= n && (a[j] <= b[i] || !g[a[j]]))
      j++;
    if (j <= n)
      ans[j] = b[i], flag[j++] = 1;
    else {
      while (k <= n && (a[k] == b[i] || flag[k] || !g[a[k]]))
        k++;
      ans[k] = b[i], flag[k++] = 1;
    }
  }
  for (int i = 1, j = last; i <= n; i++, last = j)
    if (!g[a[i]]) {
      while (j <= n && !f[b[j]])
        j++;
      ans[i] = b[j];
    }
  if (last <= n) {
    puts("No");
    return 0;
  }
  puts("Yes");
  for (int i = 1, j = 1; i <= n; i++)
    if (!ans[i]) {
      while (f[b[j]] || !g[b[j]])
        j++;
      g[b[j]]--;
      ans[i] = b[j];
    }
  for (int i = 1; i <= n; i++)
    printf("%d%s", ans[i], i == n ? "\\n" : " ");
  return 0;
}
/*
5
2 2 3 3 3
1 1 2 2 3
*/