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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

n = int(input())
word = deque()

for i in range(n):
    command = list(map(int, input().split()))
    if command[0] == 0:
        if command[1] == 0:
            word.insert(0, command[1])
        else:
            word.append(command[1])
    elif command[0] == 1:
        print('{0}'.format(word[command[1]]))
    elif command[0] == 2:
        if command[1] == 0:
            word.pop()
        else:
            word.popleft()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

n = int(input())
word = deque()

for i in range(n):
    command = list(map(int, input().split()))
    if command[0] == 0:
        if command[1] == 0:
            word.insert(0, command[2])
        else:
            word.append(command[1])
    elif command[0] == 1:
        print('{0}'.format(word[command[1]]))
    elif command[0] == 2:
        if command[1] == 0:
            word.pop()
        else:
            word.popleft()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

n = int(input())
word = deque()

for i in range(n):
    command = list(map(int, input().split()))
    if command[0] == 0:
        if command[1] == 0:
            word.insert(0, command[2])
        else:
            word.append(command[2])
    elif command[0] == 1:
        print('{0}'.format(word[command[1]]))
    elif command[0] == 2:
        if command[1] == 0:
            word.pop()
        else:
            word.popleft()

Submission status: Wrong Answer

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.pop(0)
        else:
            a.pop(-1)

Submission status: Wrong Answer

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.pop(0)
        else:
            a.pop()

Submission status: Wrong Answer

Solution code:
q = int(input())
a = []
end = 0
for i in range(q):
    ops= list(map(int,input().split()))
    if ops[0] == 0:
        end += 1
        if ops[1] == 0:
            a.insert(0,ops[2])
        else:
            a.insert(end,ops[2])
    elif ops[0] == 1:
        print(a[ops[1]])
    elif ops[0] == 2:
        end -= 1

Submission status: Wrong Answer

Solution code:
q = int(input())
a = []
end = 0
for i in range(q):
    ops= list(map(int,input().split()))
    if ops[0] == 0:
        end += 1
        if ops[1] == 0:
            a.insert(0,ops[2])
        else:
            a.insert(end,ops[2])
    elif ops[0] == 1:
        print(a[ops[1]])
    elif ops[0] == 2:
        end -= 1
        if ops[1] == 0:
            a.pop(0)

Submission status: Wrong Answer

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[1])

Submission status: Wrong Answer

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[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]))

Submission status: Wrong Answer

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):
    if p == 0:
        print(duque.pop())
    else:
        for i in range(p - 1):
            duque.pop()
        print(duque.pop())

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

def randomAccess(duque, p):
    print(duque[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]))

Submission status: Time Limit Exceeded

Solution code:
listForStock=[]
listForPrint=[]

def push(d,x):
    if d==0:
        listForStock.insert(0,x)
    elif d==1:
        listForStock.append(x)
    else:
        print("Error")
   
def randomAccess(p):
    listForPrint.append(listForStock[p])

def pop(d):
    if d==0:
        del listForStock[0]
    elif d==1:
        lastIndex=len(listForStock)
        del listForStock[lastIndex-1]
    else:
        print("Error")

#-------------------main-------------------------
inputNumber=int(input())
while inputNumber>0:
    inputNumbers=input().split()
    if len(inputNumbers)==3 and int(inputNumbers[0])==0:
        push(int(inputNumbers[1]), int(inputNumbers[2]))
    elif len(inputNumbers)==2:
        if int(inputNumbers[0])==1:
            randomAccess(int(inputNumbers[1]))
        elif int(inputNumbers[0])==2:
            pop(int(inputNumbers[1]))
        else:
            print("Error")
    else:
        print("Error")
    inputNumber-=1

for item in listForPrint:
    print(item)
#--------------------main----------------------------

Submission status: Time Limit Exceeded

Solution code:
listForStock=[]
listForPrint=[]

def push(d,x):
    if d==0:
        listForStock.insert(0,x)
    elif d==1:
        listForStock.append(x)
    else:
        print("Error")
   
def randomAccess(p):
    listForPrint.append(listForStock[p])

def pop(d):
    if d==0:
        del listForStock[0]
    elif d==1:
        lastIndex=len(listForStock)
        del listForStock[lastIndex-1]
    else:
        print("Error")

