Problem:
Deque For a dynamic array $A = \{a_0, a_1, ...\}$ of integers, perform a sequence of the following operations:  push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.  randomAccess($p$): Print element $a_p$.  pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.  $A$ is a 0-origin array and it is empty in the initial state. Input The input is given in the following format. $q$
$query_1$
$query_2$
:
$query_q$ Each query $query_i$ is given by 0 $d$ $x$ or 1 $p$ or 2 $d$ where the first digits 0 , 1 and 2 represent push, randomAccess and pop operations respectively. randomAccess and pop operations will not be given for an empty array. Output For each randomAccess, print $a_p$ in a line. Constraints  $1 \leq q \leq 400,000$  $0 \leq p < $ the size of $A$  $-1,000,000,000 \leq x \leq 1,000,000,000$  Sample Input 1 11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1 Sample Output 1 2
1
3
4
1

Solution (Python):
from collections import deque

def get_input():
  return list(map(int, input().split(' ')))

n = get_input()[0]
d = deque()
for _ in range(n):
  c = get_input()
  if c[0] == 0: 
    if c[1] == 0:
      d.appendleft(c[2])
    else:
      d.append(c[2])
  elif c[0] == 1: 
    print(d[c[1]])
  elif c[0] == 2: 
    if c[1] == 0:
      d.popleft()
    else:
      d.pop()

Solution (Python):
from collections import deque

def main():
    N = int(input())
    q = deque()
    o = []
    for _ in range(N):
        query = list(map(int, input().split()))
        if query[0] == 0:
            if query[1] == 0:
                q.appendleft(query[2])
            elif query[1] == 1:
                q.append(query[2])
        elif query[0] == 1:
            o.append(q[query[1]])
        elif query[0] == 2:
            if query[1] == 0:
                q.popleft()
            elif query[1] == 1:
                q.pop()
    print(*o, sep='\n')

main()

Solution (Python):
import collections
_q = int(input())
que = collections.deque([])
for _ in range(_q):
    *q, = map(int, input().split())

    if q[0] == 0:
        if q[1] == 0:
            que.appendleft(q[2])
        else:
            que.append(q[2])
    elif q[0] == 1:
        print(que[q[1]])
    elif q[0] == 2:
        if q[1] == 0:
            que.popleft()
        else:
            que.pop()

Solution (Python):
from collections import deque
n = int(input())
d = deque()

for _ in range(n):
    t = input().split()
    if t[0] == "0":
        if t[1] == "0":
            d.appendleft(t[2])
        else:
            d.append(t[2])
    elif t[0] == "1":
        print(d[int(t[1])])
    else:
        if t[1] == "0":
            d.popleft()
        else:
            d.pop()

Solution (Python):
from collections import deque

d = deque()
q = int(input())

for _ in range(q):
    x = list(map(int, input().split()))
    if x[0]==0:
        dr = x[1]
        val = x[2]
        if dr==1:
            d.append(val)
        else:
            d.appendleft(val)
    elif x[0]==1:
        print(d[x[1]])
    elif x[0]==2:
        dr = x[1]
        if dr==0:
            d.popleft()
        else:
            d.pop()

Solution (Python):
from collections import deque

q = int(input())
a = deque()

for i in range(q):
    inp = input()
    
    if inp[0] == "0":
        operation, d, x = list(map(int, inp.split()))
        if d == 0:
            a.appendleft(x)
        else:
            a.append(x)

    
    elif inp[0] == "1":
        operation, p = list(map(int, inp.split()))
        print(a[p])

    
    else:
        operation, d = list(map(int, inp.split()))
        if d == 0:
            a.popleft()
        else:
            a.pop()

Solution (Python):
from collections import deque
class shinajina():
    def push(A,que):
        if que[1]==0:
            A.appendleft(que[2])
        elif que[1]==1:
            A.append(que[2])
    def randomaccess(A,que):
        print(A[que[1]])
    def pop(A,que):
        if que[1]==0:
            A.popleft()
        elif que[1]==1:
            A.pop()
        
    
    


    
def main():
    q = int(input())
    A = deque()
    query = [list( map(int,input().split())) for i in range(q)]
    for que in query:
        if que[0]==0:
            shinajina.push(A,que)
        elif que[0]==1:
            shinajina.randomaccess(A,que)
        elif que[0]==2:
            shinajina.pop(A,que)


