Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).

Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.


-----Input-----

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.

The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.

Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.


-----Output-----

Print n integers — the final report, which will be passed to Blake by manager number m.


-----Examples-----
Input
3 1
1 2 3
2 2

Output
2 1 3 
Input
4 2
1 2 4 3
2 3
1 2

Output
2 4 1 3 


-----Note-----

In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.

In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.

Difficulty: interview

Starter code:

Solutions:
# You lost the game.
n, m = map(int, input().split())
A = list(map(int, input().split()))
M = [list(map(int, input().split())) for _ in range(m)]

j = m-1
x = 2
h = -1
B = [-1 for _ in range(n+1)]
while h < n and j >= 0:
    h = M[j][1]
    if h >= x:
        B[h] = j
        x = h+1
    j -= 1

O = [0 for _ in range(n)]

for i in range(n-1,x-2,-1):
    O[i] = A[i]
    del A[i]

n2 = len(A)
    
R = A[:]
R.sort()
      
d = 0
f = n2-1


c = 0
for i in range(n2-1,-1,-1):
    j = B[i+1]
    if j >= 0:
        c = M[j][0]
    if c == 1:
        O[i] = R[f]
        f -= 1
    else:
        O[i] = R[d]
        d += 1

    
for i in range(n):
    print(O[i],end=" ")
        
    
    


n, m = list(map(int,input().split()))
a = list(map(int,input().split()))
t = list()
b = list()

for i in range(m):
	x, y = list(map(int,input().split()))
	while len(t) > 0 and y >= t[-1][1]: 
		t.pop()
	t.append([x, y])

x, y = 0, t[0][1] - 1
t.append([0, 0])
b = sorted(a[: y + 1])

for i in range(1, len(t)):
		for j in range(t[i - 1][1], t[i][1], -1):
			if t[i - 1][0] == 1:
				a[j - 1] = b[y]
				y -= 1 
			else:
				a[j - 1] = b[x]
				x += 1

print(" ".join(list(str(i) for i in a)))


import sys

n, m = [int(x) for x in input().split()]
notes = [int(x) for x in input().split()]
managers = []
for line in sys.stdin:
    managers.append(tuple(int(x) for x in line.split()))

bearing_managers = []
M, T = 0, 0
for t, r in reversed(managers):
    if r > M:
        if t == T:
            bearing_managers[-1] = (t, r)
        else:
            T = t
            bearing_managers.append((t, r))
        M = r
bearing_managers.reverse()
t, r = bearing_managers[0]
bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0))
L, R = 0, r-1
sorted_part = list(sorted(notes[:r]))
for t1, r1 in bearing_managers:
    if t1 == 2:
        for i in range(r-1, r1-1, -1):
            notes[i] = sorted_part[R]
            R -= 1
    else:
        for i in range(r-1, r1-1, -1):
            notes[i] = sorted_part[L]
            L += 1
    r = r1

for x in notes:
    sys.stdout.write(str(x) + ' ')


read = lambda: list(map(int, input().split()))
n, m = read()
a = list(read())
t = []
for i in range(m):
    x, r = read()
    while len(t) and r >= t[-1][1]:
        t.pop()
    t.append((x, r))
x, r = 0, t[0][1] - 1
t.append((0, 0))
b = sorted(a[:r + 1])
for i in range(1, len(t)):
    for j in range(t[i - 1][1], t[i][1], -1):
        if t[i - 1][0] == 1:
            a[j - 1] = b[r]
            r -= 1
        else:
            a[j - 1] = b[x]
            x += 1
print(*a)


n, m = list(map(int,input().split()))
a = list(map(int,input().split()))
t = list()
b = list()

for i in range(m):
	x, y = list(map(int,input().split()))
	while len(t) > 0 and y >= t[-1][1]: 
		t.pop()
	t.append([x, y])

x, y = 0, t[0][1] - 1
t.append([0, 0])
b = sorted(a[: y + 1])

for i in range(1, len(t)):
		for j in range(t[i - 1][1], t[i][1], -1):
			if t[i - 1][0] == 1:
				a[j - 1] = b[y]
				y -= 1 
			else:
				a[j - 1] = b[x]
				x += 1

