Problem p02557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i < (b); i++)
#define qi queue<int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
#define prq priority_queue
#define foreach(it, c)                                                         \
  for (__typeof((c).begin()) it = c.begin(); it != c.end(); it++)
// #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
inline ll read() {
  ll x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c - '0');
  return x * f;
}
inline void out(int x) {
  if (x >= 10)
    out(x / 10);
  putchar(x % 10 + '0');
}
int n, a[202020], b[202020];
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  cin >> n;
  fo(i, 0, n) cin >> a[i];
  fo(i, 0, n) cin >> b[i];
  if (n <= 200000) {
    fo(i, 0, n) {
      bool q = 1;
      fo(j, i, i + n) if (a[j - i] == b[j % n]) {
        q = 0;
        break;
      }
      if (q) {
        cout << "Yes\n";
        fo(j, i, i + n - 1) cout << b[j % n] << ' ';
        cout << b[(i + n - 1) % n] << endl;
        return 0;
      }
    }
    cout << "No\n";
    return 0;
  }
  fo(i, 0, n) if (a[0] != b[i]) {
    fo(j, i, i + n) {
      while (a[j - i] == b[j % n]) {
        i++;
        j++;
      }
      if (i >= n) {
        cout << "No\n";
        return 0;
      }
    }
    fo(j, i, i + n) if (a[j - i] == b[j % n]) {
      cout << "No\n";
      return 0;
    }
    cout << "Yes\n";
    fo(j, i, i + n - 1) cout << b[j % n] << ' ';
    cout << b[(i + n - 1) % n] << endl;
    return 0;
  }
  cout << "No\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i < (b); i++)
#define qi queue<int>
#define vi vector<int>
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
#define prq priority_queue
#define foreach(it, c)                                                         \
  for (__typeof((c).begin()) it = c.begin(); it != c.end(); it++)
// #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
inline ll read() {
  ll x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c - '0');
  return x * f;
}
inline void out(int x) {
  if (x >= 10)
    out(x / 10);
  putchar(x % 10 + '0');
}
int n, a[202020], b[202020];
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  cin >> n;
  fo(i, 0, n) cin >> a[i];
  fo(i, 0, n) cin >> b[i];
  if (n <= 190000) {
    fo(i, 0, n) {
      bool q = 1;
      fo(j, i, i + n) if (a[j - i] == b[j % n]) {
        q = 0;
        break;
      }
      if (q) {
        cout << "Yes\n";
        fo(j, i, i + n - 1) cout << b[j % n] << ' ';
        cout << b[(i + n - 1) % n] << endl;
        return 0;
      }
    }
    cout << "No\n";
    return 0;
  }
  fo(i, 0, n) if (a[0] != b[i]) {
    fo(j, i, i + n) {
      while (a[j - i] == b[j % n]) {
        i++;
        j++;
      }
      if (i >= n) {
        cout << "No\n";
        return 0;
      }
    }
    fo(j, i, i + n) if (a[j - i] == b[j % n]) {
      cout << "No\n";
      return 0;
    }
    cout << "Yes\n";
    fo(j, i, i + n - 1) cout << b[j % n] << ' ';
    cout << b[(i + n - 1) % n] << endl;
    return 0;
  }
  cout << "No\n";
  return 0;
}