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:
from collections import deque

S = input()
Q = int(input())
L = ""
R = ""
flag = 0

for i in range(Q):
    q = list(map(str , input().split()))
    if q[0] == "1":
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        if q[1] == "1":
            if flag == 0:
                L.append(q[2])
            else:
                R.append(q[2])
        else:
            if flag == 0:
                R.appned(q[2])
            else:
                L.append(q[2])
K = L[::-1] + R

if flag == 0:
    print("".join(K))
else:
    print("".join(K[::-1]))

Submission status: Runtime Error

Solution code:
ans = [str(input())]
n = int(input())
query = [input().split() for _ in range(n)] 
reverse = False
for i in query:
    if query[0] == 1:
		reverse = not reverse
    else:
		if reverse:
          ans.reverse()
        if query[1] == "1":
            ans.appendleft(query[2])
        else:
            ans.append(query[2])
if reverse:
    ans.reverse()
print("".join(ans))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
flag = 0b0
for i in range(q) :
    t = input()
    if t == 1 :
        flag ^= 1
        continue
    if (int(t[2]) - flag) == 1 :
        s = t[4] + s
    else :
        s = s + t[4]
if flag == 0 :
    print(s)
else :
    print(s[::-1])

Submission status: Runtime Error

Solution code:
a = input()
b = input()
c = input()
d=[]
alist = list(a)
for i in range(int(c)):
  e = input().split()
  d.append(e)