print(" ".join(list(str(i) for i in a)))


n, m = map(int,input().split())
a = list(map(int,input().split()))
t = list()
b = list()

for i in range(m):
	x, y = map(int,input().split())
	while len(t) > 0 and y >= t[-1][1]: 
		t.pop()
	t.append([x, y])

x, y = 0, t[0][1] - 1
t.append([0, 0])
b = sorted(a[: y + 1])

for i in range(1, len(t)):
		for j in range(t[i - 1][1], t[i][1], -1):
			if t[i - 1][0] == 1:
				a[j - 1] = b[y]
				y -= 1 
			else:
				a[j - 1] = b[x]
				x += 1

print (" ".join(list(str(i) for i in a)))

import sys
from collections import deque
# sys.stdin = open("ivo.in")

n, m = map(int, sys.stdin.readline().split())

a = list(map(int, sys.stdin.readline().split()))

b = []
mx = 0
for i in range(m):
    t = tuple(map(int, sys.stdin.readline().split()))
    mx = max([mx, t[1]])
    b.append(t)

real = []
so_far = 0
for i in range(m-1,-1,-1):
    if b[i][1] > so_far:
        real.append(b[i])
        so_far = b[i][1]

real.reverse()

answer = [0] * n
b= a[0:mx]
b.sort()
d = deque(b)

for i in range(mx, len(a)):
    answer[i] = a[i]

for i in range(len(real)):
    if i + 1 == len(real):
        for j in range(real[i][1]):
            if real[i][0] == 1:
                answer[j] = d.popleft()
            else:
                answer[j] = d.pop()
    else:
        for j in range(real[i][1] - 1, real[i + 1][1] - 1, -1):
            if real[i][0] == 1:
                answer[j] = d.pop()
            else:
                answer[j] = d.popleft()

print(" ".join(map(str, answer)))

def compress(ops):
    cops = []
    for r, dir in ops:
        while cops and cops[-1][0] <= r:
            cops.pop()
        if not cops or cops[-1][1] != dir:
            cops.append((r, dir))
    return cops


def transform(lst, ops):
    mr, mdir = ops[0]

    sections = [list(range(mr, len(lst)))]

    ost = 0
    oen = mr
    pr, pdir = ops[0]
    for r, dir in ops[1:]:
        k = pr-r
        if pdir:
            sections.append(reversed(list(range(ost, ost+k))))
            ost += k
        else:
            sections.append(list(range(oen-k, oen)))
            oen -= k
        pr, pdir = r, dir

    if pdir:
        sections.append(reversed(list(range(ost, oen))))
    else:
        sections.append(list(range(ost, oen)))

    olst = lst[:mr]
    olst.sort()
    olst.extend(lst[mr:])
    return [olst[i] for sec in reversed(sections) for i in sec]


def parse_op():
    d, r = input().split()
    return int(r), (d == '2')


def parse_input():
    n, m = list(map(int, input().split()))
    lst = list(map(int, input().split()))
    assert len(lst) == n
    ops = [parse_op() for _ in range(m)]
    return lst, ops


def __starting_point():
    lst, ops = parse_input()
    cops = compress(ops)
    tlst = transform(lst, cops)
    print(' '.join(map(str, tlst)))

__starting_point()

def main():
    from collections import deque
    n, m = list(map(int, input().split()))
    aa = list(map(int, input().split()))
    l = list(tuple(map(int, input().split())) for _ in range(m))
    tmp, r0, t0 = [(0, 0)], 0, 0
    for t, r in reversed(l):
        if r0 < r:
            r0 = r
            if t0 != t:
                t0 = t
                tmp.append((t, r))
            else:
                tmp[-1] = (t, r)
    t0, r0 = tmp.pop()
    q = deque(sorted(aa[:r0]))
    aa = aa[r0:]
    aa.reverse()
    ff = (None, q.pop, q.popleft)
    for t, r in reversed(tmp):
        f = ff[t0]
        for i in range(r0 - r):
            aa.append(f())
        t0, r0 = t, r
    print(' '.join(map(str, reversed(aa))))


def __starting_point():
    main()

__starting_point()

