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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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':    # push 0or1 x
        deq.appendleft(arg[1]) if arg[0] == '0' else deq.append(arg[1])
    elif query == '1':  # randaccess p
        print(deq[int(arg[0])])
    elif query == '2':    # pop 0or1
        deq.popleft() if arg[0] == '0' else deq.pop()
    else:
        raise AssertionError

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_1_B&lang=jp
# Deque

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()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Runtime Error

Solution code:
cnt = int(input())
a = []
for i in range(cnt):
    li = list(map(int,input().split()))
    if li[0] == 0:
        if li[1] == 0:
            a.insert(0,li[2])
        else:
            a.insert(-1,li[2])
    if li[0] == 1:
        print(a[li[1]])
    if li[0] ==2:
        if li[1]==0:
            a.popleft()
        else:
            a.pop()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May 19 15:10:55 2018
AOJ ITP2_1_B
@author: maezawa
"""
import numpy as np
q = int(input())
a = np.zeros(8000000, dtype=int)
start = 4000000
end = 4000001
for i in range(q):
    ops = list(map(int,input().split()))
    if ops[0] == 0:
        if ops[1] == 0:
            a[start] = ops[2]
            start -= 1
        else:
            a[end] = ops[2]
            end += 1
    elif ops[0] == 1:
        print(a[start+ops[1]+1])
    elif ops[0] == 2:
        if ops[1] == 0:
            start += 1
        elif ops[1] == 1:
            end -= 1
#    print(a[start+1:end])

Submission status: Runtime Error

Solution code:
def push(lists, d, x):
    if d == 0:
        lists.insert(0, x)
    else:
        lists.insert(len(lists), x)

    return lists

def randomAccess(lists, p):
    print(lists[p])

def pop(lists, d):
    if d == 0:
        lists.pop(0)
    else:
        lists.pop(len(lists) - 1)
    return lists

if __name__ == '__main__':
    l = list()
    n = int(input())
    for i in range(n):
        j= input().split()

        if int(j[0]) == 0:
            push(l, int(j[1]), int(j[2]))
        elif int(j[0]) == 1:
            randomAccess(l, int(j[1]))
        else:
            pop(l, j[2])

Submission status: Runtime Error

Solution code:
from collections import deque

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

def randomAccess(duque, p):
    print(duque.index(p))

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

if __name__ == '__main__':
    l = deque([])
    n = int(input())
    for i in range(n):
        j= input().split()

        if int(j[0]) == 0:
            push(l, int(j[1]), int(j[2]))
        elif int(j[0]) == 1:
            randomAccess(l, int(j[1]))
        else:
            pop(l, int(j[1]))