if __name__ == '__main__':
    main()

Solution (Python):
import collections
q = int(input())
A = collections.deque()
ans = list()
for i in range(q):
    query = list(map(int, input().split()))
    if query[0] == 0:
        if query[1] == 0:
            A.appendleft(query[2])
        elif query[1] == 1:
            A.append(query[2])
    elif query[0] == 1:
        ans.append(A[query[1]])
    elif query[0] == 2:
        if query[1] == 0:
            A.popleft()
        elif query[1] == 1:
            A.pop()
for a in ans:
    print(a)

Solution (Python):
from collections import deque

def main():
	q = int(input())
	a = deque()
	for _ in range(q):
		query = list(map(int,input().split()))
		if query[0] == 0:
			if query[1] == 0:
				a.appendleft(query[2])
			else:
				a.append(query[2])
		elif query[0] == 1:
			print(a[query[1]])
		elif query[1] == 0:
			a.popleft()
		else:
			a.pop()

main()

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque

Q = int(input())
A = deque([])
ans = []

for _ in range(Q):
    q = list(map(int,input().split()))
    if q[0] == 0:
        if q[1] == 0:
            A.appendleft(q[2])
        else:
            A.append(q[2])
    elif q[0] == 1:
        ans.append(A[q[1]])
    else:
        if q[1] == 0:
            A.popleft()
        else:
            A.pop()

print(*ans,sep='\n')

Solution (Python):
import collections
q = int(input())
v = collections.deque()
for _ in range(q):
	op = list(map(int, input().split()))
	if op[0]==0:
		if op[1]==0:
			v.appendleft(op[2])
		else:
			v.append(op[2])
	elif op[0]==1:
		print(v[op[1]])
	else:
		if op[1]==0:
			v.popleft()
		else:
			v.pop()

Solution (Python):
from collections import deque

if __name__ == "__main__":

    n = int(input())
    queue = deque()

    for _ in range(n):
        param = input().split(" ")

        if param[0] == "0":
            if param[1] == "0":
                queue.appendleft(param[2])
            else:
                queue.append(param[2])

        elif param[0] == "1":
            print(queue[int(param[1])])

        elif param[0] == "2":
            if param[1] == "0":
                queue.popleft()
            else:
                queue.pop()

Solution (Python):
import collections


class Vector():
    def __init__(self):
        self.vector = collections.deque()

    def randomAccess(self, p):
        print(f'{self.vector[p]}')

    def push(self, d, x):
        if d == 0:
            self.vector.appendleft(x)
        elif d == 1:
            self.vector.append(x)
        return self

    def pop(self, d):
        if d == 0:
            self.vector.popleft()
        elif d == 1:
            self.vector.pop()


num_queue = int(input())
A = Vector()

for i in range(0, num_queue):
    queue = tuple(map(int, input().split(' ')))
    if queue[0] == 0:
        A.push(queue[1], queue[2])
    elif queue[0] == 1:
        A.randomAccess(queue[1])
    elif queue[0] == 2:
        A.pop(queue[1])

Solution (Python):
from collections import deque
z = deque()
for i in range(int(input())):
    c = list(map(int,input().split()))
    if c[0] == 0:
        z.appendleft(c[2]) if c[1] == 0 else z.append(c[2])
    elif c[0] == 1:
        print(z[c[1]])
    else:
        del z[0 if c[1] == 0 else -1]

Solution (Python):
from collections import deque

q=int(input())

S=deque()

for i in range(q):
    query=list(map(int,input().split()))
    
    if query[0]==0:
       
        if query[1]==0:
            S.appendleft(query[2])
        elif query[1]==1:
            S.append(query[2])
        
    elif query[0]==1:
        print("{}".format(S[query[1]]))
        
    elif query[0]==2:
        if query[1]==0:
            S.popleft()
        elif query[1]==1:
            S.pop()

Solution (Python):
from collections import deque

query_n = int(input())

d = deque()

for _ in range(query_n):
    query = input().split()
    
    if query[0] == "0":
        if query[1] == "0":
            d.appendleft(query[2])
        elif query[1] == "1":
            d.append(query[2])
    elif query[0] == "1":
        print(d[int(query[1])])
    elif query[0] == "2":
        if query[1] == "0":
            d.popleft()
        elif query[1] == "1":
            d.pop()