#-------------------main-------------------------
inputNumber=int(input())
while inputNumber>0:
    inputNumbers=input().split()
    if int(inputNumbers[0])==0:
        push(int(inputNumbers[1]), int(inputNumbers[2]))
    elif len(inputNumbers)==2:
        if int(inputNumbers[0])==1:
            randomAccess(int(inputNumbers[1]))
        elif int(inputNumbers[0])==2:
            pop(int(inputNumbers[1]))
    inputNumber-=1

for item in listForPrint:
    print(item)
#--------------------main----------------------------

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for i in range(n):
    order = input().split()
    if order[0] == '0':
        if order[1] == '0': a.insert(0, int(order[2]))
        elif order[1] == '1': a.append(int(order[2]))
    elif order[0] == '1':
        print(a[int(order[1])])
    elif order[0] == '2':
        if order[1] == '0': a.pop(0)
        elif order[1] == '1': a.pop()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for i in range(n):
    order = [int(o) for o in input().split()]
    if order[0] == 0:
        if order[1]: a.append(order[2])
        else: a.insert(0, order[2])
    elif order[0] == 1:
        print(a[order[1]])
    elif order[0] == 2:
        if order[1]: a.pop()
        else: a.pop(0)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May 19 15:10:55 2018
AOJ ITP2_1_B
@author: maezawa
"""

q = int(input())
a = []

for i in range(q):
    ops= list(map(int,input().split()))
    if ops[0] == 0:
        if ops[1] == 0:
            a.insert(0,ops[2])
        else:
            a.append(ops[2])
    elif ops[0] == 1:
        print(a[ops[1]])
    else:
        if ops[1] == 0:
            a.pop(0)
        else:
            a.pop(-1)

Submission status: Time Limit Exceeded

Solution code:
q = int(input())
a = []

for i in range(q):
    ops= list(map(int,input().split()))
    if ops[0] == 0:
        if ops[1] == 0:
            a.insert(0,ops[2])
        else:
            a.append(ops[2])
    elif ops[0] == 1:
        print(a[ops[1]])
    else:
        if ops[1] == 0:
            a.pop(0)
        else:
            a.pop(-1)

Submission status: Time Limit Exceeded

Solution code:
q = int(input())
a = []

for i in range(q):
    ops= list(map(int,input().split()))
    if ops[0] == 0:
        if ops[1] == 0:
            a.insert(0,ops[2])
        else:
            a.append(ops[2])
    elif ops[0] == 1:
        print(a[ops[1]])
    elif ops[0] == 2:
        if ops[1] == 0:
            a.pop(0)
        else:
            a.pop(-1)

Submission status: Time Limit Exceeded

Solution code:
arr = []

for _ in range(int(input())):
    cmd = [int(x) for x in input().split()]

    if cmd[0] == 0:
        if cmd[1]:
            arr.append(cmd[2])
        else:
            arr.insert(0, cmd[2])
    elif cmd[0] == 1:
        print(arr[cmd[1]])
    else:
        if cmd[1]:
            arr.pop()
        else:
            arr.pop(0)

Submission status: Time Limit Exceeded

Solution code:
# 2018/06/02

class Deque2(object):
    def __init__(self):
        self.a = list()

    def push(self, d, x):
        if d == 0:
            self.a = [x] + self.a
        elif d == 1:
            self.a.append(x)

    def randomAccess(self, p):
        print self.a[p]

    def pop(self, d):
        if d == 0:
            self.a = self.a[1:]
        elif d == 1:
            self.a = self.a[:-1]

    def __repr__(self):
        return " ".join([str(i) for i in self.a])

if __name__ == "__main__":
    d = Deque2()
    q = int(raw_input())
    for i in range(q):
        query = [int(j) for j in raw_input().split()]
        if query[0] == 0:
            d.push(query[1], query[2])
        elif query[0] == 1:
            d.randomAccess(query[1])
        elif query[0] == 2:
            d.pop(query[1])

Submission status: Time Limit Exceeded

Solution code:
# 2018/06/02
 
class Deque2(object):
    def __init__(self):
        self.a = list()
 
    def push(self, d, x):
        if d == 0:
            self.a = [x] + self.a
        elif d == 1:
            self.a.append(x)
 
    def randomAccess(self, p):
        print self.a[p]
 
    def pop(self, d):
        if d == 0:
            self.a = self.a[1:]
        elif d == 1:
            self.a = self.a[:-1]
 
    def __repr__(self):
        return " ".join([str(i) for i in self.a])
 
if __name__ == "__main__":
    d = Deque2()
    q = int(raw_input())
    for i in range(q):
        query = [int(j) for j in raw_input().split()]
        if query[0] == 0:
            d.push(query[1], query[2])
        elif query[0] == 1:
            d.randomAccess(query[1])
        elif query[0] == 2:
            d.pop(query[1])

Submission status: Time Limit Exceeded

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, int(j[1]))

Submission status: Time Limit Exceeded

Solution code:
import sys

def decodes(x):
	temp = []
	for i in range(len(x)):
		if x[i][0] == 0:
			if x[i][1] == 0:
				temp = [x[i][2]] + temp
			else:
				temp = temp + [x[i][2]]
		elif x[i][0] == 1:
			print(temp[x[i][1]])
		elif x[i][0] == 2:
			if x[i][1] == 0:
				temp = temp[1:]
			else:
				temp = temp[:len(temp) - 1]

l = []
for i in sys.stdin:
	l.append(i.split())

for i in range(len(l)):
	for j in range(len(l[i])):
		l[i][j] = int(l[i][j])

decodes(l)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

from collections import deque

n = int(input())
word = deque()

for i in range(n):
    command = list(map(int, input().split()))
    if command[0] == 0:
        if command[1] == 0:
            word.insert(0, command[2])
        else:
            word.append(command[2])
    elif command[0] == 1:
        print('{0}'.format(word[command[1]]))
    elif command[0] == 2:
        if command[1] == 1:
            word.pop()
        else:
            word.popleft()

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

num = int(input())

q = deque()

for i in range(num):
	cmd = stdin.readline().strip().split()
	if cmd[0] == "0":
		if cmd[1] == "0":
			q.appendleft(cmd[2])
		else:
			q.append(cmd[2])
	elif cmd[0] == "1":
		print(q[int(cmd[1])],sep='\n')
	else:
		if cmd[1] == "0":
			q.popleft()
		else:
			q.pop()

Submission status: Accepted

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

for i in range(cnt):
    li = list(map(int,input().split()))
    if li[0] == 0:
        if li[1] == 0:
            a.appendleft(li[2])
        else:
            a.append(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: Accepted

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 = [0 for _ in range(8000000)]
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: Accepted

Solution code:
from collections import deque

n = int(input())
d = deque()

for i in range(n):
    cmd = [int(i) for i in input().split()]

    if cmd[0] == 0:
        if cmd[1] == 0: d.appendleft(cmd[2])
        else: d.append(cmd[2])

    elif cmd[0] == 1:
        print(d[cmd[1]])

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

Submission status: Accepted

Solution code:
from collections import deque

deq = deque([])

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

Submission status: Accepted

Solution code:

from math import ceil, floor, log2


class Node:
    __slots__ = ('value', 'prev', 'next', 'pskip', 'nskip')

    def __init__(self, value):
        self.value = value
        self.prev = None
        self.next = None
        self.pskip = None
        self.nskip = None

    def __str__(self):
        return '<Node({})>'.format(self.value)


class Deque:
    SKIPSIZE = 128

    def __init__(self):
        self.count = 0
        self.head = Node(None)
        self.tail = Node(None)
        self.head.next = self.tail
        self.index = []
        self.tail.prev = self.head

    def push(self, value):
        node = Node(value)
        node.prev = self.tail.prev
        node.next = self.tail
        self.tail.prev.next = node
        self.tail.prev = node
        self.count += 1
        self._add_index(node)
        self._add_pskip(node)

    def push_left(self, value):
        node = Node(value)
        node.prev = self.head
        node.next = self.head.next
        self.head.next.prev = node
        self.head.next = node
        self.count += 1
        self._add_index_left(node)
        self._add_nskip(node)

    def pop(self):
        node = self.tail.prev
        self.tail.prev = node.prev
        node.prev.next = self.tail
        self.count -= 1
        self._remove_index()
        self._remove_pskip(node)
        return node.value

    def pop_left(self):
        node = self.head.next
        self.head.next = node.next
        node.next.prev = self.head
        self.count -= 1
        self._remove_index_left()
        self._remove_nskip(node)
        return node.value

    def _add_pskip(self, node):
        if self.count >= self.SKIPSIZE:
            if node.prev.pskip is None:
                pnode = node
                for _ in range(self.SKIPSIZE):
                    pnode = pnode.prev
            else:
                pnode = node.prev.pskip.next
            node.pskip = pnode
            pnode.nskip = node

    def _add_nskip(self, node):
        if self.count >= self.SKIPSIZE:
            if node.next.nskip is None:
                nnode = node
                for _ in range(self.SKIPSIZE):
                    nnode = nnode.next
            else:
                nnode = node.next.nskip.prev
            node.nskip = nnode
            nnode.pskip = node

    def _remove_pskip(self, node):
        if node.pskip is not None:
            node.pskip.nskip = None
            node.pskip = None

    def _remove_nskip(self, node):
        if node.nskip is not None:
            node.nskip.pskip = None
            node.nskip = None

    def _add_index(self, node):
        exp = ceil(log2(self.count))
        if self.count == 2**exp:
            # print('add_index', self.count)
            self.index.append(node)

    def _add_index_left(self, node):
        self.index = [n.prev for n in self.index]
        self._add_index(self.tail.prev)

    def _remove_index(self):
        if self.count == 0:
            # print('remove_index', self.count)
            self.index.pop()
            return
        exp = ceil(log2(self.count))
        if self.count == 2**exp - 1:
            # print('remove_index', self.count)
            self.index.pop()

    def _remove_index_left(self):
        index = [n.next for i, n in enumerate(self.index) if i > 0]
        index.insert(0, self.head.next)
        self.index = index
        self._remove_index()

    def _select_index(self, n):
        exp = floor(log2(n+1))
        p1 = 2**exp
        p2 = 2**(exp+1)
        step = n - p1 + 1
        chunksize = min(p2, self.count) - p1 + 1
        if step > chunksize // 2:
            if p2 > self.count:
                node = self.tail.prev
                step -= chunksize-1
            else:
                node = self.index[exp+1]
                step -= chunksize-1
            while -step > self.SKIPSIZE:
                if node.pskip is None:
                    break
                node = node.pskip
                step += self.SKIPSIZE
        else:
            node = self.index[exp]
            while step > self.SKIPSIZE:
                if node.nskip is None:
                    break
                node = node.nskip
                step -= self.SKIPSIZE
        return (node, step)

    def __getitem__(self, key):
        if key is slice:
            raise NotImplemented()

        n = int(key)
        if n < 0:
            n = self.count + n
        if n >= self.count or n < 0:
            raise IndexError()

        node, sp = self._select_index(n)
        # print([str(n) for n in self.index])
        # print(n, 'index', node, 'sup', sp)
        if sp < 0:
            for i in range(-sp):
                node = node.prev
        else:
            for i in range(sp):
                node = node.next

        return node.value


def run():
    n = int(input())
    deque = Deque()

    for _ in range(n):
        command = input()
        if command.startswith('0'):
            sub, val = [int(x) for x in command[2:].split()]
            if sub == 0:
                deque.push_left(val)
            elif sub == 1:
                deque.push(val)
            else:
                raise ValueError('subcommand of push')
        elif command.startswith('1'):
            i = int(command[2:])
            print(deque[i])
        elif command.startswith('2'):
            sub = int(command[2:])
            if sub == 0:
                deque.pop_left()
            elif sub == 1:
                deque.pop()
            else:
                raise ValueError('subcommand of pop')
        else:
            raise ValueError('invalid command')


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
from collections import deque

arr = deque()

for _ in range(int(input())):
    cmd = [int(x) for x in input().split()]

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

Submission status: Accepted

Solution code:
# 2018/06/02

from collections import deque

d = deque()
q = int(raw_input())

for i in range(q):
    query = [int(j) for j in raw_input().split()]
    if query[0] == 0:
        if query[1] == 0:
            d.appendleft(query[2])
        else:
            d.append(query[2])
    elif query[0] == 1:
        print  d[query[1]]
    elif query[0] == 2:
        if query[1] == 0:
            d.popleft()
        else:
            d.pop()

Submission status: Accepted

Solution code:
from collections import deque
 
d = deque()
q = int(raw_input())
 
for i in range(q):
    query = [int(j) for j in raw_input().split()]
    if query[0] == 0:
        if query[1] == 0:
            d.appendleft(query[2])
        else:
            d.append(query[2])
    elif query[0] == 1:
        print  d[query[1]]
    elif query[0] == 2:
        if query[1] == 0:
            d.popleft()
        else:
            d.pop()

Submission status: Accepted

Solution code:
from collections import deque

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

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

def pop(duque, d):
    if d == 1:
        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]))

Submission status: Accepted

Solution code:
# coding=utf-8

from collections import deque

A = deque([])
N = int(input())
for i in range(N):
    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:
        print(A[query[1]])
    elif query[0] == 2:
        if query[1] == 0:
            A.popleft()
        if query[1] == 1:
            A.pop()

Submission status: Accepted

Solution code:
# AOJ ITP2_1_B: Deque
# Python3 2018.6.24 bal4u

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 24 14:53:50 2018
ITP21B
@author: maezawa
"""

