Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Runtime Error

Solution code:
class DoblyLinkedList:
    class node:
        def __init__(self, pre, val, nex):
            self.val = val
            self.pre = pre
            self.nex = nex

    def __init__(self, num):
        self.elements = [None for _ in range(100001)]
        self.elements[0] = self.node(None, num, None)
        self.top = 0
        self.bottom = 0
        self.cnt = 0

    def push_top(self, val):
        self.cnt += 1
        self.elements[self.top].pre = self.cnt
        node = self.node(None, val, self.top)
        self.elements[self.cnt] = node
        self.top = self.cnt

    def push_bottom(self, val):
        self.cnt += 1
        self.elements[self.bottom].nex = self.cnt
        node = self.node(self.bottom, val, None)
        self.elements[self.cnt] = node
        self.bottom = self.cnt

    @staticmethod
    def from_str(s):
        ret = DoblyLinkedList(s[0])
        for v in s[1:]:
            ret.push_bottom(v)
        return ret

    def to_str(self):
        cur = self.top
        val = self.elements[self.top].val
        while self.elements[cur].nex is not None:
            val += self.elements[self.elements[cur].nex].val
            cur = self.elements[cur].nex
        return val


def main():
    s = input()
    d = DoblyLinkedList.from_str(s)
    q = int(input())
    swapped = 0
    for _ in range(q):
        query = input()
        if len(query) == 1:
            swapped = 1 - swapped
            continue
        _, f, c = query.split()
        if swapped:
            if f == '1':
                d.push_bottom(c)
            else:
                d.push_top(c)
        else:
            if f == '1':
                d.push_top(c)
            else:
                d.push_bottom(c)

    if swapped:
        print(d.to_str()[::-1])
    else:
        print(d.to_str())


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest
import os
from collections import deque

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    que = deque()
    que.append(*list(input()))
    q = int(input())
    query_s = [list(input().split()) for i in range(q)]

    isReverse = False
    for query in query_s:
        if query[0] == "1":
            isReverse = True if not isReverse else False
        else:
            # 先頭に追加
            if (isReverse is False and query[1] == "1") or (isReverse and query[1] == "2"):
                que.appendleft(query[2])
            # 末尾に追加
            else:
                que.append(query[2])

    if isReverse:
        print("".join(list(reversed(que))))
    else:
        print("".join(que))


# パターン
# !isReverse 1(先頭)　= 先頭に追加
#  isReverse 1(先頭)　= 末尾に追加
# !isReverse 2(末尾)　= 先頭に追加
#  isReverse 2(末尾)　= 先頭に追加

# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
s=[]
r=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0 or int(qv[1])^t==2:
            r.append(qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    ss=s.copy()
    s=[]
    for j in range(len(ss)):
        s.append(ss[-1-j])
    ans=s
    ans.extend(r)
else:
    rr=r.copy()
    r=[]
    for j in range(len(rr)):
        r.append(rr[-1-j])
    ans=r
    ans.extend(s)
answer=''.join(ans)
print(answer)

Submission status: Runtime Error

Solution code:
S=input()
q=int(input())
s=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0 or nt(qv[1])^t==2:
            s.insert(0,qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    ss=s.copy()
    s=[]
    for j in range(len(ss)):
        s.append(ss[-1-j])
ans=''.join(s)
print(ans)

Submission status: Runtime Error

Solution code:
S=input()
q=int(input())
s=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0:
            s.insert(0,qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    for j in range(len(s)):
        ss.append(s[-1-j])
ans=''.join(s)
print(ans)

Submission status: Runtime Error

Solution code:
S=input()
q=int(input())
s=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if (qv[1]=='1' and t==1) or (qv[1]=='2' and t==0):
            s.insert(0,qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    for j in range(len(s)):
        ss.append(s[-1-j])
ans=''.join(s)
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
Q = int(input())
query = [input().split() for _ in range(Q)]

cnt = 0
for q in query:
    if q[0] == '1':
        cnt += 1
    else:
        reverse = cnt % 2
        if q[1] == '1':
            if reverse == 0:
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if reverse == 0:
                s = s + q[2]
            else:
                s = q[2] + s
    print(s)
if cnt % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque
s = input()
Q = int(input())
t = []
f = []
c = []
for i in range(Q):
    in_ = list(input().split())
    if in_[0] == "1":
        t.append(1)
        f.append(0)
        c.append(0)
    else:
        t.append(2)
        f.append(int(in_[1]))
        c.append(in_[2])
acc = [0 for _ in range(Q)]
if t[-1] == 1:  acc[-1] = 1
for i in reversed(range(Q - 1)):
    if t[i] == 1:
        acc[i] = acc[i + 1] + 1
    else:
        acc[i] = acc[i + 1]
        
if acc[0] % 2 == 0:
    ans = deque(S)
else:
    ans = deque(S[::-1])
    
for i in range(Q):
    if t[i] == 2:
        if (f[i] + acc[i]) % 2 == 1:
            ans.appendleft(c[i])
        else:
            ans.append(c[i])
print("".join(ans))

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
P = 0
for _ in range(Q):
    q = list(input().split())
    if q[0]=="1":
        P += 1
        P = P%2
    else:
        c = q[2]
        if q[1]=="1":
            if P==0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if P==0:
                S.append(c)
            else:
                S.appendleft(c)
if P==1:
    S = S[::-1]
print("".join(S))

Submission status: Runtime Error

Solution code:
import collections

n=int(input())
arr=list(map(int,input().split()))
cnt=collections.defaultdict(int)
for val in arr:
  cnt[val]+=1
sums=sum(arr)
q=int(input())
for _ in range(q):
  b,c=map(int,input().split())
  diff=(c-b)*(cnt[b])
  sums+=diff
  print(sums)
  cnt[c]+=cnt[b]
  cnt[b]=0

Submission status: Runtime Error

Solution code:
import collections

n=int(input())
arr=list(map(int,input().split()))
cnt=collections.defaultdict(int)
for val in arr:
  cnt[val]+=1
sums=sum(arr)
q=int(input())
for a in range(q):
  b,c=map(int,input().split())
  diff=(c-b)*(cnt[b])
  sums+=diff
  print(sums)
  cnt[c]+=cnt[b]
  cnt[b]=0

Submission status: Runtime Error

Solution code:
s = input()
q = int(ionput())
h,t = [],[]
p = 0

for i in range(q):
    c = input().split()
    if c[0] =='1':
        h,t = t,h
        p = 1-p
    else:
        if c[1] =='1':
            h.append(c[2])
        else:
            t.append(c[2])
if p ==1:
    s = s[::-1]
print("".join(h[::-1])+s+"".join(t))

Submission status: Runtime Error

Solution code:
s = inmput()
q = int(ionput())
h,t = [],[]
p = 0

for i in range(q):
    c = input().split()
    if c[0] =='1':
        h,t = t,h
        p = 1-p
    else:
        if c[1] =='1':
            h.append(c[2])
        else:
            t.append(c[2])
if p ==1:
    s = s[::-1]
print("".join(h[::-1])+s+"".join(t))

Submission status: Runtime Error

Solution code:
s=str(input())
s=list(s)
q=int(input())
sb=[0]*q
sa=[0]*q
sbi=0
sai=0
ta=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    ta=ta+1
  else:
    if (temp[2]=="1" and ta%2==0) or (temp[2]=="2" and ta%2!=0):
      sb[sbi]=temp[4]
      sbi=sbi+1
    else:
      sa[sai]=temp[4]
      sai=sai+1
lsb=sb.index(0)
lsa=sa.index(0)
sa.reverse()
ans=sb[0:lsb]+s+sa[len(sa)-lsa:len(sa)]
if ta%2!=0:
  ans.reverse()
print("".join(ans))

Submission status: Runtime Error

Solution code:
S = str(input())
Q = int(input())
t = 0

for _ in range(Q):
    A = list(input().split())
    if A[0] == "1":
        if t == 0:
            t = 1
        else:
            t = 0

    elif (A[1] == "1" and t == 0) or (A[1] == "2" and t == 1):
        S = A[2] + S
    else:
        S = S + A[2]

if t == 1:
    S = s[::-1]

print(S)

Submission status: Runtime Error

Solution code:
print(xy)

Submission status: Runtime Error

Solution code:

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def query2(s,f,c,ct):
    if ct%2==1:
        s.reverse()
    
    if f=='1':
        s=c+s
    else:
        s=s+c
    
    
    #print(s)
    return s
    
def do():
    S=STR()
    q=INT()
    ct=0
    for i in range(q):
        qt=LISTS()
        if qt[0]=='1':
            ct+=1
        else:
            S=query2(S,qt[1],qt[2],ct)
            ct=0
    
    if ct%2==1:
        S.reverse()
    
    print(S)
    
if __name__ == '__main__':
    do()

Submission status: Runtime Error

Solution code:
import sys
s = str(input())
Q = int(input())
q = sys.stdin.readlines()
for i in q:
  if i[0] == 1:
    s = alpha[::-1]
  elif i[1] == 1:
    s.appendleft(i[2])
  else:
    s.append(i[2])
print(s)

Submission status: Runtime Error

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

rev = 0

for i in range(N):
  Q = input().split()
  if Q[0] == "1":
    rev += 1
    rev %= 2
  else:
    if (rev + int(Q[1])) % 2 == 1:
      S.appendleft(Q[2])
    else:
      S.append(Q[2])
if rev == 1:
  S = S[::-1]
print("".join(S))

Submission status: Runtime Error

Solution code:
a = inpuut()
num = int(input())
b = [input().split() for i in range(num)]
for f in b:
  if f[0] == "1":
    a = a[::-1]
  else:
    if f[1] == "1":
      a = f[2] + a
    else:
      a = a + f[2]
print(a)

Submission status: Runtime Error

Solution code:
from collections import deque

S = input()
B = deque()
res = 0
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    A = list(map(str, input().split()))
    if int(A[0]) == 1:
        if res == 0:
            res = 1
        else:
            res = 0
    else:
        if int(A[1]) == 1:
            if res == 0:
                B.appendleft(A[2])
            else:
                B.append(A[2])
        else:
            if res == 0:
                B.append(A[2])
            else:
                B.appendleft(A[2])
if res == 0:
    print(''.join(B))
else:
    B = B[::-1]
    print(''.join(B))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import deque

S = input().rstrip()
S = deque(S)
Q = int(input())
Que = (input().rstrip() for _ in range(Q))

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if elem[0] == '1':
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S.appendleft(elem[4])
    elif s == 2 and flag == 0:
      S.append(elem[4])
    elif s == 1 and flag == 1:
      S.append(elem[4],0)
    else:
      S.appendleft(elem[4])

if flag == 1:
  S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
#D
s_list=list(input())
q=int(input())
r=0
sl_list=[]
for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            sl_list.append(ope[2])
        else:
            s_list.append(ope[2])
s_list=sl_list.reverse() + s_list
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
rev=False
count=0
while(q):
    q-=1
    t=list(input().split())
    if t[0]=='2':
        f=t[1]
        if rev and count%2==1:
            if f=='1':
                f='2'
            else:
                f='1'
        c=t[2]
        if f=='1':
            s=c+s
        else:
            s=s+c
    else:
        count+=1
        rev=True
    print(s)
if count%2!=0:
    s=s[::-1]
print(s)

Submission status: Runtime Error

Solution code:
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput()
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt:
        if f == "1": s = s + c
        if f == "2": s = c + s
    else:
        if f == "1": s = c + s
        if f == "2": s = s + c
    #print(s)

s = s[::-1] if cnt else s
print(s)

Submission status: Runtime Error

Solution code:
#import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput()
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt:
        if f == "1": s = s + c
        if f == "2": s = c + s
    else:
        if f == "1": s = c + s
        if f == "2": s = s + c
    #print(s)

s = s[::-1] if cnt else s
print(s)

Submission status: Runtime Error

Solution code:
S: str = input()
Q = int(input())

ANS = S

for _ in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        ANS = ANS[::-1]
    else:
        f, c = query[1:]
        if f == '1':
            ANS = c + ANS
        else:
            ANS = ANS + c

print(ANS)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
    ans = list(input().rstrip())
    n = int(input())
    li = [input().split() for i in range(n)]
    a = 0 #先頭がどっちかという情報

    for x in li:
        if x[0] == "1": #反転クエリ
            a = (a+1)%2
        else: #追加クエリ
            if (x[1] == "1" and a == 0) or (x[1] == "2" and a == 1):
                ans = x[2] + ans
            else:
                ans += x[2]

    if a == 0: #先頭が前なら
        print(ans)
    else: #先頭が後なら
        print(ans[::-1])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput()
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    print("====", t,f,c)
    if cnt:
        if f == 1: s = s + c
        if f == 2: s = c + s
    else:
        if f == 1: s = c + s
        if f == 2: s = s + c
    print(s)

s = s[::-1] if cnt else s
print(s)

Submission status: Runtime Error

Solution code:
S = str(input())
Q = int(input())
List = [list(map(str, input().split())) for i in range(Q)]

#print(List)

#S='a'
#Q=4
#List=[['2', '1', 'p'], ['1'], ['2', '2', 'c'], ['1']]
han=0
b=''
for a in List:
    if a[0]=='1':
        han+=1
    if a[0]=='2':
        if (a[1]=='1' and han%2==0) or (a[1]=='2' and han%2==1):
            b=+a[2]
        if (a[1]=='2' and han%2==0) or (a[1]=='1' and han%2==1):
            S=S+a[2]
            
if han%2==0:
    print(b[::-1]+S)
else:
    print(S[::-1]+b)

Submission status: Runtime Error

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


s = S()
q = I()
queries = SR(q)
for query in queries:
    print(query[0])
    if query[0] == '1':
        if len(s) <= 1:
            continue
        s = s[-1] + s[1:-1] + s[0]
    else:
        _, f, c = query.split()
        if f == '1':
            s = c + s
        else:
            s = s + c
    print(f'{s=}')
print(s)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
a = []
# for j in range(q):
#     a.extend([list(map(str,input().split()))])
# # print(a)
# for i in a:
for j in range(q):
    i = map(str,input().split())
    if i ==["1"]:
        s = s[::-1]
    elif i[0]=="2" and i[1]=="1":
        s = i[2]+s
    elif i[0]=="2" and i[1]=="2":
        s = s +i[2]
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque
S=deque(list(input()))
Q=int(input())
l=list(input().split() for i in range(Q))
a=0
for j in l:
  if j[0]=="1":
      a+=1
  else:
    if j[1]=="1":
        if a %2==0:
            S.appendleft(j[2])
        else:
            S.append(j[2])
    else:
        if a %2==1:
            S.appendleft(j[2])
        else:
            S.append(j[2])
        
if a%2==0:
    print("".join(S))
else:
    S=S[::-1]
    print("".join(S))

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
counter =0

for q in range(Q):
    inpu_ = input()
    if len(inpu_)==1:
        counter += 1
    else:
        a,f,c = map(str,inpu_.split())
        if f=="1":
            if counter%2 == 1:
                S = S+c
            else:
                S = c+S

        else:
            if counter%2==1:
                S = c+S
            else: 
                S = S+c

if counter%2==1:
    S.reverse()
print(S)

Submission status: Runtime Error

Solution code:
from collections import deque

S = input()
Q = int(input())

q = deque()
rev = 0

for s in S:
  q.append(s)

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '1' and rev:
      q.append(C)
    else:
      q.appendleft(C)
      
ans = ''.join(q)

if reverse:
  print(ans[::-1])
else:
  print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

S = input()
Q = int(input())

q = deque()
rev = 0

for s in S:
  q.append(S)
  
for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if F == '1' and rev:
      q.append(C)
    else:
      q.appendleft(C)
      
if rev:
  q.reverse()

print(''.join(q))

Submission status: Runtime Error

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
Queries = [input().split() for _ in range(Q)]

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not need_reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])

print(S if not need_reverse else S[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque

S = input()
Q = int(input())
q = deque([S])
reverse = -1

for i in range(Q):
    print(reverse)
    X = input().split()
    if X[0] == "1":
        reverse *= -1
    # if X[1] == "1":
    elif X[1] == "1":
        if reverse == -1:
            q.appendleft(X[2])
        else:
            q.append(X[2])
    elif X[1] == "2":
        if reverse == -1:
            q.append(X[2])
        else:
            q.appendleft(X[2])
    print(reverse, q)
if reverse == 1: q.reverse()

for i in range(len(q)):
    print(q[i],end="")
print()

Submission status: Runtime Error

Solution code:
from collections import deque
import sys

S = input()
Q = int(input())
q = deque([S])
reverse = False

for i in range(Q):
    X = sys.stdin.readline().split()
    if X[0] == "1":
        if reverse:
            reverse = False
        else:
            reverse = True
    elif X[0] == "2":
        if reverse:
            print(reverse)
            X[1] = str(3-int(X[1]))
        if X[1] == "1":
            q.appendleft(X[2])
        else:
            q.append(X[2])
        print(reverse, X[1], q)

if reverse: q.reverse()

for i in range(len(q)):
    print(q[i],end="")
print()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
  string s;
  cin >> s;
  string t;
  int q;
  cin >> q;
  rep(qi,q){
    int ty;
    cin >> ty;
    if (ty == 1){
      swap(s,t);
    }else{
      int f; char c;
      cin >> f >> c;
      if (f == 1){
	t += c;
      }else{
	s+=c;
      }}
  }
  reverse(t.begin(), t.end());
  t += s;
  cout << t << endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
from collections import deque
s = input()
q = int(input())
former = deque()
latter = deque()

flg = False
for i in range(q):
    p = list(map(str, input().split()))
    if p[0] == "1":
        flg = True if flg == False else False
        continue
    else:
        if (p[1] == "1" and not flg) or (p[1] == "2" and flg):
            former.appendleft(p[2])
        else:
            latter.append(p[2])

ans = former + deque(s) + latter
ans = "".join(ans)
print(ans if not flg else ans[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
cnt = 0
for i in range(Q):
    q = list(input().split())
    if len(q)==1:
        cnt += 1
    else:
        F = int(q[1])
        C = q[2]
        if cnt%2==0:
            if F==1:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F==1:
                S.append(C)
            else:
                S.appendleft(C)
if cnt%2==0:
    print("".join(S))
else:
    S = S[::-1]
    print("".join(S))

Submission status: Runtime Error

Solution code:
S = str(input())
Q = int(input())

#Que = [list(map(str, input().split())) for _ in range(Q)]

for i in range(Q):
    flag, F, C = map(str, input().split())

    if flag == '1':
        S = S[::-1]

    if flag == '2':
        if F == '1':
            S = C + S
        if F == '2':
            S = S + C


print(S)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
head = []
tail = []
rev=False
for _ in range(q):
  tmp = list(input.split())
  if tmp[0] == '1'
  	if rev == False:
      rev == True
    else:
      rev == False
  if tmp[0] == '2':
    if tmp[1] == '1':
      if rev == False:
        head.append(tmp[2])
      if rev == True:
        tail.append(tmp[2])
    if tmp[1] == '2':
      if rev == False:
        tail.append(tmp[2])
      if rev == True:
        head.append(tmp[2])
head = ''.join(map(str,head))
tail = ''.join(map(str,tail))

ans=head[::-1]+s+tail 
if rev==True:
 ans=ans[::-1]
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
s = input()
q = int(input())
que = deque([s])
flg=False
for _ in range(q):
    a = list(input().split())
    if a[0]=='1':
        flg=not(flg)
#        s=s[::-1]
    else:
        if flg ^ a[1]!='1':
            que.append(a[2])
        else:
            que.appendleft(a[2])

ans = ''.join(que)

if flg:
    ans = ans[::-1]
print(ans)

Submission status: Runtime Error

Solution code:
import math

s = list(input())
q = int(input())

mae = []
usi = []
k = 0
m = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
    if(a[0] == '2'):
        if(k %2 == 0):
            if(a[1] == '1'):
                mae.appendleft(a[2])
            else:
                usi.append(a[2])
        else:
            if(a[1] == '1'):
                usi.append(a[2])
            else:
                mae.appendleft(a[2])

if(k % 2 == 1):
    mae = mae[::-1]
    usi = usi[::-1]
    s = s[::-1]
    s = ''.join(usi+s+mae)
else:
    s = ''.join(mae+s+usi)
print(s)

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input()))
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            S.appendleft(t[2])
        else:
            S.append(t[2])
if r:
    print(''.join(S[::-1]))
else:
    print(''.join(S))

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
C = collections.Counter(A)

ans = 0
for i in C.values():
    ans += int(0.5* i * (i-1))
for j in A:
    print(ans-C[j]+1)

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])
if not flag:
    s = s[::-1]

print(''.join(s))

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
parity = 0

for i in range(Q):
    query = input().split()
    if query == '1':
        parity += 1
    else:
        if int(query[1]) == 1 + (parity % 2):
            S = query[2] + S
        else:
            S = S + query[2]

if parity % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
Query = [list(map(str,input().split())) for _ in range(Q)]
flag = True
print(Query)
for i in Query:
    if i[0]=="1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if (flag==True and i[1]=="1") or (flag==False and i[1]=="2"):
            f += i[1]
        elif (flag=True and i[1]=="2") or (flag==False and i[1]=="1":
            l += i[1]
S = f+S+l
if flag:
    print(S)
else:
    print(S[::-1])

Submission status: Runtime Error

Solution code:
s = input()

n = int(input())
l = []

base = 1

for i in range(n):
  l=list(input().split())
  if l[0] == '1':
    base = base *-1
  else:
    if l[1] =='1' and base == 1 :
      s = l[2]+s
    elif l[1] =='2' and base == 1 :
      s = s+l[2]
    elif l[1] =='1' and base == -1 :
      s = s+l[2]
    elif l[1] =='2' and base == -1 :
      s = l[2]+s

if base ==-1:
  s=''.join(list(reversed(org_str)))
      
print(s)

Submission status: Runtime Error

Solution code:
import numpy as np

S = input()
Q = int(input())
q = np.array([[i for i in input().split()] for j in range(Q)])

for i in range(Q):
    if q[i,0] == "1":
        S = S[::-1]
    elif q[i,0] == "2":
        if q[i,1] == "1":
            S = q[i,2] + S
        else:
            S = S + q[i,2]
            
print(S)