Problem p02867 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pair<int, int>>
#define gph map<int, vector<int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define FORS(i, n) for (; i < (int)(n); i++)
#define debug(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define printArr(A, n)                                                         \
  {                                                                            \
    cout << #A << " = ";                                                       \
    FOR(i, n) cout << A[i] << ' ';                                             \
    cout << endl;                                                              \
  }
using namespace std;
#define int long long int
#define ll int
bool vis[(int)1e5 + 5] = {0};
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  freopen("in29.txt", "r", stdin);
  int n;
  cin >> n;
  vi a(n), b(n);
  vector<pair<int, int>> temp;
  FOR(i, n) cin >> a[i];
  FOR(i, n) cin >> b[i];
  FOR(i, n) temp.pb(mp(b[i], a[i]));
  sort(temp.begin(), temp.end());
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  FOR(i, n) {
    temp[i].first = temp[i].second;
    temp[i].second = i;
  }
  sort(temp.begin(), temp.end());

  FOR(i, n) {
    if (a[i] > b[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  FOR(i, n - 1) {
    if (a[i + 1] <= b[i]) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  int cnt = 0;
  int j = 0;
  while (!vis[j]) {
    vis[j] = true;
    cnt++;
    j = temp[j].second;
  }
  // debug(cnt);

  if (cnt < n)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pair<int, int>>
#define gph map<int, vector<int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define FORS(i, n) for (; i < (int)(n); i++)
#define debug(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define printArr(A, n)                                                         \
  {                                                                            \
    cout << #A << " = ";                                                       \
    FOR(i, n) cout << A[i] << ' ';                                             \
    cout << endl;                                                              \
  }
using namespace std;
#define int long long int
#define ll int
bool vis[(int)1e5 + 5] = {0};
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("in29.txt", "r", stdin);
  int n;
  cin >> n;
  vi a(n), b(n);
  vector<pair<int, int>> temp;
  FOR(i, n) cin >> a[i];
  FOR(i, n) cin >> b[i];
  FOR(i, n) temp.pb(mp(b[i], a[i]));
  sort(temp.begin(), temp.end());
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  FOR(i, n) {
    temp[i].first = temp[i].second;
    temp[i].second = i;
  }
  sort(temp.begin(), temp.end());

  FOR(i, n) {
    if (a[i] > b[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  FOR(i, n - 1) {
    if (a[i + 1] <= b[i]) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  int cnt = 0;
  int j = 0;
  while (!vis[j]) {
    vis[j] = true;
    cnt++;
    j = temp[j].second;
  }
  // debug(cnt);

  if (cnt < n)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}