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



Sample input:
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:
2
1
3
4
1

Submission status: Accepted

Solution code:
from collections import deque

class MyDeque:
    def __init__(self):
        self.data = deque()

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

    def random_access(self, p):
        print(self.data[p])

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

my_deque = MyDeque()
q = int(input())

for _ in range(q):
    args = input().split(' ')
    if args[0] == '0':
        d = int(args[1])
        x = int(args[2])
        my_deque.push(d, x)

    elif args[0] == '1':
        p = int(args[1])
        my_deque.random_access(p)

    elif args[0] == '2':
        d = int(args[1])
        my_deque.pop(d)

Submission status: Accepted

Solution code:
from collections import deque
q=int(input())
lis=deque()
lq=[]
for i in range(q):
	lq.append([int(x) for x in input().split(' ')])

for i in lq:
	order=i[0]
	if order==0:
		d=i[1]
		if d==0:
			lis.appendleft(i[2])
		else:
			lis.append(i[2])
	elif order==1:
		print(lis[i[1]])
	elif order==2:
		d=i[1]
		if d==0:
			lis.popleft()
		else:
			lis.pop()

Submission status: Accepted

Solution code:
from collections import deque
que = deque([])
Q = int(input())

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

Submission status: Accepted

Solution code:
from collections import deque
num = int(input())
A = deque()

for i in range(num):
    queryi = list(map(int, input().split()))

    if queryi[0] == 1:
        print(A[queryi[1]])

    elif queryi[0] == 2:
        if queryi[1] == 0:
            A.popleft()
        else:
            A.pop()

    else:
        if queryi[1] ==0:
            A.appendleft(queryi[2])
        else:
            A.append(queryi[2])

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
num_list = []
for i in range(n):
    num_list.append(list(map(int,input().split())))

num_of_operations = num_list[0][0]

vec = deque('')

for i in range(len(num_list)):
    operation = num_list[i][0]

    if(operation == 0):
        d = num_list[i][1]
        if(d == 0): vec.appendleft(num_list[i][2])
        elif(d == 1): vec.append(num_list[i][2])
            
            
    elif(operation == 1):
        p = num_list[i][1]
        print(vec[p])

    elif(operation == 2):
        d = num_list[i][1]
        if(d == 0): vec.popleft() 
        elif(d == 1): vec.pop()         

    #print(vec)

Submission status: Accepted

Solution code:
from collections import deque

A = deque()

#0:pushBack
def pushBack(op,x):
  if op == 0:
    A.appendleft(x)
  else:
    A.append(x)

#1:randomAccess
def randomAccess(x):
  print(A[x])
  
#2:popBack
def popBack(op):
  if op == 0:
    A.popleft()
  else:
    A.pop()


cmdList = [ pushBack, randomAccess, popBack ]
n = int(input())

for i in range(n):
  cmd = input()
  if cmd.startswith("0"):
    c,o,x = map(int,cmd.split(" "))
    cmdList[0](o,x)
  if cmd.startswith("1"):
    c,param = map(int,cmd.split(" "))
    cmdList[1](param)
  if cmd.startswith("2"):
    c,o = map(int,cmd.split(" "))
    cmdList[2](o)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
from collections import deque

q = int(input())
querys = [list(map(int, input().split())) for _ in range(q)]

d = deque()

for query in querys:
    if query[0] == 0:
        if query[1] == 0:
            d.appendleft(query[2])
        else:
            d.append(query[2])

    if query[0] == 1:
        print(d[query[1]])

    if query[0] == 2:
        if query[1] == 0:
            d.popleft()
        else:
            d.pop()

Submission status: Accepted

Solution code:
from collections import deque

def push(a, d, x):
    if d == 0: a.appendleft(x)
    elif d == 1: a.append(x)
def randomAccess(a, p):
    print(a[p])
def pop(a, d):
    if d == 0: _=a.popleft()
    elif d == 1: _=a.pop()

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

Submission status: Accepted

Solution code:
from collections import deque

que = deque()
q = int(input())

for i in range(q):
    query = input().split()

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

Submission status: Accepted

Solution code:
from collections import deque

q = int(input())
data_list = list()
for i in range(q):
    data = input()
    data_list.append(data)

a = deque()
for data in data_list:
    if data[0] == "0":
        if data.split()[1] == "1":
            a.append(data.split()[2])
        else:
            x = data.split()[2]
            a.appendleft(x)
        # print(a)
    elif data[0] == "1":
        print(a[int(data.split()[1])])
        # print(a)
    else:
        if data.split()[1] == "1":
            a.pop()
        else:
            a.popleft()
        # print(a)

