using
System;
using
System.Collections.Generic;
class
Program
{
static
void
CheckPossibility(
int
n, List<
long
> A, List<
long
> B, List<
long
> C, List<
long
> D)
{
Dictionary<
long
, PriorityQueue<
long
>> hm =
new
Dictionary<
long
, PriorityQueue<
long
>>();
for
(
int
i = 0; i < n; i++)
{
if
(!hm.ContainsKey(D[i]))
{
hm[D[i]] =
new
PriorityQueue<
long
>();
}
hm[D[i]].Push(C[i]);
}
long
prev = -1;
for
(
int
i = 0; i < n; i++)
{
if
(!hm.ContainsKey(B[i]))
{
if
(A[i] < prev)
{
Console.WriteLine(
"NO"
);
return
;
}
prev = A[i];
}
else
{
PriorityQueue<
long
> pq = hm[B[i]];
while
(pq.Count > 0 && pq.Top() < prev)
{
pq.Pop();
}
if
(pq.Count == 0)
{
Console.WriteLine(
"NO"
);
return
;
}
prev = pq.Top();
pq.Pop();
}
}
Console.WriteLine(
"YES"
);
}
static
void
Main()
{
int
N = 2;
List<
long
> A =
new
List<
long
> { 3, 2 };
List<
long
> B =
new
List<
long
> { 1, 2 };
List<
long
> C =
new
List<
long
> { 1, 2 };
List<
long
> D =
new
List<
long
> { 1, 1 };
CheckPossibility(N, A, B, C, D);
}
}
public
class
PriorityQueue<T>
where
T : IComparable<T>
{
private
List<T> heap;
public
int
Count {
get
{
return
heap.Count; } }
public
PriorityQueue()
{
heap =
new
List<T>();
}
public
void
Push(T value)
{
heap.Add(value);
int
index = heap.Count - 1;
while
(index > 0)
{
int
parentIndex = (index - 1) / 2;
if
(heap[index].CompareTo(heap[parentIndex]) >= 0)
break
;
Swap(index, parentIndex);
index = parentIndex;
}
}
public
T Pop()
{
if
(heap.Count == 0)
throw
new
InvalidOperationException(
"PriorityQueue is empty"
);
T top = heap[0];
heap[0] = heap[heap.Count - 1];
heap.RemoveAt(heap.Count - 1);
int
index = 0;
while
(
true
)
{
int
leftChildIndex = 2 * index + 1;
int
rightChildIndex = 2 * index + 2;
if
(leftChildIndex >= heap.Count)
break
;
int
minIndex = leftChildIndex;
if
(rightChildIndex < heap.Count && heap[rightChildIndex].CompareTo(heap[leftChildIndex]) < 0)
minIndex = rightChildIndex;
if
(heap[index].CompareTo(heap[minIndex]) <= 0)
break
;
Swap(index, minIndex);
index = minIndex;
}
return
top;
}
public
T Top()
{
if
(heap.Count == 0)
throw
new
InvalidOperationException(
"PriorityQueue is empty"
);
return
heap[0];
}
private
void
Swap(
int
i,
int
j)
{
T temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
}