Solution (Python):
from collections import deque
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n=int(input())
    aa=deque()
    for _ in range(n):
        qq=list(map(int, input().split()))
        if qq[0]==0:
            if qq[1]:aa.append(qq[2])
            else:aa.appendleft(qq[2])
        elif qq[0]==1:
            print(aa[qq[1]])
        else:
            if qq[1]:aa.pop()
            else:aa.popleft()

main()

Solution (Python):
q = int(input())
from collections import deque
A = deque()
for _ in range(q):
    L = list(map(int, input().split()))
    if L[0] == 0 and L[1] == 0:
        A.appendleft(L[2])
    elif L[0] == 0 and L[1] == 1:
        A.append(L[2])
    elif L[0] == 1:
        print(A[L[1]])
    else:
        if L[1] == 0:
            A.popleft()
        else:
            A.pop()

Solution (Python):
from collections import deque

n=int(input())
que=deque()

for i in range(n):
    command=list(map(str,input().split()))
    if command[0]=="0":
        if command[1]=="0":
            que.appendleft(int(command[2]))
        elif command[1]=="1":
            que.append(int(command[2]))
    elif command[0]=="1":
        print(que[int(command[1])])
    elif command[0]=="2":
        if command[1]=="0":
            que.popleft()
        elif command[1]=="1":
            que.pop()

Solution (Python):
from collections import deque
import sys
q = int(input())

ans = []
A = deque()
for i in range(q):
    query = sys.stdin.readline()
    if query[:3] == '0 0':A.appendleft(query[4:])
    elif query[:3] == '0 1':A.append(query[4:])
    
    
    
    elif query[0] == '1':
        
        print(A[int(query[2:])].strip())
    elif query[:3] == '2 0':A.popleft()
    else:A.pop()

Solution (Python):
from collections import deque
Q = deque()
n = int(input())

for _ in range(n):
    command = input()
    if command[0] =="0":
        if command[2] == "0":
            Q.appendleft(command[4:])
        else:
            Q.append(command[4:])
    
    elif command[0] == "1":
        print(Q[int(command[2:])])
    
    else:
        if command[2] =="0":
            Q.popleft()
        else:
            Q.pop()

Solution (Python):
from collections import deque
import sys

n=int(input())
A=deque([])
for i in range(n):
  S=input()
  if S[0]=='0':
    if S[2]=='0':
      A.appendleft(S[4:])
    elif S[2]=='1':
      A.append(S[4:])
  elif S[0]=='1':
    print(A[int(S[2:])])
  elif S[0]=='2':
    if S[2]=='0':
      A.popleft()
    if S[2]=='1':
      A.pop()

Solution (Python):
from collections import deque
n = int(input())
lists = deque([])
 
for i in range(n):
    query = list(map(int, input().split()))
    if(query[0] == 0 and query[1] == 1):
        lists.append(query[2])
    elif(query[0] == 0 and query[1] == 0):
        lists.appendleft(query[2])
    elif(query[0] == 1):
        print(lists[query[1]])
    elif(query[0] == 2 and query[1] == 0):
        lists.popleft()
    elif(query[0] == 2 and query[1] == 1):
        lists.pop()

Solution (Python):
from collections import deque

num = int(input())

d = deque()
for i in range(num):
    c = [int(x) for x in input().split()]
    if c[0] == 0:
        if c[1] == 0:
            d.insert(0, c[2])
        else:
            d.append(c[2])
            
    elif c[0] == 1:
        print(d[c[1]])
    elif c[0] == 2:
        if c[1] == 1:
            d.pop()
        else:
            d.popleft()

Solution (Python):
from collections import deque


if __name__ == "__main__":
    num_query = int(input())
    int_que: deque = deque()
    for _ in range(num_query):
        op, *value = map(lambda x: int(x), input().split())
        if (0 == op):
            if (0 == value[0]):
                int_que.appendleft(value[1])
            else:
                int_que.append(value[1])
        elif (1 == op):
            print(f"{int_que[value[0]]}")
        elif (2 == op):
            if (0 == value[0]):
                int_que.popleft()
            else:
                int_que.pop()
        else:
            pass

Solution (Python):


from collections import deque
n=int(input())
Deq=deque()
for i in range(n):
    command=input().split( )
    com=int(command[0])
    com1=int(command[1])
    if com==0:
        if com1==1:
            Deq.append(command[2])
        elif com1==0:
            Deq.appendleft(command[2])
    elif com==1:
        q=Deq[com1]
        print(q)
    elif com==2:
        if com1==1:
            Deq.pop()
        elif com1==0:
            Deq.popleft()