s = input()
num = [int(x) for x in s.split()]
n,m = num[0],num[1]
s = input()
a = [int(x) for x in s.split()]
t1 = [0 for x in range(m)]
r1 = [0 for x in range(m)]
for i in range(m):
    s = input()
    num = [int(x) for x in s.split()]
    t1[i],r1[i] = num[0],num[1]
    r1[i] = r1[i]-1
    if t1[i] is 2:
        t1[i] = -1

t = []
r = []
for i in range(m):
    while len(r)!=0 and r[-1] <= r1[i]:
        t.pop()
        r.pop()
    t.append(t1[i])
    r.append(r1[i])

#print(t)
#print(r)

m = len(t)
b = [0 for x in range(n)]
last = 0
for i in range(m-1,-1,-1):
    if r[i] > last:
        last = r[i]
        b[r[i]] = t[i]

#print(b)
r = []
r.extend(a)
for i in range(n-1,-1,-1):
    if b[i] is 1:
        c = sorted(a[:i+1])
        c.extend(a[i+1:])
        typ = b[i]
        r = []
        r.extend(c)

        le = 0
        ri = i-1
        tp = typ
        for j in range(i-1,-1,-1):
            if b[j] is not 0:
                tp = b[j]
            if tp==typ:
                r[j] = c[ri]
                ri = ri-1
            else:
                r[j] = c[le]
                le = le+1     
        break
    elif b[i] is -1:
        c = sorted(a[:i+1])
        c.reverse()
        c.extend(a[i+1:])
        typ = b[i]
        r = []
        r.extend(c)

        le = 0
        ri = i-1
        tp = typ
        #print(c)
        for j in range(i-1,-1,-1):
            if b[j] is not 0:
                tp = b[j]
            if tp==typ:
                r[j] = c[ri]
                ri = ri-1
            else:
         #       print(le)
         #       print(c[le])
                r[j] = c[le]
                le = le+1

          #  print(r)
        break

#print(c)
for j in range(len(r)):
    print(r[j], end = ' ')
print()


3

class StdReader:
	def read_int(self):
		return int(self.read_string())

	def read_ints(self, sep = None):
		return [int(i) for i in self.read_strings(sep)]

	def read_float(self):
		return float(self.read_string())

	def read_floats(self, sep = None):
		return [float(i) for i in self.read_strings(sep)]

	def read_string(self):
		return input()

	def read_strings(self, sep = None):
		return self.read_string().split(sep)

reader = StdReader()


def part_sort(a, i, j, reverse=False):
	a[i:j] = sorted(a[i:j], reverse=reverse)


def part_sorted(b, i, j, reverse=False):
	a = list(b)
	part_sort(a, i, j, reverse)
	return a


def main():
	n, m = reader.read_ints()
	a = reader.read_ints()

	ops = []
	for i in range(m):
		t, r = reader.read_ints()
		op = (t, r)

		while ops and op[1] >= ops[-1][1]:
			ops.pop()

		ops.append(op)

	# newops = []
	# prevt = None
	# for op in ops:
	# 	if prevt is None or prevt != op[0]:
	# 		newops.append(op)
	# 		# max_r = max(max_r, op[1])

	# 	prevt = op[0]

	# ops = newops

	max_r = ops[0][1]
	b = sorted(a[:max_r])
	bl = 0
	br = max_r - 1

	for i in range(len(ops)):
		t, r = ops[i]

		r1 = 0
		if i < len(ops) - 1:
			t1, r1 = ops[i+1]

		k = r - r1

		if t == 1:
			for p in range(k):
				a[r - 1 - p] = b[br]
				br -= 1
		else:
			for p in range(k):
				a[r - 1 - p] = b[bl]
				bl += 1

	# for op in ops:
	# 	t, r = op
	# 	reverse = t != 1
	# 	part_sort(a, 0, r, reverse=reverse)

	for ai in a:
		print(ai, end=' ')


def __starting_point():
	main()

__starting_point()

3

