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)

#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct BIT {
    int n;
    vector<T> dat;

    BIT(int n=0){
        initialize(n);
    }

    void initialize(int nin){
        n = nin;
        dat.resize(n, 0);
    }

    T sum(int i){
        T s = 0;
        while(i >= 0){
            s += dat[i];
            i = (i & (i+1)) - 1;
        }
        return s;
    }

    T sum_between(int i, int j){
        return sum(j) - sum(i-1);
    }

    void plus(int i, T x){
        while(i < n){
            dat[i] += x;
            i |= i+1;
        }
    }

    // a[0]+...+a[ret] >= x
    int lower_bound(T x){
        int ret = -1;
        int k = 1;
        while(2*k <= n) k <<= 1;
        for( ;k>0; k>>=1){
            if(ret+k < n && dat[ret+k] < x){
                x -= dat[ret+k];
                ret += k;
            }
        }
        return ret + 1;
    }
};

int main(){
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    for(int i=0; i<N; i++) cin >> A[i];
    for(int i=0; i<N; i++) cin >> B[i];

    vector<pair<int, int>> AS(N), BS(N);
    for(int i=0; i<N; i++){
        AS[i] = {A[i], i};
        BS[i] = {B[i], i};
    }
    sort(AS.begin(), AS.end());
    sort(BS.begin(), BS.end());
    vector<int> AR(N), BR(N);
    for(int i=0; i<N; i++){
        AR[AS[i].second] = i;
        BR[BS[i].second] = i;
    }

    // NGなところ
    BIT<int> bitl(N), bitc(N), bitr(N);
    for(int i=0; i<N; i++){
        if(i > 0 && AS[i].first > BS[i-1].first) bitl.plus(i, 1);
        if(AS[i].first > BS[i].first) bitc.plus(i, 1);
        if(i < N-1 && AS[i].first > BS[i+1].first) bitr.plus(i, 1);
    }

    for(int i=0; i<N; i++){
        if(A[i] > B[i]) continue;
        int ap = AR[i], bp = BR[i];
        int ng = 0;
        if(ap < bp){
            ng = bitc.sum_between(0, ap-1) + bitl.sum_between(ap+1, bp) + bitc.sum_between(bp+1, N-1);
        }else if(ap > bp){
            ng = bitc.sum_between(0, bp-1) + bitr.sum_between(bp, ap-1) + bitc.sum_between(ap+1, N-1);
        }else{
            ng = bitc.sum_between(0, ap-1) + bitc.sum_between(ap+1, N-1);
        }
        if(ng == 0){
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}