Solution (Python):
from collections import deque

def push(d, x):
    if d == 0:
        ary.appendleft(x)
    elif d == 1:
        ary.append(x)

def random_access(p):
    print(ary[p])

def pop(d):
    if d == 0:
        ary.popleft()
    elif d == 1:
        ary.pop()

q = int(input())
ary = deque()
for _ in range(q):
    op = list(map(int, input().split()))
    if op[0] == 0:
        push(op[1], op[2])
    elif op[0] == 1:
        random_access(op[1])
    elif op[0] == 2:
        pop(op[1])

Solution (Python):
from collections import deque
Q = deque()
n = int(input())
for i in range(n):
    x = list(map(int, input().split()))
    if x[0] == 0:
        if x[1] == 0:
            Q.appendleft(x[2])
        if x[1] == 1:
            Q.append(x[2])
    elif x[0] == 1:
        print(Q[x[1]])
    elif x[0] == 2:
        if x[1] == 0:
            Q.popleft()
        if x[1] == 1:
            Q.pop()

Solution (Python):
from collections import deque
a = int(input())

res = deque()

for i in range(a):
    temp = list(map(int, input().split()))
    if temp[0] == 0:
        if temp[1] == 0:
            res.appendleft(temp[2])
        elif temp[1] == 1:
            res.append(temp[2])
    elif temp[0] == 1:
        print(res[temp[1]])
    else:
        if temp[1] == 0:
            res.popleft()
        elif temp[1] == 1:
            res.pop()

Solution (Python):
import sys
from collections import deque
sys.stdin.readline()
A = deque()
ans = []

def push(arg):
    A.appendleft(arg[2:]) if arg[0] == '0' else A.append(arg[2:])
def randomAccess(arg):
    ans.append(A[int(arg)])
def pop(arg):
    A.popleft() if arg[0] == '0' else A.pop()

C = {'0': push, '1': randomAccess, '2': pop}

for query in sys.stdin:
    C[query[0]](query[2:])

sys.stdout.writelines(ans)

Solution (Python):
from collections import deque
q=int(input())
a=deque([])
for i in range(q):
    query=input()
    if query[0]=="0":
        q,d,x=query.split()
        if d=="1":
            a.append(x)
        else:
            a.appendleft(x)
    else:
        q,s=query.split()
        if q=="1":
            print(a[int(s)])
        else:
            if s=="1":
                a.pop()
            else:
                a.popleft()

Solution (Python):
from collections import deque
q = int(input())
ans = deque()
for i in range(q):
    a = list(map(int, input().split()))
    if a[0] == 0:
        if a[1] == 0:
            ans.appendleft(a[2])
        else:
            ans.append(a[2])
    elif a[0] == 1:
        print(ans[a[1]])
    else:
        if a[1] == 0:
            ans.popleft()
        else:
            ans.pop()

Solution (Python):
def solve():
    import sys
    f_i = sys.stdin
    q = f_i.readline()
    
    from collections import deque
    A = deque()
    
    ans = []
    
    for q in f_i:
        if q[0] == '0':
            if q[2] == '0':
                A.appendleft(q[4:])
            else:
                A.append(q[4:])
        elif q[0] == '1':
            ans.append(A[int(q[2:])])
        else:
            if q[2] == '0':
                A.popleft()
            else:
                A.pop()
    print(''.join(ans), end='')

solve()

Solution (Python):

"""
Dynamic Arrays and List - Deque
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_1_B&lang=jp

"""
from collections import deque

dq = deque()
for _ in range(int(input())):
    op, m, n = (input() + ' 1 1').split()[:3]
    if op == '0':
        if m == '0':
            dq.appendleft(n)
        else:
            dq.append(n)
    elif op == '1':
        print(dq[int(m)])
    else:
        if m == '0':
            dq.popleft()
        else:
            dq.pop()

Solution (Python):
q = int(input())
a_list = [0]*(q*2)
head = tail = q
for _ in range(q):
    com, *query = map(int, input().split())
    if com == 0:
        if query[0] == 0:
            a_list[head] = query[1]
            if head == tail:
                tail += 1
            head -= 1
        else:
            a_list[tail] = query[1]
            if head == tail:
                head -= 1
            tail += 1
    elif com == 1:
        print(a_list[query[0] + head + 1])
    else:
        if query[0] == 0:
            head += 1
        else:
            tail -= 1