for i in range(int(c)):
  if d[i][0]=="1":
    l = len(alist)
    for x in range(int(l//2)):
      alist[x],alist[l-x-1] = alist[l-x-1],alist[x]
  else:
    if d[i][1] == "1":
      alist = [d[i][2]] + alist
    else:
      alist = alist + [d[i][2]]
le = len(alist)
answer =""
for i in range(le):
  answer = answer + alist[i]
print(answer)

Submission status: Runtime Error

Solution code:
import collections

x = collections.deque(input())
n = int(input())

for i in range(n):
    y = input()
    if len(y) == 1:
      x.reverse()
    else:
      if int(y[1]) == 2:
          x.append(y[2])
      else:
          x.appendleft(y[2])

print("".join(x))

Submission status: Runtime Error

Solution code:
B = input()
order = True
F = ""
for q in range(int(input())):
    T = input().rstrip().split()
    if T[0] == "1":
        order ^= True
    else:
        f,c = T[1],T[2]
        if not((f == "1") ^ order):
            F += c
        else:
            B += c
    print(F,B)
F = F[::-1]+B
if not order:
    F = F[::-1]
print(F)

Submission status: Runtime Error

Solution code:
import math
S = input()
Q = int(input())

lr = 'left'

def reverse_list(lr):
    if lr == 'right':
        return 'left'
    else:
        return 'right'

def moji_tuika(S, f, c, lr):
    if f == 1 and lr == 'left':
        return S.insert(0,c)
    elif f == 1 and lr == 'right':
        return S.append(c)
    elif f == 2 and lr == 'left':
        return S.append(c)
    else:
        return S.insert(0,c)
q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        q.append(temp)

S = list(S)
for i in q:
    if type(i) == int:
        lr = reverse_list(lr)
    elif int(i[0]) == 2:
        S = moji_tuika(S,int(i[1]),i[2],lr)

if lr == 'right':
    S.reverse()

S = ''.join(S)
print(S)

Submission status: Runtime Error

Solution code:
import math
S = input()
Q = int(input())

lr = 'left'

def reverse_list(lr):
    if lr == 'right'
        return 'left'
    else return 'right'

def moji_tuika(S, f, c, lr):
    if f == 1 and lr == 'left':
        return c + S
    elif f == 1 and lr == 'right':
        return S + c
    elif f == 2 and lr == 'left':
        return S + c
    else:
        return c + S
q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        q.append(temp)

for i in q:
    if type(i) == int:
        lr = reverse_list(lr)
    elif int(i[0]) == 2:
        S = moji_tuika(S,int(i[1]),i[2],lr)

if lr == 'right':
    S = list(S)
    S.reverse()
    S = ''.join(S)


print(S)

Submission status: Runtime Error

Solution code:
from collections import deque
S = list(map(str,input().split()))
Q = int(input())
Query = [list(map(str,input().split())) for _ in range(Q)]
d = deque(S)
for i in range(Q):
  if Query[i][0] == "1":
    d = d.rotate()
  elif Query[i][0] == "2":
    if Query[i][1] == "1":
      d.append(Query[i][2])
    elif Query[i][1] == "2":
      d.appendleft(Query[i][2])
ans = ''.join(d)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
S = list(map(str,input().split()))
Q = int(input())
Query = [list(map(str,input().split())) for _ in range(Q)]
d = deque(S)
for i in range(Q):
  if Query[i][0] == "1":
    d = ''.join(list(reversed(d)))
  elif Query[i][0] == "2":
    if Query[i][1] == "1":
      d.append(Query[i][2])
    elif Query[i][1] == "2":
      d.appendleft(Query[i][2])
ans = ''.join(d)
print(ans)

Submission status: Runtime Error

Solution code:
import math
S = input()
Q = int(input())

def reverse_list(S):
    S = ("")
    S.reverse()
    return ''.join(S)

def moji_tuika(S, f, c):
    if f == 1:
        return c + S
    else:
        return S + c

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        q.append(temp)

for i in q:
    if type(i) == int:
        S = reverse_list(S)
    elif i[0] == 2:
        S = moji_tuika(S,int(i[1]),i[2])

print(S)

Submission status: Runtime Error

Solution code:
import math
S = input()
Q = int(input())

def reverse_list(S):
    S = S.split("")
    S.reverse()
    return ''.join(S)

def moji_tuika(S, f, c):
    if f == 1:
        return c + S
    else:
        return S + c

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        t = int(temp[0])
        f = int(temp[1])
        c = temp[2]
        q.append((t,f,c))

for i in q:
    if type(i) == int:
        S = reverse_list(S)
    elif i[0] == 2:
        S = moji_tuika(S,i[1],i[2])

print(S)

Submission status: Runtime Error

Solution code:
import math
S = input()
Q = int(input())

def reverse_list(S):
    S = S.split("")
    S.sort()
    return ''.join(S)

def moji_tuika(S, f, c):
    if f == 1:
        return c + S
    else:
        return S + c

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        t = int(temp[0])
        f = int(temp[1])
        c = temp[2]
        q.append((t,f,c))

for i in q:
    if type(i) == int:
        S = reverse_list(S)
    elif i[0] == 2:
        S = moji_tuika(S,i[1],i[2])

print(S)

Submission status: Runtime Error

Solution code:
N, P = map(int, input().split())
d = list(map(int, input()))
ans =0
for k in range(0,N):
    for l in range(k+1,N+1):
        num = int("".join(map(str,d[k:l])))
        
        if num % P == 0:
            ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
   #include <bits/stdc++.h>
 
//    #include <boost/multiprecision/cpp_int.hpp>
 #define int long long
 #define inf  1000000007
 #define pa pair<int,int>
 #define ll long long
 #define pal pair<double,double>
 #define ppap pair<pa,int>
  #define PI 3.14159265358979323846
  #define paa pair<int,char>
  #define  mp make_pair
  #define  pb push_back
  #define EPS (1e-8)
                                          
    int dx[8]={0,1,0,-1,1,1,-1,-1};
    int dy[8]={1,0,-1,0,-1,1,1,-1};
                                            using namespace std;
                                   			class pa3{
                                            	public:
                                            	int x;
                                   				int y,z;
                                            	pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
                                            	bool operator < (const pa3 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		 return z<p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                   				bool operator > (const pa3 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		 return z>p.z;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa3 &p) const{
                                            		return x==p.x && y==p.y && z==p.z;
                                            	}
                                            		bool operator != (const pa3 &p) const{
                                            			return !( x==p.x && y==p.y && z==p.z);
                                            	}
                                            
                                            };
                                            
                                            class pa4{
                                            	public:
                                            	int x;
                                            	int y,z,w;
                                            	pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
                                            	bool operator < (const pa4 &p) const{
                                            		if(x!=p.x) return x<p.x;
                                            		if(y!=p.y) return y<p.y;
                                            		if(z!=p.z)return z<p.z;
                                            		return w<p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator > (const pa4 &p) const{
                                            		if(x!=p.x) return x>p.x;
                                            		if(y!=p.y) return y>p.y;
                                            		if(z!=p.z)return z>p.z;
                                            		return w>p.w;
                                            		//return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa4 &p) const{
                                            		return x==p.x && y==p.y && z==p.z &&w==p.w;
                                            	}
                                            		
                                            
                                            };
                                            class pa2{
                                            	public:
                                            	int x,y;
                                            	pa2(int x=0,int y=0):x(x),y(y) {}
                                            	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                            	pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
                                            	bool operator < (const pa2 &p) const{
                                            		return y != p.y ? y<p.y: x<p.x;
                                            	}
                                            	bool operator > (const pa2 &p) const{
                                            		return x != p.x ? x<p.x: y<p.y;
                                            	}
                                            	bool operator == (const pa2 &p) const{
                                            		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                            	}
                                            	bool operator != (const pa2 &p) const{
                                            		return !(abs(x-p.x)==0 && abs(y-p.y)==0);
                                            	}
                                            		
                                            
                                            };
                                            
 
                      
                                string itos( int i ) {
                                ostringstream s ;
                                s << i ;
                                return s.str() ;
                                }
                                 
                                int gcd(int v,int b){
                                	if(v==0) return b;
                                	if(b==0) return v;
                                	if(v>b) return gcd(b,v);
                                	if(v==b) return b;
                                	if(b%v==0) return v;
                                	return gcd(v,b%v);
                                }
                 
                            
                                int mod;
int extgcd(int a, int b, int &x, int &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int d = extgcd(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
pa operator+(const pa & l,const pa & r) {   
    return {l.first+r.first,l.second+r.second};                                    
}    
pa operator-(const pa & l,const pa & r) {   
    return {l.first-r.first,l.second-r.second};                                    
}  
                int beki(int wa,int rr,int warukazu){
                	if(rr==0) return 1%warukazu;
                	if(rr==1) return wa%warukazu;
                	wa%=warukazu;
                	if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu;
                	ll zx=beki(wa,rr/2,warukazu);
                	return (zx*zx)%warukazu;
                }
 
              /*                  
                int pr[700100];
                int inv[700010];
                

                
    			int comb(int nn,int rr){
    				if(rr<0 || rr>nn || nn<0) return 0;
    				int r=pr[nn]*inv[rr];
    				r%=mod;
    				r*=inv[nn-rr];
    				r%=mod;
    				return r;
    			}
               
                void gya(int ert){
                	pr[0]=1;
                	for(int i=1;i<=ert;i++){
                		pr[i]=((ll)pr[i-1]*i)%mod;
                	}
                		inv[ert]=beki(pr[ert],mod-2,mod);
                	for(int i=ert-1;i>=0;i--){
                		inv[i]=(ll)inv[i+1]*(i+1)%mod;
                	}
                }
                */
              //   cin.tie(0);
    		//	ios::sync_with_stdio(false);
    			//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;            
                 //sort(ve.begin(),ve.end(),greater<int>());
 //   mt19937(clock_per_sec);
  //  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ;

struct Segmax{
	//       1
	//   2        3
	// 4   5   6    7
	
	
	private:
	public:
	
	// (1<<15)=32768
	// 1<<16 = 65536
	// 1<<17 = 131072
	// 1<<18 = 262144
	
	int cor=(1<<18);
	
	vector<pa> vec;
	
	void shoki1(){
		
		vec.resize(2*cor+3, mp(0,0));
		for(int i=cor;i<2*cor;i++)vec[i].second=i-cor;
	}
		void shoki2(){
			for(int i=cor-1;i>0;i--) {
				if(vec[2*i].first>=vec[2*i+1].first) vec[i]=vec[2*i];
				else vec[i]=vec[2*i+1];
			}
		}
		
	void updadd(int x,int w){
		//x 項目に w加算
		x+=cor;
		vec[x].first+=w;
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first>=vec[2*x+1].first) vec[x]=vec[2*x];
			else vec[x]=vec[2*x+1];
		}
		
	}
	
	void updchan(int x,int w){
		//x項目をwに変更
		
		x+=cor;
		vec[x].first=w;
		
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first>=vec[2*x+1].first) vec[x]=vec[2*x];
			else vec[x]=vec[2*x+1];
		}
		
	}
	// [a,b)
	// k-th node
	// k no kukanha [l,r)
	pa segmax(int a,int b,int k=1,int l=0,int r=-10){
		if(r<0)r=cor;
		
	//	cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
		
		if(a<=l && r<=b){
			return vec[k];
		}
		if(r<=a || b<=l){
			return mp(-(1ll<<31),-1);
		}
		
		pa v1=segmax(a,b,k*2,l,(l+r)/2),v2=segmax(a,b,k*2+1,(l+r)/2,r);
		
		if(v1.first>=v2.first)return v1;
		else return v2;
	}
	
	// leftmost ブロックkは値がb以上のときに，このブロックに属する値がb以上のもののうち最も左のものを返す
	pa leftmost(int b, int k,int l,int r){
		if(r-l==1) return vec[k]; 
		if(vec[2*k].first>=b) return leftmost(b,2*k,l,(l+r)/2);
		else return leftmost(b,2*k+1,(l+r)/2,r);
		
	}
	

	
};


signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n;
	cin>>n;
	vector<pa> ve(n+1);
	for(int i=0;i<n;i++)cin>>ve[i].first>>ve[i].second;
	ve[n]=mp(inf*1000000ll,1);
	vector<int> dp(n+1);
	dp[n]=1;
	mod=998244353;
	sort(ve.begin(),ve.end());
	
	 Segmax ss;
	ss.shoki1();
	ss.shoki2();
	ss.updchan(n,n);
	for(int i=n-1;i>=0;i--){
		dp[i]=dp[i+1];
		
		int r=ve[i].first+ve[i].second;
		
		int it=lower_bound(ve.begin(),ve.end(),(pa)mp(r,-1))-ve.begin();
		ss.updchan(i,i);
		int e=ss.segmax(i,it).first;
	//	cout<<i<<" "<<it<<" "<<e<<endl;
		ss.updchan(i,e);
		dp[i]+=dp[e+1];
		dp[i]%=mod;
	}
	
cout<<dp[0]<<endl;
return 0;
 }

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
flg = 0
senntou = ''
ushiro = ''
for i in range(Q):
    A = list(input().split())
    if int(A[0]) == 1 and flg == 0:
        flg = 1
        continue
    elif int(A[0]) == 1 and flg == 1:
        flg = 0
        continue
    if (int(A[1]) == 1 and if flg == 0) or (int(A[1]) == 2 and if flg == 1):
        senntou = A[2] + senntou
    else:
        ushiro = ushiro + A[2]
if flg == 0:
    print(senntou + S + ushiro)
if flg == 1:
    print(ushiro[::-1] + S + senntou[::-1])

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
flg = 0
for i in range(Q):
    A = list(input().split())
    if int(A[0]) == 1 and flg == 0:
        flg = 1
        continue
    elif int(A[0]) == 1 and flg == 1:
        flg = 0
        continue
    if int(A[1]) == 1:
        if flg == 0:
            S.insert(0,A[2])
        else:
            S.append(A[2])
    else:
        if flg == 0:
            S.append(A[2])
        else:
            S.insert(0,A[2])
if flg == 0:
    print(S)
if flg == 1:
    print(S[::-1])

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                S.extendleft(B_)
                B = []
            elif len(B) == 1:
                S.extendleft(B)
                B = []
            if len(C) > 0:
                S.extend(C)
                C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            elif len(C) == 1:
                S.extendleft(C)
                C = []
            if len(B) > 0:
                S.extend(B)
                B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            S.extendleft(B_)
            B = []
        elif len(B) == 1:
            S.extendleft(B)
            B = []
        if len(C) > 0:
            S.extend(C)
            C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        elif len(C) == 1:
            S.extendleft(C)
            C = []
        if len(B) > 0:
            S.extend(B)
            B = []

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
# 2**nの繰り返し二乗法によるあまり計算
def mpow(a,n,mod):
  if n == 0:
    return 1
  elif n == 1:
    return a
  elif n%2==0:
    return (pow(a,n//2,mod))**2%mod
  else:
    return a*pow(a,n-1,mod)%mod
# 2項係数のあまり計算
def mfac(l,r,mod):
  ans = l
  for i in reversed(range(r,l)):
    ans *= i
    ans %= mod
  return ans
def mcomb(n,k,mod):
  A = mfac(n,n-k+1,mod)
  B = mfac(k,1,mod)
  B = mpow(B,mod-2,mod)
  return A*B%mod
 
def main():
  mod = 10**9+7
  n,a,b = map(int,input().split(" "))
  ans = mpow(2,n,mod)-1-mcomb(n,a,mod)-mcomb(n,b,mod)
  ans %= mod
  print(ans)
 
if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                S.extendleft(B_)
                B = []
            else:
                S.extendleft(B)
                B = []
            if len(C) > 0:
                S.extend(C)
                C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            else:
                S.extendleft(C)
                C = []
            if len(B) > 0:
                S.extend(B)
                B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            S.extendleft(B_)
            B = []
        else:
            S.extendleft(B)
            B = []
        if len(C) > 0:
            S.extend(C)
            C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        else:
            S.extendleft(C)
            C = []
        if len(B) > 0:
            S.extend(B)
            B = []

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                S.extendleft(B_)
                B = []
            else:
                S.extendleft(B)
                B = []
            S.extend(C)
            C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            else:
                S.extendleft(C)
                C = []
            S.extend(B)
            B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            S.extendleft(B_)
            B = []
        elif len(B)==1:
            S.extendleft(B)
            B = []
        S.extend(C)
        C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        elif len(C) == 1:
            S.extendleft(C)
            C = []
        S.extend(B)
        B = []

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                S.extendleft(B_)
                B = []
            else:
                S.extendleft(B)
                B = []
            S.extend(C)
            C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            else:
                S.extendleft(C)
                C = []
            S.extend(B)
            B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            S.extendleft(B_)
            B = []
        else:
            S.extendleft(B)
            B = []
        S.extend(C)
        C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        else:
            S.extendleft(C)
            C = []
        S.extend(B)
        B = []

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                S.extendleft(B_)
                B = []
            else:
                S.extendleft(B)
                B = []
            S.extend(C)
            C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            else:
                S.extendleft(C)
                C = []
            S.extend(B)
            B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            S.extendleft(B_)
            B = []
        else:
            S.extendleft(B)
            B = []
        S.extend(C)
        C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        else:
            S.extendleft(C)
            C = []
        S.extend(B)

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                S.extendleft(B_)
                B = []
            else:
                S.extendleft(B)
                B = []
            S.extend(C)
            C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            else:
                S.extendleft(C)
                C = []
            S.extend(B)
            B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            print(B_)
            S.extendleft(B_)
            B = []
        else:
            S.extendleft(B)
            B = []
        S.extend(C)
        C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        else:
            S.extendleft(C)
            C = []
        S.extend(B)

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
S.append(Str)
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                B_=B.reverse()
                print(B_)
                S.extendleft(B_)
                B = []
            else:
                S.extendleft(B)
                B = []
            S.extend(C)
            C = []
        else:
            if len(C)>1:
                C_=C.reverse()
                S.extendleft(C_)
                C = []
            else:
                S.extendleft(C)
                C = []
            S.extend(B)
            B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            B_=B.reverse()
            print(B_)
            S.extendleft(B_)
            B = []
        else:
            S.extendleft(B)
            B = []
        S.extend(C)
        C = []
    else:
        if len(C)>1:
            C_=C.reverse()
            S.extendleft(C_)
            C = []
        else:
            S.extendleft(C)
            C = []
        S.extend(B)

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Runtime Error

Solution code:
from collections import deque

s = input()
d = deque()
for i in s:
    d.append(i)
q = int(input())
for i in range(q):
    data = input().split()
    if len(data) == 1:
      if len(d) != 1:
        tmp1 = d.pop()
        tmp2 = d.popleft()
        d.appendleft(tmp1)
        d.append(tmp2)
      else:
        f = int(data[1])
        c = data[2]
        if f == 1:
          d.appendleft(c)
        else:
          d.append(c)
            
s = list(d)
s = "".join(s)
print(s)

Submission status: Runtime Error

Solution code:
s=input()
q= int(input())

for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		s=s[::-1]
	else:
		if t[1]=='1':
#			s=t[2]+s
            s='{}{}'.format(t[2],s)
#			s=f"{t[2]}{s}"
#			list1=[t[2],s]
#			s="".join(list1)
#			s="".join(t[2],s)
		else:
#			s=s+t[2]
            s='{}{}'.format(s,t[2])
#			s=f"{s}{t[2]}" 
#			list2=[s,t[2]]
#			s="".join(list2)
#			s="".join(s,t[2])

print (s)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())

f = ''
b = ''
num = 0
for i in range(q):
    a = input().split()
    if len(a) == 1:
        num ^= 1
        f,b = b,f
        print(f+s+b)
    else :
        if a[1] == '1' :
            f = a[2] + f
        elif a[1] == '2' :
            b = b + a[2]
        print(f+s+b)

if num:
    print(f[::-1]+s[::-1]+b)
else:
    print(f+s+b)