Submission #56993655
Source Code Expand
class DoubleListDeque:
__slots__ = ("front", "back")
def __init__(self, init_arr: list = None) -> None:
init_arr = init_arr or []
mid = len(init_arr)
self.front = init_arr[:mid][::-1]
self.back = init_arr[mid:]
def _rebalance(self) -> None:
source, target = (
(self.front, self.back) if not self.back else (self.back, self.front)
)
mid = len(source) >> 1
target.extend(source[: mid + 1][::-1])
del source[: mid + 1]
def append(self, val) -> None:
self.back.append(val)
def appendleft(self, val) -> None:
self.front.append(val)
def pop(self):
if not self:
raise IndexError("pop from empty deque")
if not self.back:
self._rebalance()
return self.back.pop()
def popleft(self):
if not self:
raise IndexError("popleft from empty deque")
if not self.front:
self._rebalance()
return self.front.pop()
def __getitem__(self, i: int):
l = len(self)
if i < -l or l <= i:
raise IndexError("deque index out of range")
i = i if i >= 0 else i + l
if i < len(self.front):
return self.front[~i]
else:
return self.back[i - len(self.front)]
def __setitem__(self, i: int, val):
l = len(self)
if i < -l or l <= i:
raise IndexError("deque index out of range")
i = i if i >= 0 else i + l
if i < len(self.front):
self.front[~i] = val
else:
self.back[i - len(self.front)] = val
def __len__(self) -> int:
return len(self.front) + len(self.back)
def __str__(self) -> str:
return f"Deque({self._tolist()})"
def _tolist(self) -> list:
return self.front[::-1] + self.back
import sys
def input():
return sys.stdin.readline().strip()
Q: int = int(input())
deq = DoubleListDeque()
for _ in range(Q):
qt, x = map(int, input().split())
if qt == 1:
deq.appendleft(x)
elif qt == 2:
deq.append(x)
else:
print(deq[x - 1])
Submission Info
| Submission Time | |
|---|---|
| Task | 061 - Deck(★2) |
| User | Alumite14 |
| Language | Python (PyPy 3.10-v7.3.12) |
| Score | 2 |
| Code Size | 2245 Byte |
| Status | AC |
| Exec Time | 100 ms |
| Memory | 85932 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 2 / 2 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt, sample_02.txt, sample_03.txt |
| All | sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| sample_01.txt | AC | 57 ms | 76220 KiB |
| sample_02.txt | AC | 57 ms | 76356 KiB |
| sample_03.txt | AC | 57 ms | 76024 KiB |
| subtask_1_1.txt | AC | 83 ms | 85640 KiB |
| subtask_1_10.txt | AC | 98 ms | 85772 KiB |
| subtask_1_11.txt | AC | 90 ms | 83644 KiB |
| subtask_1_12.txt | AC | 100 ms | 85712 KiB |
| subtask_1_13.txt | AC | 99 ms | 85812 KiB |
| subtask_1_14.txt | AC | 97 ms | 85688 KiB |
| subtask_1_15.txt | AC | 98 ms | 85684 KiB |
| subtask_1_16.txt | AC | 98 ms | 85932 KiB |
| subtask_1_2.txt | AC | 96 ms | 84520 KiB |
| subtask_1_3.txt | AC | 91 ms | 82976 KiB |
| subtask_1_4.txt | AC | 81 ms | 85576 KiB |
| subtask_1_5.txt | AC | 95 ms | 84356 KiB |
| subtask_1_6.txt | AC | 91 ms | 82920 KiB |
| subtask_1_7.txt | AC | 91 ms | 84232 KiB |
| subtask_1_8.txt | AC | 90 ms | 84312 KiB |
| subtask_1_9.txt | AC | 78 ms | 81528 KiB |