Solution (Python):
from collections import deque
import sys
q = int(sys.stdin.readline())
dq = deque()
for _ in range(q):
    s = sys.stdin.readline()
    if s[0] == '0':
        c, d, x = map(int, s.split())
        if d:
            dq.append(x)
        else:
            dq.appendleft(x)
    else:
        c, d = map(int, s.split())
        if c == 1:
            sys.stdout.write("{}\n".format(dq[d]))
        else:
            if d:
                dq.pop()
            else:
                dq.popleft()

Solution (Python):
from collections import deque

anslist = deque()
for i in range(int(input())):
    query = list(map(int, input().split()))
    if query[0] == 0:
        if query[1] == 0:
            anslist.appendleft(query[2])
        elif query[1] == 1:
            anslist.append(query[2])
    elif query[0] == 1:
        print(anslist[query[1]])
    elif query[0] == 2:
        if query[1] == 0:
            anslist.popleft()
        elif query[1] == 1:
            anslist.pop()

Solution (Python):
from collections import deque
n = int(input())
A = deque([])
for i in range(n):
    x = list(map(int, input().split()))
    if x[0] == 0:
        if x[1] == 0:
            A.appendleft(x[2])
        else:
            A.append(x[2])
    elif x[0] == 1:
        print(A[x[1]])
    elif x[0] == 2:
        if x[1] == 0:
            A.popleft()
        else:
            A.pop()

Solution (Python):
import sys
from collections import deque

n = int(sys.stdin.readline())

que = deque()

for i in range(n):
    query = sys.stdin.readline().split()
    if query[0] == '0': 
        if query[1] == '1':
            que.append(query[2])
        else:
            que.appendleft(query[2])
    elif query[0] == '1':
        print(que[int(query[1])])
    elif query[0] == '2':
        if query[1] == '1':
            que.pop()
        else:
            que.popleft()
    else:
        raise ValueError

Solution (Python):
from collections import deque



q = int(input())

A = deque([])

for _ in range(q):
    
    command, *list_num = input().split()

    if   command == "0":
        if   list_num[0] == "0":
            A.appendleft(int(list_num[1]))
        elif list_num[0] == "1":
            A.append(int(list_num[1]))
        else:
            raise
    elif command == "1":
        print(A[int(list_num[0])])
    elif command == "2":
        if   list_num[0] == "0":
            A.popleft()
        elif list_num[0] == "1":
            A.pop()
        else:
            raise
    else:
        raise

Solution (Python):
from collections import deque
deq = deque()
for _ in range(int(input())):
    moji = list(map(int, input().split()))
    method, d = moji[:2]
    if method == 0:
        x = moji[2]
        if d == 0:
            deq.appendleft(x)
        else:
            deq.append(x)
    elif method == 1:
        print(deq[d])
    elif method == 2:
        if d == 0:
            deq.popleft()
        else:
            deq.pop()

Solution (Python):
from collections import deque
def vec(ap):
    query = list(map(int, input().split()))
    if query[0] == 0:
        if query[1] == 0:
            ap.appendleft(query[2])
        elif query[1] == 1:
            ap.append(query[2])
            
    elif query[0] == 1:
        print(ap[query[1]])
        
    elif query[0] == 2:
        if query[1] == 0:
            ap.popleft()
        elif query[1] == 1:
            ap.pop()

x = []
ap = deque()
input_num = int(input())
for a in range(input_num):
    vec(ap)

Solution (Python):
from collections import deque
a = deque()
q = int(input())
for i in range(q):
  lst = list(map(int, input().split()))
  if lst[0] == 0:
    d = lst[1]
    x = lst[2]
    if d == 0:
      a.appendleft(x)
    else :
      a.append(x)
  elif lst[0] == 1:
    p = lst[1]
    print(a[p])
  elif lst[0] == 2:
    d = lst[1]
    if d == 0:
      a.popleft()
    else:
      a.pop()
  else:
    pass

Solution (Python):
from collections import deque
box = deque()
n = int(input())
for _ in range(n):
    a, b, *c = map(int, input().split())
    if a == 0:
        if b == 0:
            box.appendleft(c[0])
        else:
            box.append(c[0])
    elif a == 1:
        print(box[b])
    else:
        if b == 0:
            box.popleft()
        else:
            box.pop()

