Problem:
Score : 600 points

Problem Statement

Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N.
Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N, A_i \leq B_i holds:

Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y.

Constraints

2 \leq N \leq 10^5

1 \leq A_i,B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

3
1 3 2
1 2 3

Sample Output 1

Yes

We should swap the values of A_2 and A_3.

Sample Input 2

3
1 2 3
2 2 2

Sample Output 2

No

Sample Input 3

6
3 1 2 6 3 4
2 2 8 3 4 3

Sample Output 3

Yes

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;

//Range Minimum Query
struct segment {
    int n;
    vector<int64_t> dat;
    // n_は範囲
    segment(int n_) { // nはn_以上の2^?
        n = 1<<(32-__builtin_clz(n_));
        if(n == 2*n_) n >>= 1;
        dat.assign(2*n-1, INT64_MAX);
    }
    //更新の手順
    int64_t theorem(int i) {
        int64_t chld = dat[i*2+1];
        int64_t chrd = dat[i*2+2];
        return min(chld, chrd);
    }
    //初期化(初期状態はINT64_MAX)
    void init(vector<int64_t> &vec) {
        int num = vec.size();
        rep(i, num) {
            dat[i+n-1] = vec[i];
        }
        rep(i, n-1) {
            dat[n-2-i] = theorem(n-2-i);
        }
    }
    // k番目の値をaに変更
    void update(int k, int64_t a) {
        // 葉の節点
        k += n-1;
        dat[k] = a;
        // 登りながら更新
        while(k>0) {
            k = (k-1)/2;
            dat[k] = theorem(k);
        }
    }
    // [a, b)の最小値
    int64_t query(int a, int b) {
        assert(a<b);
        return query_(a, b, 0, 0, n);
    }
    //kは節点の番号, [l, r)に節点が対応
    // (a, b, 0, 0, .n)で呼ぶ
    int64_t query_(int a, int b, int k, int l, int r) {
        if(a>=r || l>=b) return INT64_MAX;
        if(a<=l && r<=b) return dat[k];
        else {
            int chl = k*2+1;
            int chr = k*2+2;
            int64_t vl = query_(a, b, chl, l, (l+r)/2);
            int64_t vr = query_(a, b, chr, (l+r)/2, r);
            return min(vl, vr);
        }
    }
};

int main() {
    int n;
    cin >> n;
    vector<P> ba(n);
    rep(i, n) {
        int a;
        cin >> a;
        ba[i].second = a;
    }
    rep(i, n) {
        int b;
        cin >> b;
        ba[i].first = b;
    }
    sort(all(ba));

    map<int, vi> mp;
    rep(i, n) {
        mp[ba[i].second].push_back(i);
    }

    segment seg(n);
    vector<ll> aaa(n);
    rep(i, n) {
        aaa[i] = ba[i].second;
    }
    seg.init(aaa);

    /*rep(i, n) {
        cout << ba[i].first << ' ' << ba[i].second << endl;
    }*/

    int cou = 0;
    rep(i, n) {
        if(ba[i].second > ba[i].first) {
            cou++;
            auto itr = lower_bound(all(ba), P(ba[i].second, 0));
            if(itr == ba.end()) {
                cou = n;
                break;
            }
            int num = itr-ba.begin();
            int smm = seg.query(num, n);
            //cout << i << endl;
            if(smm <= ba[i].first) {
                seg.update(*(mp[smm].end()-1), ba[i].second);
                mp[ba[i].second].push_back(*(mp[smm].end()-1));
                mp[smm].pop_back();
            } else {
                smm = seg.query(i+1, n);
                if(smm > ba[i].first) {
                    cou = n;
                    break;
                } else {
                    seg.update(*(mp[smm].end()-1), ba[i].second);
                    mp[ba[i].second].push_back(*(mp[smm].end()-1));
                    mp[smm].pop_back();
                }
            }
        }
    }
    cout << (cou <= n-2 ? "Yes":"No") << endl;
}