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# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using static Template;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pi = Pair<int, int>;
using Number = System.Int64;

class Solver
{
    public void Solve()
    {
        var num = Input.num;
        var ar = Input.ar;
        var bs = Input.ar;
        //var sm = new List<Pi>();//a>b
        var bg = new List<Pi>();//a<b
        var set = new CompLib.Collections.Set<Pi>((a, b) => a.v1.CompareTo(b.v1));
        set.IsMultiSet = true;
        for (var i = 0; i < num; i++)
            if (ar[i] > bs[i])
                set.Add(new Pi(ar[i], bs[i]));
            else bg.Add(new Pi(ar[i], bs[i]));
        //sm.Sort((a, b) => b.v1.CompareTo(a.v1));
        bg.Sort((a, b) => a.v1.CompareTo(b.v1));
        var seg = new SegmentTree<Pi>(bg.Count, new Pi(int.MinValue,-1), (a,b)=>a.v1>b.v1?a:b);
        for (var i = 0; i < bg.Count; i++)
            seg[i] = new Pi(bg[i].v2, i);
        seg.All_Update();
        for(var i = 0; i < num - 2; i++)
        {
            if (set.Count == 0) break;
            var p = set[set.Count - 1];
            var lw = UpperBound(bg, new Pi(p.v2, int.MinValue));
            var q = seg.Query(0, lw + 1);
            if(q.v1>=p.v1)
            {
                seg.Update(q.v2, new Pi(q.v1, p.v2));
                set.RemoveAt(set.Count - 1);
                continue;
            }
            q = seg.Query(lw, bg.Count);
            if (q.v2 == -1||p.v1>q.v1) Fail("No");
            set.RemoveAt(set.Count - 1);
            seg.Update(q.v2, new Pi(int.MinValue, -1));
            set.Add(new Pi(q.v1, p.v2));
        }
        WriteLine(set.Count == 0 ? "Yes" : "No");
    }
    #region UpperBound/LowerBound
    public static int UpperBound<T>(IList<T> array, T value)
        where T : IComparable<T>
    {
        var low = 0;
        var high = array.Count;
        while (high > low)
        {
            var mid = (high + low) / 2;
            if (array[mid].CompareTo(value) == 1) high = mid;
            else low = mid + 1;
        }
        return low;
    }

    public static int LowerBound<T>(IList<T> array, T value)
        where T : IComparable<T>
    {
        var low = 0;
        var high = array.Count;
        while (high > low)
        {
            var mid = (high + low) / 2;
            if (array[mid].CompareTo(value) != -1) high = mid;
            else low = mid + 1;
        }
        return low;
    }
    #endregion
}

public class SegmentTree<T>
{
    protected readonly T[] item;
    protected readonly int num;
    protected readonly Func<T, T, T> func;
    protected readonly Func<T, T, T> updateFunc;
    protected readonly T minT;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Parent(int index)
        => (index - 1) >> 1;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Left(int index)
        => (index << 1) + 1;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Right(int index)
        => (index + 1) << 1;
    public T this[int i]
    {
        get { return item[i + num - 1]; }
        set { item[i + num - 1] = value; }
    }

    public SegmentTree(int num, T minT, Func<T, T, T> func, Func<T, T, T> updateFunc = null)
    {
        this.func = func;
        this.num = 1;
        this.minT = minT;
        this.updateFunc = updateFunc ?? ((T val1, T val2) => val2);
        while (this.num <= num)
            this.num <<= 1;
        item = new T[2 * this.num - 1];
        for (var i = 0; i < 2 * this.num - 1; i++)
            item[i] = minT;
    }

    public void Update(int index, T value)
    {
        index += num - 1;
        item[index] = updateFunc(item[index], value);
        while (index > 0)
        {
            index = Parent(index);
            item[index] = func(item[Left(index)], item[Right(index)]);
        }
    }

    public virtual void Update(int left, int right, T value)
        => Update(left, right, 0, 0, num, value);
    protected virtual void Update(int left, int right, int k, int l, int r, T value)
    {
        if (r <= left || right <= l) return;
        if (left <= l && r <= right) item[k] = updateFunc(item[k], value);
        else
        {
            Update(left, right, Left(k), l, (l + r) >> 1, value);
            Update(left, right, Right(k), (l + r) >> 1, r, value);
        }
    }

    public void All_Update()
    {
        for (int i = num - 2; i >= 0; i--)
            item[i] = func(item[Left(i)], item[Right(i)]);
    }