Solution (Python):
from collections import deque
Q = deque()
q = int(input())
for i in range(q):
	a = input()
	if   a[0] == '0':
		if a[2] == '0': Q.appendleft(a[4:])
		else          : Q.append(a[4:])
	elif a[0] == '1': print(Q[int(a[2:])])
	else:
		if a[2] == '0': Q.popleft()
		else:           Q.pop()

Solution (Python):
from collections import deque
A=deque()

def push(A,d,x):
    if d==0:
        A.appendleft(x)
    else:
        A.append(x)
    return A

def randomaccess(A,p):
    print(A[p])

def pop(A,d):
    if d==0:
        A.popleft()
    else:
        A.pop()
    return A

q=int(input())

for i in range(q):
    query=list(map(int,input().split()))

    if query[0]==0:
        A=push(A,query[1],query[2])
    elif query[0]==1:
        randomaccess(A,query[1])
    else:
        A=pop(A,query[1])

Solution (Python):
int(input())
A = [0 for i in range(2000000)]
back = 999999
front = 1000000

def push(d,x):
	global back, front
	if d == 0:
		front -= 1
		A[front] = x
	else:
		back += 1
		A[back] = x
def randomAccess(p):
	print(A[front + p])
def pop(d):
	global back, front
	if d == 0:
		front += 1
	else:
		back -= 1

while(1):
	try:
		a = input()
		b = [int(i) for i in a.split()]
		if b[0] == 0:
			push(b[1],b[2])
		elif b[0] == 1:
			randomAccess(b[1])
		else:
			pop(b[1])
	except EOFError:
		break

Solution (Python):
from collections import deque
a=deque()
n=int(input())
for i in range(n):
    x=list(map(int, input().split()))
    if x[0]==0:
        if x[1]==1:a.append(x[2])
        else:a.appendleft(x[2])
    if x[0]==1:print(a[x[1]])
    if x[0]==2:
        if x[1]==1:a.pop()
        else:a.popleft()

Solution (Python):
from collections import deque

q=deque()
for i in range(int(input())):
	req=list(map(int,input().split()))
	if req[0]==0:
		if req[1]==0:
			q.appendleft(req[2])
		else:
			q.append(req[2])
	elif req[0]==1:
		print(q[req[1]],end='\n')
	else:
		if req[1]==0:
			q.popleft()
		else:
			q.pop()

Solution (Python):
from collections import deque

A = deque([])

for i in range(int(input())):
    a = [int(j) for j in input().split()]
    if a[0] == 0:
        if a[1] == 0:
            A.insert(0,a[2])
        elif a[1] == 1:
            A.append(a[2])
    elif a[0] == 1:
        print(A[a[1]])
    elif a[0] == 2:
        if a[1] == 0:
            A.popleft()
        else:
            A.pop()

Solution (Python):
import sys
from collections import deque
nq = int(input())
lines = sys.stdin.readlines()
deq = deque()
for i in range(nq):
    query, *arg = lines[i].split()
    if query == '0':    
        deq.appendleft(arg[1]) if arg[0] == '0' else deq.append(arg[1])
    elif query == '1':  
        print(deq[int(arg[0])])
    elif query == '2':    
        deq.popleft() if arg[0] == '0' else deq.pop()
    else:
        raise AssertionError

Solution (Python):
from collections import deque
n=int(input())
l=deque()

def push(L,d,x):
    if d=="0":
        L.appendleft(x)
    else:
        L.append(x)

def pop(L,d):
    if d=="0":
        L.popleft()
    else:
        L.pop()

for i in range(n):
    q=input().split()
    if q[0]=="0":
        push(l,q[1],q[2])
    elif q[0]=="1":
        print(l[int(q[1])])
    else:
        pop(l,q[1])

Solution (Python):



from collections import deque

q = deque()
for i in range(0,int(input())):
    l = list(map(str,input().split()))
    c = int(l[0])

    if ( c == 0 ):
        d = int(l[1])
        if (d == 0):
            q.appendleft(l[2])
        elif (d == 1):
            q.append(l[2])
    elif(c == 1):
        d = int(l[1])
        print (q[d])
    elif(c == 2):
        d = int(l[1])
        if (d == 0):
            q.popleft()
        elif (d == 1):
            q.pop()

