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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::collections::{BTreeMap, BTreeSet};

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner(s.lock());

    let n: usize = sc.read();
    let a: Vec<u64> = sc.vec(n);
    let b: Vec<u64> = sc.vec(n);
    if solve(a, b) {
        println!("Yes");
    } else {
        println!("No");
    }
}

fn solve(mut a: Vec<u64>, mut b: Vec<u64>) -> bool {
    let n = a.len();
    let mut ba = (0..n).map(|i| (b[i], a[i])).collect::<Vec<_>>();
    ba.sort();

    a.sort();
    b.sort();
    for i in 0..n {
        if a[i] > b[i] {
            return false;
        }
    }

    //    eprintln!("{:?}", a);
    //    eprintln!("{:?}", ba);

    let mut a_seg = BTreeMap::new();
    let mut na_seg = BTreeMap::new();
    let mut seg = vec![0; n];
    let mut tail = 0;
    for head in 0..n {
        if head > tail {
            na_seg.clear();
            tail = head;
        }
        while tail < n && b[head] >= a[tail] {
            let a = a[tail];
            let (_, na) = ba[tail];
            if a == na {
                return true;
            } else {
                push(&mut na_seg, na);
                push(&mut a_seg, a);
            }

            tail += 1;
        }
        seg[head] = tail;

        let a = a[head];
        let (_, na) = ba[head];

        if na_seg.contains_key(&a) {
            return true;
        }
        if a_seg.contains_key(&na) {
            return true;
        }

        remove(&mut na_seg, na);
        remove(&mut a_seg, a);
    }
    false
}

fn push(map: &mut BTreeMap<u64, usize>, v: u64) {
    *map.entry(v).or_insert(0) += 1;
}

fn remove(map: &mut BTreeMap<u64, usize>, v: u64) {
    let c = *map.get(&v).unwrap();
    if c == 1 {
        let removed = map.remove(&v).unwrap();
        assert_eq!(removed, 1);
    } else if c > 1 {
        map.insert(v, c - 1);
    } else {
        unreachable!()
    }
}

pub struct Scanner<R>(R);

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}