from collections import deque

d = deque()

n = int(input())

for i in range(n):
    c = list(map(int, input().split()))
    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]])
    else:
        if c[1] == 0:
            d.popleft()
        else:
            d.pop()

Submission status: Accepted

Solution code:
# AOJ ITP2_1_B: Deque
# Python3 2018.6.24 bal4u

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

Submission status: Accepted

Solution code:
from collections import deque
A = deque()
n = int(input())
for i in range(n):
    tmp = input().split()
#    print(tmp,A)
    if tmp[0] == "0" and tmp[1] == "0":
        A.appendleft(tmp[2])
    elif tmp[0] == "0" and tmp[1] == "1":
        A.append(tmp[2])
    elif tmp[0] == "1":
        print(A[int(tmp[1])])
    elif tmp[1] == "0":
        A.popleft()
    else:
        A.pop()

Submission status: Accepted

Solution code:
from collections import deque

Q = deque()
q=int(input())

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

Submission status: Accepted

Solution code:
from collections import deque

q = int(input())
A = deque()

for i in range(q):
    query = str(input())
    if query[0] == "0": #push
        if query[2] == "0":
            A.appendleft(int(query[4:]))
        else:
            A.append(query[4:])
    elif query[0] == "1": #randamAccess
        print(A[int(query[2:])])
    else: #pop
        if query[2] == "0":
            A.popleft()
        else:
            A.pop()