Solution (Python):
import sys
from collections import deque
sys.stdin.readline()
A = deque()
ans = []

def push(arg):
    A.appendleft(arg[2:]) if arg[0] == '0' else A.append(arg[2:])
def randomAccess(arg):
    ans.append(A[int(arg)])
def pop(arg):
    A.popleft() if arg[0] == '0' else A.pop()

C = {'0': push, '1': randomAccess, '2': pop}

for query in sys.stdin:
    C[query[0]](query[2:])

sys.stdout.writelines(ans)

Solution (Python):
from collections import deque
readline = open(0).readline
writelines = open(1, 'w').writelines
Q = int(readline())
ans = []
A = deque()
def push(d, x):
    (A.append if d else A.appendleft)(x)
def access(p):
    ans.append("%d\n" % A[p])
def pop(d):
    (A.pop if d else A.popleft)()


C = [push, access, pop].__getitem__
for i in range(Q):
    t, *a=map(int, readline().split())
    C(t)(*a)
writelines(ans)

Solution (Python):
from collections import deque


def main():
    n = int(input())
    deq = deque()
    for i in range(n):
        query = input()
        if (query[0] == '0'):
            vals = query.split(" ")
            d = int(vals[1])
            elem = int(vals[2])
            if d == 0:
                deq.appendleft(elem)
            else:
                deq.append(elem)
        elif (query[0] == '1'):
            vals = query.split(" ")
            idx = int(vals[1])
            print(deq[idx])
        elif (query[0] == '2'):
            vals = query.split(" ")
            d = int(vals[1])
            if d == 0:
                deq.popleft()
            else:
                deq.pop()


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

n = int(input())

anslist = deque()

for i in range(n):
    query = list(map(int, input().split()))
    if query[0] == 0:
        if query[1] == 0:
            anslist.appendleft(query[2])
        elif query[1] == 1:
            anslist.append(query[2])
    elif query[0] == 1:
        print(anslist[query[1]])
    elif query[0] == 2:
        if query[1] == 0:
            anslist.popleft()
        elif query[1] == 1:
            anslist.pop()

Solution (Python):
from collections import deque
n = int(input())
dq = deque()
for i in range(n):
    stdin = input()
    if(stdin.split()[0] == "0"):
        if(stdin.split()[1] == "1"):
            dq.append(stdin.split()[2])
        else:
            dq.appendleft(stdin.split()[2])

    elif stdin.split()[0] == "1":
        print(dq[int(stdin.split()[1])])
    else:
        if stdin.split()[1] == "1":
              dq.pop()
        else:
              dq.popleft()

Solution (Python):



import sys
from collections import deque

num_tc = int(sys.stdin.readline())
queue = deque()

for i in range(num_tc):
    q = list(map(int, sys.stdin.readline().split()))
    if q[0] == 0:
        if q[1]: queue.append(q[2])
        else: queue.appendleft(q[2])
    elif q[0] == 1:
        print(queue[q[1]])
    elif q[0] == 2:
        if q[1]: queue.pop()
        else: queue.popleft()
    else:
        raise ValueError

Solution (Python):
from collections import deque
queries = [input().split() for _ in range(int(input()))]
A = deque()
for query in queries:
    if query[0] == "0":
        if query[1] == "0":
            A.appendleft(int(query[2]))
        elif query[1] == "1":
            A.append(int(query[2]))
    elif query[0] == "1":
        print(A[int(query[1])])
    elif query[0] == "2":
        if query[1] == "0":
            A.popleft()
        elif query[1] == "1":
            A.pop()

Solution (Python):
from collections import deque
n = int(input())
de = deque()
for i in range(n):
    o = list(map(int, input().split()))
    if (o[0] == 0):
        if (o[1] == 0):
            de.appendleft(o[2])
        else:
            de.append(o[2])
    elif (o[0] == 1):
        print(de[o[1]])
    else:
        if (o[1] == 0):
            de.popleft()
        else:
            de.pop()

Solution (Python):
from collections import deque
n = int(input())
a = deque([])
for _ in range(n):
  q = list(map(int, input().split()))
  if q[0]==0:
    if q[1]==0:
      a.appendleft(q[2])
    else:
      a.append(q[2])
  elif q[0]==1:
    print(a[q[1]])
  else:
    if q[1]==0:
      a.popleft()
    else:
      a.pop()