Problem p02867 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define cauto const auto &
#define ALL(v) begin(v), end(v)
#else
#define ALL(v) (v).begin(), (v).end()
#endif

using namespace std;

namespace {
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y) { return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y) { return x = max(x, y); }
template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RALL(v) (v).rbegin(), (v).rend()

#define MOD 1000000007LL
#define EPS 1e-8

struct union_find {
  typedef unsigned size_type;
  vector<int> par;
  size_type grs;
  explicit union_find(size_type n = 0) { init(n); }
  void init(size_type n) {
    par.assign(n, -1);
    grs = n;
  }
  size_type find(size_type x) {
    if (par[x] < 0) {
      return x;
    }
    return par[x] = find(par[x]);
  }
  size_type size(size_type x) { return -par[find(x)]; }
  size_type groups() { return grs; }
  bool unite(size_type x, size_type y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return false;
    }
    if (par[x] < par[y]) {
      swap(x, y);
    }
    par[y] += par[x];
    par[x] = y;
    --grs;
    return true;
  }
  bool same(size_type x, size_type y) { return find(x) == find(y); }
  bool is_root(size_type x) { return find(x) == x; }
};

bool check1(vint as, vint bs) {
  int n = as.size();
  sort(ALL(as));
  sort(ALL(bs));
  for (int i = 0; i < n; ++i) {
    if (as[i] > bs[i]) {
      return false;
    }
  }
  return true;
}

bool solve() {
  int n;
  scanf("%d", &n);
  vector<pii> ps(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ps[i].second);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ps[i].first);
  }
  sort(ALL(ps));
  vector<int> as(n), bs(n);
  for (int i = 0; i < n; ++i) {
    as[i] = ps[i].second;
    bs[i] = ps[i].first;
  }

  if (!check1(as, bs)) {
    return false;
  }

  vector<int> ls(n);
  for (int i = 0; i < n; ++i) {
    ls[i] = upper_bound(ALL(bs), as[i]) - bs.begin() - 1;
  }

  if (set<int>(ALL(ls)).size() != n) {
    return true;
  }
  union_find uf(n);
  for (int i = 0; i < n; ++i) {
    uf.unite(i, ls[i]);
  }
  if (uf.groups() > 1) {
    return true;
  }

  return false;
}

void mainmain() { puts(solve() ? "Yes" : "No"); }

} // namespace
int main() try {
  //	ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(4);
  mainmain();
} catch (...) {
  throw;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define cauto const auto &
#define ALL(v) begin(v), end(v)
#else
#define ALL(v) (v).begin(), (v).end()
#endif

using namespace std;

namespace {
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y) { return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y) { return x = max(x, y); }
template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RALL(v) (v).rbegin(), (v).rend()

#define MOD 1000000007LL
#define EPS 1e-8

struct union_find {
  typedef unsigned size_type;
  vector<int> par;
  size_type grs;
  explicit union_find(size_type n = 0) { init(n); }
  void init(size_type n) {
    par.assign(n, -1);
    grs = n;
  }
  size_type find(size_type x) {
    if (par[x] < 0) {
      return x;
    }
    return par[x] = find(par[x]);
  }
  size_type size(size_type x) { return -par[find(x)]; }
  size_type groups() { return grs; }
  bool unite(size_type x, size_type y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return false;
    }
    if (par[x] < par[y]) {
      swap(x, y);
    }
    par[y] += par[x];
    par[x] = y;
    --grs;
    return true;
  }
  bool same(size_type x, size_type y) { return find(x) == find(y); }
  bool is_root(size_type x) { return find(x) == x; }
};

bool check1(vint as, vint bs) {
  int n = as.size();
  sort(ALL(as));
  sort(ALL(bs));
  for (int i = 0; i < n; ++i) {
    if (as[i] > bs[i]) {
      return false;
    }
  }
  return true;
}

bool solve() {
  int n;
  scanf("%d", &n);
  vector<pii> ps(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ps[i].second);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ps[i].first);
  }
  sort(ALL(ps));
  vector<int> as(n), bs(n);
  for (int i = 0; i < n; ++i) {
    as[i] = ps[i].second;
    bs[i] = ps[i].first;
  }

  if (!check1(as, bs)) {
    return false;
  }

  vector<int> ls(n);
  for (int i = 0; i < n; ++i) {
    ls[i] = lower_bound(ALL(bs), as[i]) - bs.begin();
    if (ls[i] >= n) {
      return false;
    }
  }

  if (set<int>(ALL(ls)).size() != n) {
    return true;
  }
  union_find uf(n);
  for (int i = 0; i < n; ++i) {
    uf.unite(i, ls[i]);
  }
  if (uf.groups() > 1) {
    return true;
  }

  return false;
}

void mainmain() { puts(solve() ? "Yes" : "No"); }

} // namespace
int main() try {
  //	ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(4);
  mainmain();
} catch (...) {
  throw;
}