Buggy python submission (verdict: Time Limit Exceeded):
# -*- 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)


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
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)


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Time Limit Exceeded):
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)


Fixed submission:
# -*- 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()

Buggy python submission (verdict: Runtime Error):
# -*- 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])


Fixed submission:
# -*- 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()