    public T Query(int index)
    {
        index += num - 1;
        var value = func(minT, item[index]);
        while (index > 0)
        {
            index = Parent(index);
            value = func(value, item[index]);
        }
        return value;
    }

    public virtual T Query(int left, int right)
        => Query(left, right, 0, 0, num);
    protected virtual T Query(int left, int right, int k, int l, int r)
    {
        if (r <= left || right <= l) return minT;
        if (left <= l && r <= right) return item[k];
        else
            return func(Query(left, right, Left(k), l, (l + r) >> 1), Query(left, right, Right(k), (l + r) >> 1, r));
    }

    /// <summary>
    /// check(func(item[st]...item[i]))がtrueとなる最小のi
    /// </summary>
    public int Find(int st, Func<T, bool> check)
    {
        var x = minT;
        return Find(st, check, ref x, 0, 0, num);
    }
    private int Find(int st, Func<T, bool> check, ref T x, int k, int l, int r)
    {
        if (l + 1 == r)
        {
            x = func(x, item[k]);
            return check(x) ? k - num + 1 : -1;
        }
        var m = (l + r) >> 1;
        if (m <= st) return Find(st, check, ref x, Right(k), m, r);
        if (st <= l && !check(func(x, item[k])))
        { x = func(x, item[k]); return -1; }
        var xl = Find(st, check, ref x, Left(k), l, m);
        if (xl >= 0) return xl;
        return Find(st, check, ref x, Right(k), m, r);
    }
}
namespace CompLib.Collections
{
    #region Set
    public class Set<T>
    {
        Node root;
        readonly IComparer<T> comparer;
        readonly Node nil;
        public bool IsMultiSet { get; set; }
        public Set(IComparer<T> comparer)
        {
            nil = new Node(default(T));
            root = nil;
            this.comparer = comparer;
        }
        public Set(Comparison<T> comaprison) : this(Comparer<T>.Create(comaprison)) { }
        public Set() : this(Comparer<T>.Default) { }
        public bool Add(T v)
        {
            return insert(ref root, v);
        }
        public bool Remove(T v)
        {
            return remove(ref root, v);
        }
        public T this[int index] { get { return find(root, index); } }
        public int Count { get { return root.Count; } }
        public void RemoveAt(int k)
        {
            if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
            removeAt(ref root, k);
        }
        public T[] Items
        {
            get
            {
                var ret = new T[root.Count];
                var k = 0;
                walk(root, ret, ref k);
                return ret;
            }
        }
        void walk(Node t, T[] a, ref int k)
        {
            if (t.Count == 0) return;
            walk(t.lst, a, ref k);
            a[k++] = t.Key;
            walk(t.rst, a, ref k);
        }

        bool insert(ref Node t, T key)
        {
            if (t.Count == 0) { t = new Node(key); t.lst = t.rst = nil; t.Update(); return true; }
            var cmp = comparer.Compare(t.Key, key);
            bool res;
            if (cmp > 0)
                res = insert(ref t.lst, key);
            else if (cmp == 0)
            {
                if (IsMultiSet) res = insert(ref t.lst, key);
                else return false;
            }
            else res = insert(ref t.rst, key);
            balance(ref t);
            return res;
        }
        bool remove(ref Node t, T key)
        {
            if (t.Count == 0) return false;
            var cmp = comparer.Compare(key, t.Key);
            bool ret;
            if (cmp < 0) ret = remove(ref t.lst, key);
            else if (cmp > 0) ret = remove(ref t.rst, key);
            else
            {
                ret = true;
                var k = t.lst.Count;
                if (k == 0) { t = t.rst; return true; }
                if (t.rst.Count == 0) { t = t.lst; return true; }


                t.Key = find(t.lst, k - 1);
                removeAt(ref t.lst, k - 1);
            }
            balance(ref t);
            return ret;
        }
        void removeAt(ref Node t, int k)
        {
            var cnt = t.lst.Count;
            if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
            else if (cnt > k) removeAt(ref t.lst, k);
            else
            {
                if (cnt == 0) { t = t.rst; return; }
                if (t.rst.Count == 0) { t = t.lst; return; }

                t.Key = find(t.lst, k - 1);
                removeAt(ref t.lst, k - 1);
            }
            balance(ref t);
        }
        void balance(ref Node t)
        {
            var balance = t.lst.Height - t.rst.Height;
            if (balance == -2)
            {
                if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
                rotL(ref t);
            }
            else if (balance == 2)
            {
                if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
                rotR(ref t);
            }
            else t.Update();
        }