class StdReader:
	def read_int(self):
		return int(self.read_string())

	def read_ints(self, sep = None):
		return [int(i) for i in self.read_strings(sep)]

	def read_float(self):
		return float(self.read_string())

	def read_floats(self, sep = None):
		return [float(i) for i in self.read_strings(sep)]

	def read_string(self):
		return input()

	def read_strings(self, sep = None):
		return self.read_string().split(sep)

reader = StdReader()


def part_sort(a, i, j, reverse=False):
	a[i:j] = sorted(a[i:j], reverse=reverse)


def part_sorted(b, i, j, reverse=False):
	a = list(b)
	part_sort(a, i, j, reverse)
	return a


def main():
	n, m = reader.read_ints()
	a = reader.read_ints()

	ops = []
	for i in range(m):
		t, r = reader.read_ints()
		op = (t, r)

		while ops and op[1] >= ops[-1][1]:
			ops.pop()

		ops.append(op)

	newops = []
	prevt = None
	for op in ops:
		if prevt is None or prevt != op[0]:
			newops.append(op)
			# max_r = max(max_r, op[1])

		prevt = op[0]

	ops = newops

	max_r = ops[0][1]
	b = sorted(a[:max_r])
	bl = 0
	br = max_r - 1

	for i in range(len(ops)):
		t, r = ops[i]

		r1 = 0
		if i < len(ops) - 1:
			t1, r1 = ops[i+1]

		k = r - r1

		if t == 1:
			for p in range(k):
				a[r - 1 - p] = b[br]
				br -= 1
		else:
			for p in range(k):
				a[r - 1 - p] = b[bl]
				bl += 1

	# for op in ops:
	# 	t, r = op
	# 	reverse = t != 1
	# 	part_sort(a, 0, r, reverse=reverse)

	for ai in a:
		print(ai, end=' ')


def __starting_point():
	main()

__starting_point()

a,b=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
s=[]
for p in range(b):
    s+=[[int(i) for i in input().split()]]
S=[]
k=0
for p in range(b-1,-1,-1):
    if s[p][1]>k:
        S+=[s[p]]
        k=s[p][1]
Q1=sorted(q[:S[-1][1]])
x,y=0,S[-1][1]-1
S=[[0,0]]+S
l=1
for j in range(S[-1][1]-1,-1,-1):
    if x>y:break
    if j<S[-l][1]:
        k=S[-l][0]
        l+=1
    if k==1:
        q[j]=Q1[y]
        y-=1
    if k==2:
        q[j]=Q1[x]
        x+=1
print(' '.join(list(map(str,q))))


[n,m]=[int(i) for i in input().split()];
a=[int(i) for i in input().split()];
t=m*[0];
r=m*[0];
for k in range(0,m):
    [t[k],r[k]]=[int(i) for i in input().split()];

tt=[];
rr=[];
mm=-1;
lastt=-1;
tt.append(t[0]);
rr.append(r[0]);
top=0;
for i in range(1,m):
    while(top>-1):
        if(r[i]>=rr[top]):
            rr.pop();
            tt.pop();
            top=top-1;
        else:
            break;
    if(top==-1):
        rr.append(r[i]);
        tt.append(t[i]);
        top=top+1;
    else:
        if(tt[top]!=t[i]):
            rr.append(r[i]);
            tt.append(t[i]);
            top=top+1;

r0=rr[0];
sorteda=sorted(a[:r0]);
b=0;
end=len(sorteda)-1;

for mindex in range(0,len(tt)-1):
    rthis=rr[mindex];
    rnext=rr[mindex+1];
    if(tt[mindex]==1):
        for i in range(rthis-1,rnext-1,-1):
            a[i]=sorteda[end];
            end=end-1;
    else:
        for i in range(rthis-1,rnext-1,-1):
            a[i]=sorteda[b];
            b=b+1;

if(tt[-1]==1):
    for i in range(0,rr[-1]):
        a[i]=sorteda[i+b];
else:
    for i in range(0,rr[-1]):
        a[i]=sorteda[end-i];

print(*a, sep=" ")




a, b = (int(i) for i in input().split())
f = list(map(int,input().split()))
d = []
for i in range(b):
    c, e = (int(t) for t in input().split())
    while len(d) > 0 and e > d[-1][1]:
        d.pop()
    d.append([c, e])