Submission status: Accepted

Solution code:
import collections
q = int(input())
dq = collections.deque()
ans = []
while q:
    q -= 1
    s = input().split()
    if s[0] == '0':
        if s[1] == '0':
            dq.appendleft(s[2])
        else:
            dq.append(s[2])
    elif s[0] == '1':
        ans.append(dq[int(s[1])])
    else:
        if s[1] == '0':
            dq.popleft()
        else:
            dq.pop()
print('\n'.join(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

d = deque()
q = int(input())
for i in range(q):
    a, *b = map(int, input().split())
    if a == 0:
        if b[0] == 0:
            d.appendleft(b[1])
        else:
            d.append(b[1])
    elif a == 1:
            print(d[b[0]])
    else:
        if b[0] == 0:
            d.popleft()
        else:
            d.pop()

Submission status: Accepted

Solution code:
from  collections import deque

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

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

Solution code:
from collections import deque

ans=deque([])

for _ in range(int(input())):
    
    a=list(map(int,input().split()))
    
    if len(a)==3:
        if a[1]==0: ans.appendleft(a[2])
        
        else: ans.append(a[2])
        
    if len(a)==2:
        
        if a[0]==1 : print(ans[a[1]])
        
        else: 
            
            if a[1]==0: ans.popleft()
            
            else: ans.pop()

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
ans = deque()


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

def spaced_to_ints(spaced_some):
    return [int(a) for a in spaced_some.split(" ")]

def order_sequence_chars(str):
    return list(str)

def count_order(order):
    return(int(order))

def get_dice():
    input_data = input().split(" ")
    items = [int(cont) for cont in input_data]
    keys = ['T', 'S', 'E', 'W', 'N', 'B']
    return dict(zip(keys, items))

def rot_dice(rot, dice):
    if rot == "N":
        keys = ['T', 'S', 'B', 'N']
        items = dice['S'], dice['B'], dice['N'], dice['T']
        new_dice = dict(zip(keys, items))
        dice.update(new_dice)

    if rot == "S":
        keys = ['T', 'S', 'B', 'N']
        items = dice['N'], dice['T'], dice['S'], dice['B']
        new_dice = dict(zip(keys, items))
        dice.update(new_dice)

    if rot == "E":
        keys = ['T', 'E', 'B', 'W']
        items = dice['W'], dice['T'], dice['E'], dice['B']
        new_dice = dict(zip(keys, items))
        dice.update(new_dice)

    if rot == "W":
        keys = ['T', 'E', 'B', 'W']
        items = dice['E'], dice['B'], dice['W'], dice['T']
        new_dice = dict(zip(keys, items))
        dice.update(new_dice)

    if rot == "R":
        keys = ['N', 'W', 'S', 'E']
        items = dice['E'], dice['N'], dice['W'], dice['S']
        new_dice = dict(zip(keys, items))
        dice.update(new_dice)

    if rot == "L":
        keys = ['N', 'W', 'S', 'E']
        items = dice['W'], dice['S'], dice['E'], dice['N']
        new_dice = dict(zip(keys, items))
        dice.update(new_dice)

def search_right_surf(conds, dice):
    a, b = conds
    a_key = [k for k, v in dice.items() if v == a]
    b_key = [k for k, v in dice.items() if v == b]
    key_list = a_key + b_key

    part_st = ''.join(key_list)

    def search(part_st):
        if part_st in "TNBST":
            return "W"

        if part_st in "TSBNT":
            return "E"

        if part_st in "TEBWT":
            return "N"

        if part_st in "TWBET":
            return "S"

        if part_st in "NESWN":
            return "B"

        if part_st in "NWSEN":
            return "T"

    target_key = search(part_st)
    print(dice[target_key])

def search_top_surf(top_surf, dice):

    dice2_surf, *_ = [k for k, v in dice.items() if v == top_surf]

    if dice2_surf == 'N':
        rot_dice("S", dice)

    if dice2_surf == 'B':
        controls = list("SS")
        for i in controls:
            rot_dice(i, dice)
    if dice2_surf == 'S':
        controls = list("N")
        for i in controls:
            rot_dice(i, dice)
    if dice2_surf == 'W':
        controls = list("E")
        for i in controls:
            rot_dice(i, dice)
    if dice2_surf == 'E':
        controls = list("W")
        for i in controls:
            rot_dice(i, dice)

def search_flont_surf(flont_surf, dice):

    dice2_surf, *_ = [k for k, v in dice.items() if v == flont_surf]

    if dice2_surf == 'E':
        controls = list("R")
        for i in controls:
            rot_dice(i, dice)

    if dice2_surf == 'S':
        controls = list("RR")
        for i in controls:
            rot_dice(i, dice)

    if dice2_surf == 'W':
        rot_dice("L", dice)

def rot_compare(dice1, dice2):

    def compare_dice(dice1, dice2):
        if dice1 == dice2:
            return True

    comb = ["N", "S", "E", "W", "R", "L"]
    tups = list()
    for i in range(6):
        tups.append(itertools.combinations(comb, i))

    if compare_dice(dice1, dice2):
        return True

    for commands in itertools.chain.from_iterable(tups):
        for command in commands:
            rot_dice(command, dice2)
            if compare_dice(dice1, dice2):
                return True
    return False

def compare_many_dices(dice_list):
    dice1 = dice_list.pop(0)
    for dice2 in dice_list:
        if rot_compare(dice1, dice2):
            return True
    return False

def q1():

    dice = get_dice()
    controls = list(input())
    for i in controls:
        rot_dice(i, dice)
    print(dice["T"])

def q2():

    dice = get_dice()
    a = input()
    repeater = int(a)

    for neee in range(repeater):
        control_input = input().split(" ")
        conds = [int(i) for i in control_input]
        search_right_surf(conds, dice)

def q3():
    dice1 = get_dice()
    dice2 = get_dice()

    if rot_compare(dice1, dice2):
        print("Yes")
    else:
        print("No")

def q4():
    inp = input()
    dice_num = int(inp)
    dice_list = list()
    for i in range(dice_num):
        dice_list.append(get_dice())

    if compare_many_dices(dice_list):
        print("No")

    else:
        print("Yes")

def alds1():
    n = int(input())
    A = [int(cont) for cont in input().split(" ")]

    print(' '.join(map(str, A)))

    for _i in range(len(A)-1):
        i = _i+1
        v = A[i]
        j = i-1

        while j >= 0:
            if A[j] < v:
                break

            A[j+1] = A[j]
            A[j] = v
            j = j - 1

        print(' '.join(map(str, A)))

###############################################
###############################################
###############################################
###############################################
###############################################


def list_head():
    return 0

def list_end():
    return -1

def pushBack(li, dist, num):
    if dist == 0:
        li.insert(dist, num)
    if dist == -1:
        li.append(num)

def randomAccess(li, dist):
    print(li[dist])

def popBack(li, dist):
    if dist == 0:
        li.popleft()

    if dist == -1:
        li.pop()



def itp2_1_a():

    def func_allocate(mylist, inp):
        # "0 x" or "1 p" or "2" given

        order, *others = spaced_to_ints(inp)

        if others:
            num = others[0]

        if order == 0:
            pushBack(mylist, num, list_end())

        if order == 1:
            randomAccess(mylist, num)

        if order == 2:
            popBack(mylist, list_end())

    mylist = list()
    count = count_order(input())

    for i in range(count):
        func_allocate(mylist, input())


def itp2_1_b():
    from collections import deque
    def alloc(dist):
        if dist == 0:
            return list_head()
        if dist == 1:
            return list_end()

    def func_allocate(mylist, inp):

        # "0 d x" or "1 p" or "2 d" given
        order = spaced_to_ints(inp)
        mode = order.pop(0)

        if mode == 0:
            dist = alloc(order.pop(0))
            num = order.pop(0)
            pushBack(mylist, dist, num)

        if mode == 1:
            dist = order.pop(0)
            randomAccess(mylist, dist)

        if mode == 2:
            dist = alloc(order.pop(0))
            popBack(mylist, dist)

    mydeq = deque()
    count = count_order(input())
    for i in range(count):
        func_allocate(mydeq, input())

itp2_1_b()

Submission status: Accepted

Solution code:
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: # push(x)
    if c[1] == 0:
      d.appendleft(c[2])
    else:
      d.append(c[2])
  elif c[0] == 1: # randomAccess(p)
    print(d[c[1]])
  elif c[0] == 2: # pop()
    if c[1] == 0:
      d.popleft()
    else:
      d.pop()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
from collections import deque

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

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

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

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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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]

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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:])
    # if query[0] == '0':
    #     if query[2] == '0':A.insert(0, query[4:])
    #     else:A.append(query[4:])
    elif query[0] == '1':
        # ans.append(A[int(query[2:])])
        print(A[int(query[2:])].strip())
    elif query[:3] == '2 0':A.popleft()
    else:A.pop()
        # if query[2] == '0':A = A[1:]
        # else:A.pop()

# for i in ans:
#     print(i.strip())

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
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()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()