        T find(Node t, int k)
        {
            if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
            for (; ; )
            {
                if (k == t.lst.Count) return t.Key;
                else if (k < t.lst.Count) t = t.lst;
                else { k -= t.lst.Count + 1; t = t.rst; }
            }
        }
        public int LowerBound(T v)
        {
            var k = 0;
            var t = root;
            for (; ; )
            {
                if (t.Count == 0) return k;
                if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
                else { k += t.lst.Count + 1; t = t.rst; }
            }
        }
        public int UpperBound(T v)
        {
            var k = 0;
            var t = root;
            for (; ; )
            {
                if (t.Count == 0) return k;
                if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
                else t = t.lst;
            }
        }

        void rotR(ref Node t)
        {
            var l = t.lst;
            t.lst = l.rst;
            l.rst = t;
            t.Update();
            l.Update();
            t = l;
        }
        void rotL(ref Node t)
        {
            var r = t.rst;
            t.rst = r.lst;
            r.lst = t;
            t.Update();
            r.Update();
            t = r;
        }


        class Node
        {
            public Node(T key)
            {
                Key = key;
            }
            public int Count { get; private set; }
            public sbyte Height { get; private set; }
            public T Key { get; set; }
            public Node lst, rst;
            public void Update()
            {
                Count = 1 + lst.Count + rst.Count;
                Height = (sbyte)(1 + Math.Max(lst.Height, rst.Height));
            }
            public override string ToString()
            {
                return string.Format("Count = {0}, Key = {1}", Count, Key);
            }
        }
    }
    #endregion
}
#region Template
public class Template
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == 1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == -1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T v1, ref T v2)
    { var t = v2; v2 = v1; v1 = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<T> f)
        => Enumerable.Repeat(0, n).Select(_ => f()).ToArray();
    public static void Fail() => Fail("No");
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }
    static void Main(string[] args)
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        var p = new Solver();
        for (var i = 1; i > 0; --i)
            p.Solve();
        Console.Out.Flush();
    }
}

public class Input
{
    public static string read => Console.ReadLine().Trim();
    public static int[] ar => read.Split(' ').Select(int.Parse).ToArray();
    public static int num => Convert.ToInt32(read);
    public static long[] arL => read.Split(' ').Select(long.Parse).ToArray();
    public static long numL => Convert.ToInt64(read);
    public static char[][] grid(int h)
        => Create(h, () => read.ToCharArray());
    public static int[] ar1D(int n)
        => Create(n, () => num);
    public static long[] arL1D(int n)
        => Create(n, () => numL);
    public static string[] strs(int n)
        => Create(n, () => read);
    public static int[][] ar2D(int n)
        => Create(n, () => ar);
    public static long[][] arL2D(int n)
        => Create(n, () => arL);
    public static List<T>[] edge<T>(int n)
        => Create(n, () => new List<T>());
    public static void Make<T1, T2>(out T1 v1, out T2 v2)
    {
        v1 = Next<T1>();
        v2 = Next<T2>();
    }
    public static void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3)
    {
        Make(out v1, out v2);
        v3 = Next<T3>();
    }
    public static void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4)
    {
        Make(out v1, out v2, out v3);
        v4 = Next<T4>();
    }
    public static void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5)
    {
        Make(out v1, out v2, out v3, out v4);
        v5 = Next<T5>();
    }
    public static void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6)
    {
        Make(out v1, out v2, out v3, out v4, out v5);
        v6 = Next<T6>();
    }
    static Input()
    {
        sc = new Queue<string>();
        dic = new Dictionary<Type, Func<string, object>>();
        dic[typeof(int)] = s => int.Parse(s);
        dic[typeof(long)] = s => long.Parse(s);
        dic[typeof(char)] = s => char.Parse(s);
        dic[typeof(double)] = s => double.Parse(s);
        dic[typeof(uint)] = s => uint.Parse(s);
        dic[typeof(ulong)] = s => ulong.Parse(s);
        dic[typeof(string)] = s => s;
    }
    private static Dictionary<Type, Func<string, object>> dic;
    private static Queue<string> sc;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T Next<T>() { if (sc.Count == 0) foreach (var item in read.Split(' ')) sc.Enqueue(item); return (T)dic[typeof(T)](sc.Dequeue()); }
    public const int MOD = 1000000007;
}

public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1;
    public T2 v2;
    public Pair() { v1 = default(T1);v2 = default(T2); }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1; this.v2 = v2; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3;
    public Pair() : base() { v3 = default(T3); }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2)
    { this.v3 = v3; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public override string ToString()
        => $"{base.ToString()} {v3.ToString()}";
}
#endregion