using
System;
using
System.Collections.Generic;
class
GFG {
public
static
void
Solve(
long
N,
long
M,
List<KeyValuePair<
long
,
long
> > problemRange,
List<
long
> arr)
{
var
mp =
new
SortedDictionary<
long
,
long
>();
for
(
long
i = 0; i < N; i++) {
long
a = problemRange[(
int
)i].Key;
long
b = problemRange[(
int
)i].Value;
mp[a] = b;
}
List<
long
> ans =
new
List<
long
>();
for
(
long
i = 0; i < M; i++) {
long
key = arr[(
int
)i];
KeyValuePair<
long
,
long
> pre, it;
bool
hasPre =
false
, hasIt =
false
;
pre =
new
KeyValuePair<
long
,
long
>(
long
.MinValue,
long
.MinValue);
it =
new
KeyValuePair<
long
,
long
>(
long
.MaxValue,
long
.MaxValue);
foreach
(KeyValuePair<
long
,
long
> kvp
in
mp)
{
if
(kvp.Key <= key && kvp.Key >= pre.Key) {
pre = kvp;
hasPre =
true
;
}
if
(kvp.Key >= key && kvp.Key <= it.Key) {
it = kvp;
hasIt =
true
;
}
}
if
(hasPre && key >= pre.Key
&& key <= pre.Value) {
ans.Add(key);
long
st = pre.Key;
long
end = pre.Value;
mp.Remove(pre.Key);
long
left = key - 1;
long
right = key + 1;
if
(st <= left) {
mp[st] = left;
}
if
(end >= right) {
mp[right] = end;
}
}
else
{
long
op1
= hasPre ? pre.Value :
long
.MaxValue;
long
op2 = hasIt ? it.Key :
long
.MaxValue;
if
(Math.Abs(key - op1)
<= Math.Abs(key - op2)) {
ans.Add(op1);
long
st = pre.Key;
long
end = op1 - 1;
mp.Remove(pre.Key);
if
(st <= end) {
mp[st] = end;
}
}
else
{
ans.Add(op2);
long
st = it.Key + 1;
long
end = it.Value;
mp.Remove(it.Key);
if
(st <= end) {
mp[st] = end;
}
}
}
}
foreach
(
long
it
in
ans) { Console.Write(it +
" "
); }
Console.WriteLine();
}
public
static
void
Main(
string
[] args)
{
long
N = 5;
long
M = 4;
List<
long
> arr =
new
List<
long
>{ 14, 24, 24, 4 };
List<KeyValuePair<
long
,
long
> > problemRange
=
new
List<KeyValuePair<
long
,
long
> >();
problemRange.Add(
new
KeyValuePair<
long
,
long
>(1, 2));
problemRange.Add(
new
KeyValuePair<
long
,
long
>(6, 7));
problemRange.Add(
new
KeyValuePair<
long
,
long
>(9, 12));
problemRange.Add(
new
KeyValuePair<
long
,
long
>(24, 24));
problemRange.Add(
new
KeyValuePair<
long
,
long
>(41, 50));
Solve(N, M, problemRange, arr);
}
}