Submission status: Accepted

Solution code:
from collections import deque
A = deque()
ans = []


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


def randomAccess(p):
    global A, ans
    if A != []:
        ans.append(A[p])
    else:
        print('RandomAccessError')


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


def main():
    q = input()

    for i in range(int(q)):
        command = input().split()
        if len(command) == 3:
            query, operation, number = (int(x) for x in command)
            push(operation, number)
        else:
            query, number = (int(x) for x in command)
            if query == 1:
                randomAccess(number)
            else:
                pop(number)

    for i in range(len(ans)):
        print(ans[i])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque

A = deque()

Q = int(sys.stdin.readline().rstrip())
for _ in range(Q):
    s = sys.stdin.readline().rstrip().split()
    if s[0] == "0":  # pushback
        if s[1] == "0":
            A.appendleft(s[2])
        else:
            A.append(s[2])
    elif s[0] == "1":  # randomAccess
        print(A[int(s[1])])
    else:
        if s[1] == "0":
            A.popleft()
        else:
            A.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':  # pushBack
		if a[2] == '0':
			Q.appendleft(a[4:])
		else:
			Q.append(a[4:])
	elif a[0] == '1':
		print(Q[int(a[2:])])  # randomAccess
	else:  # popBack
		if a[2] == '0':
			Q.popleft()
		else:
			Q.pop()

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    q = int(input())
    ans = deque()
    for _ in range(q):
        q = [int(i) for i in input().split()]
        if q[0] == 2:
            if q[1] == 0:
                ans.popleft()
            else:
                ans.pop()
        else:
            if q[0] == 0:
                if q[1] == 0:
                    ans.appendleft(q[2])
                else:
                    ans.append(q[2])
            else:
                print(ans[q[1]])


resolve()

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
Q = deque()

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

Submission status: Accepted

Solution code:
from collections import deque

A = deque()

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