c, e = 0, d[0][1]
d.append([0, 0])
g = sorted(f[:e])
for i in range(1,len(d)):
    for y in range(d[i - 1][1], d[i][1], -1):
        if d[i - 1][0] == 1:
            f[y - 1] = g[e - 1]
            e -= 1
        else:
            f[y - 1] = g[c]
            c += 1
for i in range(len(f)):
    print(f[i],end = ' ')


a, b = (int(i) for i in input().split())
f = list(map(int,input().split()))
d = []
for i in range(b):
    c, e = (int(t) for t in input().split())
    while len(d) > 0 and e > d[-1][1]:
        d.pop()
    d.append([c, e])
c, e = 0, d[0][1]
d.append([0, 0])
g = sorted(f[:e])
for i in range(1,len(d)):
    for y in range(d[i - 1][1], d[i][1], -1):
        if d[i - 1][0] == 1:
            f[y - 1] = g[e - 1]
            e -= 1
        else:
            f[y - 1] = g[c]
            c += 1
print(' '.join(list(str(i) for i in f)))


n,m = [int(i) for i in input().split()]
nl = [int(i) for i in input().split()]
ml = []
for i in range(m):
    t,r = [int(k) for k in input().split()]
    ml.append((t,r))

mll = [(0,0)]
for i in range(m-1,-1,-1):
    if i == m-1:
        mll.append(ml[i])
    elif i != m-1 and ml[i][1] > mll[-1][1]:
        mll.append(ml[i])
    #print(mll)


temp = sorted(nl[:mll[len(mll)-1][1]])
x,y = 0,len(temp)-1
for i in range(len(mll)-1,0,-1):
    t,r = mll[i]
    for j in range(r-1,mll[i-1][1]-1,-1):
        if t == 1:
            nl[j] = temp[y]
            y -= 1
        elif t == 2:
            nl[j] = temp[x]
            x += 1

for i in range(n):
    nl[i] = str(nl[i])
print(' '.join(nl))


from sys import setrecursionlimit
setrecursionlimit(200000)
class data:
    def __init__(self,a):
        self.data=a
    def qsort(self,l,r,reverse):
        if l>=r :
            return None
        ind=l
        i=l
        j=r
        key=self.data[l]
        while i<j :
            if reverse==1:
                while (self.data[j]>=key) & (j>=l):
                    j-=1
                while (self.data[i]<key) & (i<=r) :
                    i+=1
                    ind=i-1
                if i<j :
                    ind+=1
                    t=self.data[i]
                    self.data[i]=self.data[j]
                    self.data[j]=t
            else :
                while (self.data[j]<=key) & (j>=l) :
                    j-=1
                    ind=j+1
                while (self.data[i]>key) & (i<=r) :
                    i+=1
                if i<j :
                    ind-=1
                    t=self.data[i]
                    self.data[i]=self.data[j]
                    self.data[j]=t
        self.qsort(l,ind,reverse)
        self.qsort(ind+1,r,reverse)            
n,m=[int(i) for i in input().split()]
a=data([int(i) for i in input().split()])
t=[None]*m
r=[None]*m
for i in range(m):
    t[i],r[i]=[int(j) for j in input().split()]
Sortlist=[]
Sortcount=0
M=0
for i in range(m-1,-1,-1) :
    if r[i]>M :
        Sortlist.append([r[i],t[i]])
        Sortcount+=1
        M=r[i]
#a.qsort(0,Sortlist[-1][0]-1,1)
S=sorted(a.data[0:Sortlist[-1][0]])
L=0
R=M-1
for i in range(M-1,-1,-1) :
    if (Sortcount>0):
        if (i<Sortlist[Sortcount-1][0]) :
            Reverse=Sortlist[Sortcount-1][1]
            Sortcount-=1
    if Reverse==1 :
        a.data[i]=S[R]
        R-=1
    else :
        a.data[i]=S[L]
        L+=1
for i in a.data[0:-1] :
    print(str(i)+" ",end="")
print(a.data[-1])

'''class data:
    def __init__(self,a):
        self.data=a
    def qsort(self,l,r,reverse):
        if l>=r :
            return None
        ind=l
        i=l
        j=r
        key=self.data[l]
        while i<j :
            if reverse==1:
                while (self.data[j]>=key) & (j>=l):
                    j-=1
                while (self.data[i]<key) & (i<=r) :
                    i+=1
                    ind=i-1
                if i<j :
                    ind+=1
                    t=self.data[i]
                    self.data[i]=self.data[j]
                    self.data[j]=t
            else :
                while (self.data[j]<=key) & (j>=l) :
                    j-=1
                    ind=j+1
                while (self.data[i]>key) & (i<=r) :
                    i+=1
                if i<j :
                    ind-=1
                    t=self.data[i]
                    self.data[i]=self.data[j]
                    self.data[j]=t
        self.qsort(l,ind,reverse)
        self.qsort(ind+1,r,reverse)'''           
n,m=[int(i) for i in input().split()]
adata=[int(i) for i in input().split()]
t=[None]*m
r=[None]*m
for i in range(m):
    t[i],r[i]=[int(j) for j in input().split()]
Sortlist=[]
Sortcount=0
M=0
for i in range(m-1,-1,-1) :
    if r[i]>M :
        Sortlist.append([r[i],t[i]])
        Sortcount+=1
        M=r[i]
#a.qsort(0,Sortlist[-1][0]-1,1)
S=sorted(adata[0:Sortlist[-1][0]])
L=0
R=M-1
for i in range(M-1,-1,-1) :
    if (Sortcount>0):
        if (i<Sortlist[Sortcount-1][0]) :
            Reverse=Sortlist[Sortcount-1][1]
            Sortcount-=1
    if Reverse==1 :
        adata[i]=S[R]
        R-=1
    else :
        adata[i]=S[L]
        L+=1
for i in adata[0:-1] :
    print(str(i)+" ",end="")
print(adata[-1])

[n,m]=[int(i) for i in input().split()];
a=[int(i) for i in input().split()];
t=m*[0];
r=m*[0];
for k in range(0,m):
    [t[k],r[k]]=[int(i) for i in input().split()];

tstack=[];
rstack=[];
mm=-1;
lastt=-1;
tstack.append(t[0]);
rstack.append(r[0]);
top=0;
for i in range(1,m):
    while(top>-1):
        if(r[i]>=rstack[top]):
            rstack.pop();
            tstack.pop();
            top=top-1;
        else:
            break;
    if(top==-1):
        rstack.append(r[i]);
        tstack.append(t[i]);
        top=top+1;
    else:
        if(tstack[top]!=t[i]):
            rstack.append(r[i]);
            tstack.append(t[i]);
            top=top+1;

r0=rstack[0];
sorteda=sorted(a[:r0]);
begin=0;
end=len(sorteda)-1;

for mindex in range(0,len(tstack)-1):
    rthis=rstack[mindex];
    rnext=rstack[mindex+1];
    if(tstack[mindex]==1):
        for i in range(rthis-1,rnext-1,-1):
            a[i]=sorteda[end];
            end=end-1;
    else:
        for i in range(rthis-1,rnext-1,-1):
            a[i]=sorteda[begin];
            begin=begin+1;

if(tstack[-1]==1):
    for i in range(0,rstack[-1]):
        a[i]=sorteda[i+begin];
else:
    for i in range(0,rstack[-1]):
        a[i]=sorteda[end-i];

print(*a, sep=" ")




__author__ = 'Utena'
import operator
n,m=map(int,input().split())
s=list(map(int,input().split()))
r0=[[0,10000000000]]
x0=[0]
for i in range(m):
    t,r=map(int,input().split())
    while r>=r0[-1][1]:
            r0=r0[:-1]
            x0=x0[:-1]
    r0.append([t,r])
    x0.append(i)
Q=sorted(s[:r0[1][1]])
r0.append([0,0])
x=0
j=2
y=r0[1][1]-1
i=r0[1][1]-1
while True:
    while i>=r0[j][1]:
        if r0[j-1][0]==2:
            temp=Q[x]
            x+=1
        else:
            temp=Q[y]
            y-=1
        s[i]=temp
        i-=1
        if i<0:break
    j+=1
    if y<x:break
print(' '.join(map(str,s)))