Problem:

Stack

  Stack is a container of elements that are inserted and deleted according to LIFO (Last In First Out).

  For $n$ stack $S_i$ ($i = 0, 1, ..., n-1$), perform a sequence of the following operations.

  
push($t$, $x$): Insert an integer $x$ to $S_t$.

  
top($t$): Report the value which should be deleted next from $S_t$. If $S_t$ is empty, do nothing.

  
pop($t$): Delete an element from $S_t$. If $S_t$ is empty, do nothing.

In the initial state, all stacks are empty.

Input

  The input is given in the following format.

$n \; q$
$query_1$
$query_2$
:
$query_q$

Each query $query_i$ is given by

0 $t$ $x$

or

1 $t$

or

2 $t$

  where the first digits 0, 1 and 2 represent push, top and pop operations respectively.

Output

  For each top operation, print an integer in a line.

Constraints

  
$1 \leq n \leq 1,000$

  
$1 \leq q \leq 200,000$

  
$-1,000,000,000 \leq x \leq 1,000,000,000$

Sample Input 1

3 9
0 0 1
0 0 2
0 0 3
0 2 4
0 2 5
1 0
1 2
2 0
1 0

Sample Output 1

3
5
2



Sample input:
3 9
0 0 1
0 0 2
0 0 3
0 2 4
0 2 5
1 0
1 2
2 0
1 0


Sample output:
3
5
2

Submission status: Accepted

Solution code:
a = [int(i) for i in input().split()]
list = []
for i in range(a[0]): list.append([])

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

    if cmd[0] == 0:
        list[cmd[1]].append(cmd[2])

    elif cmd[0] == 1:
        if list[cmd[1]] != []: print(list[cmd[1]][-1])

    elif cmd[0] == 2:
        if list[cmd[1]] != []: list[cmd[1]].pop()

Submission status: Accepted

Solution code:
n, q = list(map(int, input().split(' ')))
stacks = [[] for i in range(n)]

for i in range(q):
    op = list(map(int, input().split(' ')))
    if op[0] == 0:
        stacks[op[1]].append(op[2])
    elif op[0] == 1:
        if len(stacks[op[1]]) != 0:
            print(stacks[op[1]][-1])
    elif op[0] == 2:
        if len(stacks[op[1]]) != 0:
            stacks[op[1]].pop()

Submission status: Accepted

Solution code:

class Stack:
    def __init__(self):
        self.stack = []

    def push(self, v):
        self.stack.append(v)

    def top(self):
        if len(self.stack) == 0:
            return None
        else:
            return self.stack[-1]

    def pop(self):
        if len(self.stack) == 0:
            return None
        else:
            return self.stack.pop()


def run():
    n, q = [int(i) for i in input().split()]
    li = [Stack() for _ in range(n)]

    for _ in range(q):
        command = input()
        if command.startswith('0'):
            t, v = [int(i) for i in command[2:].split()]
            li[t].push(v)
        elif command.startswith('1'):
            t = int(command[2:])
            v = li[t].top()
            if v is not None:
                print(v)
        elif command.startswith('2'):
            t = int(command[2:])
            li[t].pop()
        else:
            raise ValueError('invalid command')


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
# coding=utf-8

N, Q = map(int, input().split())
S = [[] for i in range(N)]

for j in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 0:
        S[query[1]].append(query[2])
    elif query[0] == 1:
        if S[query[1]]:
            print(S[query[1]][-1])
    elif query[0] == 2:
        if S[query[1]]:
            S[query[1]].pop()

Submission status: Accepted

Solution code:
n, q = list(map(int, input().split(' ')))
stacks = [[] for i in range(n)]
 
for i in range(q):
    op = list(map(int, input().split(' ')))
    if op[0] == 0:
        stacks[op[1]].append(op[2])
    elif op[0] == 1:
        if len(stacks[op[1]]) != 0:
            print(stacks[op[1]][-1])
    elif op[0] == 2:
        if len(stacks[op[1]]) != 0:
            stacks[op[1]].pop()

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    n, q = input().split()
    n, q = int(n), int(q)
    S = [deque([]) for i in range(n)]

    for i in range(q):
        query = input().split()

        if query[0] == '0':
            S[int(query[1])].append(query[2])

        else:
            if len(S[int(query[1])]) == 0:
                pass
            elif query[0] == '1':
                print(S[int(query[1])][-1])
            else:
                S[int(query[1])].pop()

Submission status: Accepted

Solution code:
# AOJ TP2_2_A: Stack
# Python3 2018.6.24 bal4u

from collections import deque
n, q = map(int, input().split())
Q = []
for i in range(n): Q.append(deque())
for i in range(q):
	a = input().split()
	t = int(a[1])
	if   a[0] == '0': Q[t].append(a[2])        # push
	elif a[0] == '1' and Q[t]: print(Q[t][-1]) # top
	elif a[0] == '2' and Q[t]: Q[t].pop()      # pop

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 24 18:23:54 2018
ITP22A
@author: maezawa
"""

n, q = list(map(int, input().split()))
a = [[] for _ in range(n)]

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

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
A = [[] for i in range(n)]
for i in range(q):
    tmp = input().split()
    tmp[1] = int(tmp[1])
    if tmp[0] == "0":
        A[tmp[1]].append(tmp[2])
    elif tmp[0] == "1" and len(A[tmp[1]]) != 0:
        print(A[tmp[1]][-1])
    elif tmp[0] == "2" and len(A[tmp[1]]) != 0:
        A[tmp[1]].pop()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
n = int(sys.stdin.readline().split()[0])
A = defaultdict(deque)
ans = []
for query in sys.stdin:
    if query[0] == '0':
        t, x = query[2:].split()
        A[t].append(x)
    elif query[0] == '1':
        if A[query[2: -1]]:
            ans.append(A[query[2:-1]][-1] + '\n')
    else:
        if A[query[2: -1]]:
            A[query[2: -1]].pop()

sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    n, Q = [int(i) for i in input().split()]
    ans = [deque() for _ in range(n)]
    for _ in range(Q):
        q = [int(i) for i in input().split()]
        if q[0] == 0:
            ans[q[1]].append(q[2])
        elif q[0] == 1:
            if len(ans[q[1]]) > 0:
                print(ans[q[1]][-1])
        else:
            if len(ans[q[1]]) > 0:
                ans[q[1]].pop()


resolve()

Submission status: Accepted

Solution code:
from collections import deque

def push(t, x):
    global Slist
    Slist[t].append(x)

def top(t):
    global Slist
    if Slist[t] != deque():
        print(Slist[t][-1])

def pop(t):
    global Slist
    if Slist[t] != deque():
        Slist[t].pop()
    


n, q = map(int, input().split())
Slist =[]
for i in range(n):
    S = deque()
    Slist.append(S)

for j in range(q):
    queryi = list(map(int, input().split()))
    
    if queryi[0] == 0:
        push(queryi[1], queryi[2])

    elif queryi[0] == 1:
        top(queryi[1])

    else: 
        pop(queryi[1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	stack
# CreatedDate:  2020-05-22 23:23:23 +0900
# LastModified: 2020-05-22 23:46:42 +0900
#


import os
import sys
#import numpy as np
#import pandas as pd


def main():
    n,q = map(int,input().split())
    stack = [[] for _ in range(n)]
    for _ in range(q):
        order = list(map(int,input().split()))

        if order[0]==0:
            stack[order[1]].append(order[2])
        elif order[0]==1:
            if len(stack[order[1]])!=0:
                print(stack[order[1]][-1])
        else:
            if len(stack[order[1]])!=0:
                stack[order[1]].pop(-1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
class Stack:

  def __init__(self):
    self.S = list()

  #0:insert
  def push(self,x):
    self.S.append(x)

  #1:top
  def top(self):
    if len(self.S) > 0:
      print(self.S[-1])
      
  #2:erase
  def pop(self):
    if len(self.S) > 0:
      self.S.pop()

n,q = map(int,input().split())
SList = list()

for i in range(n):
  SList.append(Stack())

for i in range(q):
  cmd = input()
  
  if cmd.startswith("0"):
    c,t,x = map(int,cmd.split(" "))
    SList[t].push(x)
    
  if cmd.startswith("1"):
    c,t = map(int,cmd.split(" "))
    SList[t].top()
  
  if cmd.startswith("2"):
    c,t = map(int,cmd.split(" "))
    SList[t].pop()

Submission status: Accepted

Solution code:

import sys
from collections import deque
n,q = map(int, input().split())
S = []
for i in range(n):
    S.append(deque())

for i in range(q):
    l = list(map(int,input().split()))
    if l[0] == 0:
        S[l[1]].appendleft(l[2])
    elif l[0] == 1:
        if not S[l[1]]:
            continue
        else:
            print(S[l[1]][0])
    elif S[l[1]]:
        S[l[1]].popleft()

Submission status: Accepted

Solution code:
from collections import deque

n,q = map(int,input().split())

S = deque([deque() for _ in range(n)])
for i in range(q):
    query = tuple(map(int,input().split()))

    if query[0] == 0:
        S[query[1]].append(query[2])
    elif query[0] == 1:
        if len(S[query[1]]) != 0:
            print(S[query[1]][-1])
    else:
        if len(S[query[1]]) != 0:
            S[query[1]].pop()

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
stacks = [[] for i in range(n)]
while q:
    q -= 1
    s = [*map(int, input().split())]
    if s[0] == 0:
        stacks[s[1]].append(s[2])
    elif s[0] == 1 and len(stacks[s[1]]):
        print(stacks[s[1]][-1])
    elif len(stacks[s[1]]):
        stacks[s[1]].pop()

Submission status: Accepted

Solution code:
from collections import deque
Q =[]
n,q = map(int,input().split())

for i in range(n):
    Q.append(deque())
for i in range(q):
    a = input().split()
    if a[0] == "0":
        Q[int(a[1])].append(a[2])
    elif a[0] == "1":
        if Q[int(a[1])]:
            print(Q[int(a[1])][-1])
    else:
        if Q[int(a[1])]:
            Q[int(a[1])].pop()

Submission status: Accepted

Solution code:
from collections import deque

def main():
    n,m = map(int,input().split())
    dq = []
    for _ in range(n):dq.append(deque())
    for _ in range(m):
        q = list(map(int,input().split()))
        if q[0] == 0:dq[q[1]].append(q[2])
        elif dq[q[1]]:
            if q[0] == 1:
                print(dq[q[1]][len(dq[q[1]])-1])
            else        :dq[q[1]].pop()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

# AOJ TP2_2_A: Stack
# Python3 2018.6.24 bal4u

n, q = map(int, input().split())
Q = [[] for i in range(n)]
for i in range(q):
	a, *b = map(int, input().split())
	if   a == 0: Q[b[0]].append(b[1])        # push
	elif a == 1 and Q[b[0]]: print(Q[b[0]][-1]) # top
	elif a == 2 and Q[b[0]]: Q[b[0]].pop()      # pop

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int,input().split())
d = deque([[] for _ in range(n)])
for _ in range(q):
    t = input().split()
    if t[0] == "0":
        d[int(t[1])].append(t[2])
    elif t[0] == "1":
        if len(d[int(t[1])]) != 0:
            print(d[int(t[1])][-1])
    else:
        if len(d[int(t[1])]) != 0:
            d[int(t[1])].pop()

Submission status: Accepted

Solution code:
from collections import deque

n, q = list(map(int, input().split()))
S = deque()
[S.append([]) for _ in range(n)]

for i in range(q):
    query = list(map(int, input().split()))
    t = query[1]

    # push
    if query[0] == 0:
        S[t].append(query[2])

    # top
    elif query[0] == 1:
        if S[t]:
            print(S[t][-1])

    # pop
    else:
        if S[t]:
            S[t].pop()

Submission status: Accepted

Solution code:
import collections
n, q = map(int, input().split())
S = [[] for i in range(n)]
queries = list()
for i in range(q):
    queries.append(list(map(int, input().split())))
for query in queries:
    if query[0] == 0:
        S[query[1]].append(query[2])
    elif query[0] == 1:
        try:
            print(S[query[1]][-1])
        except:
            pass
    elif query[0] == 2:
        try:
            S[query[1]].pop()
        except:
            pass

Submission status: Accepted

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

N,Q = map(int,input().split())

S = [[] for _ in range(N)]

for _ in range(Q):
    q = list(map(int,input().split()))
    if q[0] == 0:
        S[q[1]].append(q[2])
    elif q[0] == 1:
        if S[q[1]]:
            print(S[q[1]][-1])
    else:
        if S[q[1]]:
            S[q[1]].pop()

Submission status: Accepted

Solution code:
n,q=map(int,input().split())

qs = [[] for _ in range(n)]

for _ in range(q):
	op = list(map(int,input().split()))
	stack = qs[op[1]]
	if op[0] == 0:
		stack.append(op[2])
	elif op[0] == 1:
		if len(stack)>0:
			print(stack[len(stack)-1])
	else:
		if len(stack)>0:
			stack.pop()

Submission status: Accepted

Solution code:
def main():
    n, q = map(int, input().split())
    St = [list() for _ in range(n)]

    for _ in range(q):
        para = [int(a) for a in input().split()]

        if para[0] == 0:
            St[para[1]].append(para[2])
        if para[0] == 1:
            if len(St[para[1]]) != 0:
                print(St[para[1]][-1])
        if para[0] == 2:
            if len(St[para[1]]) != 0:
                St[para[1]].pop()
main()

Submission status: Accepted

Solution code:
class TwoDimStack():
    def __init__(self, num_stack):
        self.stacks = [[] for i in range(0, num_stack, 1)]

    def push(self, t, x):
        self.stacks[t].append(x)
        return self

    def top(self, t):
        if len(self.stacks[t]) > 0:
            print(f'{self.stacks[t][len(self.stacks[t])-1]}')
        return self

    def pop(self, t):
        if len(self.stacks[t]) > 0:
            self.stacks[t].pop()
        return self


num_stack, num_queue = tuple(map(int, input().split(' ')))
stacks = TwoDimStack(num_stack)

for i in range(0, num_queue):
    queue = tuple(map(int, input().split(' ')))
    if queue[0] == 0:
        stacks.push(queue[1], queue[2])
    elif queue[0] == 1:
        stacks.top(queue[1])
    elif queue[0] == 2:
        stacks.pop(queue[1])

Submission status: Accepted

Solution code:
n,q=[int(i) for i in input().split(" ")]
S=[[] for i in range(n)]

for _ in range(q):
    query=[int(i) for i in input().split(" ")]
    if query[0]==0:
        S[query[1]].append(query[2])
    elif query[0]==1:
        if len(S[query[1]])>0:
            print(S[query[1]][-1])
    else:
        if len(S[query[1]])>0:
            S[query[1]].pop(-1)

Submission status: Accepted

Solution code:
import sys
n, q = map(int, input().split())

S = [[] for _ in range(n)]
ans =[]

for _ in range(q):
    query = list(map(int, sys.stdin.readline().split()))
    if query[0] == 0:
        S[query[1]].append(query[2])
    elif query[0] == 1:
        if S[query[1]]:
            ans.append(S[query[1]][-1])
            # print(S[query[1]][-1])
    else:
        if S[query[1]]:
            S[query[1]].pop()

for i in ans:
    print(i)

Submission status: Accepted

Solution code:

n, q = map(int, input().split())
stack = []
for _ in range(n):
    stack.append([])

for _ in range(q):
    c = list(map(int, input().split()))
    if c[0] == 0:
        stack[c[1]].append(c[2])
    elif c[0] == 1:
        if len(stack[c[1]]) > 0:
            print(stack[c[1]][-1])
    elif c[0] == 2:
        if len(stack[c[1]]) > 0:
            del(stack[c[1]][len(stack[c[1]])-1])

Submission status: Accepted

Solution code:
from collections import deque
from typing import List


if __name__ == "__main__":
    stack_num, query_num = map(lambda x: int(x), input().split())
    stacks: List[deque] = [deque() for _ in range(stack_num)]

    for _ in range(query_num):
        op, *value = map(lambda x: int(x), input().split())
        if (0 == op):
            stacks[value[0]].append(value[1])
        elif (1 == op):
            stack = stacks[value[0]]
            if (0 != len(stack)):
                print(stack[len(stack) - 1])
        elif (2 == op):
            stack = stacks[value[0]]
            if (0 != len(stack)):
                stack.pop()
        else:
            pass

Submission status: Accepted

Solution code:
n,q=map(int, input().split( ))
S=[[] for i in range(n)]
for i in range(q):
    c,*a=map(int,input().split( ))
    if c==0:
        S[a[0]].append(a[1])
    elif c==1:
        if len(S[a[0]])>0:
            print(S[a[0]][-1])
        else:
            pass
    elif c==2:
        if len(S[a[0]])>0:
            S[a[0]].pop()
        else:
            pass

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
n = int(sys.stdin.readline().split()[0])
A = defaultdict(deque)
ans = []
for query in sys.stdin:
    if query[0] == '0':
        t, x = query[2:].split()
        A[t].append(x)
    elif query[0] == '1':
        if A[query[2:-1]]:
            ans.append(A[query[2:-1]][-1] + '\n')
    else:
        if A[query[2:-1]]:
            A[query[2:-1]].pop()
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    n, q = map(int, f_i.readline().split())
    S = [[] for i in range(n)]
    ans = []
    
    for i in range(q):
        query = f_i.readline()
        op = query[0]
        if op == '0':
            t, x = query[2:].split()
            S[int(t)].append(x)
            continue
        s = S[int(query[2:])]
        if s:
            if op == '1':
                ans.append(s[-1])
            else:
                s.pop()
    
    print('\n'.join(ans))

solve()

Submission status: Accepted

Solution code:
from collections import deque
n, q = map(int, input().split())
Q = []
for i in range(n): Q.append(deque())
for i in range(q):
    a = input().split()
    t = int(a[1])
    if   a[0] == '0': Q[t].append(a[2])        # push
    elif a[0] == '1' and Q[t]: print(Q[t][-1]) # top
    elif a[0] == '2' and Q[t]: Q[t].pop()      # pop

Submission status: Accepted

Solution code:
from sys import stdin,stdout
from collections import defaultdict,deque

n, q = map(int,stdin.readline().split())
queries = stdin.readlines()
A = defaultdict(deque)
ans = []

#count = 0
for query in queries:
    #print(count)
    #count += 1
    query = query.split()
    if query[0] == '0':
        A[int(query[1])].append(query[2])
    elif query[0] == '1':
        index = int(query[1])
        if A[index]:
            ans.append(A[index][-1])
    else:
        index = int(query[1])
        if A[index]:
            A[index].pop()

print('\n'.join(ans))

Submission status: Accepted

Solution code:
n, q = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append([])
    
for i in range(q):
    a = list(map(int, input().split()))
    k = a[0]
    if k == 0:
        ss[a[1]].append(a[2]) 
    if k == 1:
        if len(ss[a[1]]) != 0:
            print(ss[a[1]][-1])
        else:
            continue
    if k == 2:
        if len(ss[a[1]]) != 0:
            ss[a[1]].pop(-1)
        else:
            continue

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
stack=[]
for i in range(n):
    stack.append([])
for i in range(q):
    query=list(map(int,input().split()))
    if query[0]==0:
        stack[query[1]].append(query[2])
    elif query[0]==1:
        if len(stack[query[1]])!=0:
            print(stack[query[1]][-1])
    else:
        if len(stack[query[1]])!=0:
            stack[query[1]].pop(-1)

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
A=[]
for i in range(0,n):
    A.append([])
for i in range(0,q):
    query=list(map(int,input().split()))
    if query[0]==0:
        A[query[1]].append(query[2])
    elif A[query[1]]!=[]:
        if query[0]==1:
            print(A[query[1]][-1])
        elif query[0]==2:
            A[query[1]].pop()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Basic Data Structures - Stack
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_2_A&lang=jp

"""
n, q = map(int, input().split())
stacks = [[] for _ in range(n)]
for _ in range(q):
    op, t, x = (input() + ' 1').split()[:3]
    if op == '0':
        stacks[int(t)].append(x)
    elif op == '1':
        if stacks[int(t)]:
            print(stacks[int(t)][-1])
    else:
        if stacks[int(t)]:
            stacks[int(t)].pop()

Submission status: Accepted

Solution code:
from collections import deque

n, queries = map(int, input().split())

stack_dict = []
for i in range(n):
    stack_dict.append(deque())

for i in range(queries):
    q, t, *x = map(int, input().split())
    if q == 0:
        stack_dict[t].append(x[0])
    elif q == 1:
        if stack_dict[t]:
            print(stack_dict[t][-1])
    elif q == 2:
        if stack_dict[t]:
            stack_dict[t].pop()

Submission status: Accepted

Solution code:
n, q = map(int, input().split())

S = [[] for _ in range(n)]


for _ in range(q):

    command, *list_num = input().split()

    t = int(list_num[0])

    if   command == "0":
        # push(t, x)
        x = int(list_num[1])

        S[t].append(x)
    elif command == "1":
        # top(t)
        if S[t]:
            print(S[t][-1])
    elif command == "2":
        # pop(t)
        if S[t]:
            S[t].pop()
    else:
        raise

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
box = [deque()for i in range(n)]
for _ in range(q):
    a, b, *c = map(int, input().split())
    if a == 0:
        box[b].append(c[0])
    elif a == 1:
        if len(box[b]):
            print(box[b][-1])
    else:
        if box[b]:box[b].pop()

Submission status: Accepted

Solution code:
from sys import stdin

n,q = map(int,input().split(" "))

list2 = [[] for _ in range(n)]

for i in range(q):
	cmd = stdin.readline().strip().split()

	t = int(cmd[1])
	if cmd[0] == "0":
		list2[t].append(cmd[2])
	elif cmd[0] == "1":
		if len(list2[t]) != 0:
			print(list2[t][-1])
	else:
		if len(list2[t]) != 0:
			list2[t].pop()

Submission status: Accepted

Solution code:
n, r = input().split(' ')
stack = [[] for _ in range(int(n))]
for _ in range(int(r)):
    row = list(map(int, input().split(' ')))
    c = row[0]
    i = row[1]

    if c == 0:
        stack[i].append(row[2])
    if stack[i]:
        if c == 1:
            print(stack[i][-1])
        if c == 2:
            stack[i].pop()

Submission status: Accepted

Solution code:
from collections import deque
n,q = map(int, input().split())
S = []

for i in range(n):
    S.append(deque())
for i in range(q):
    a = input().split()
    t = int(a[1])
    
    if a[0] == '0':
        S[t].append(a[2])
    elif a[0] == '1' and S[t]:
        print(S[t][-1])
    elif a[0] == '2' and S[t]:
        S[t].pop()

Submission status: Accepted

Solution code:
def push(S,t,x):
    S[t].append(x)
    return S

def top(S,t):
    count=len(S[t])
    if count!=0:
        print(S[t][-1])

def pop(S,t):
    count=len(S[t])
    if count!=0:
        del S[t][-1]
    return S

n,q=(int(x) for x in input().split())
S=[[] for i in range(n)]

for i in range(q):
    query=list(map(int,input().split()))
    if query[0]==0:
        S=push(S,query[1],query[2])
    elif query[0]==1:
        top(S,query[1])
    else:
        S=pop(S,query[1])

Submission status: Accepted

Solution code:
n,q = map(int,input().split())

stack = [[] for i in range(n)]

for i in range(q):
    query = [k for k in map(int,input().split())]

    if query[0] == 0:
        stack[query[1]].append(query[2])
    elif query[0] == 1:
        try:
            temp = stack[query[1]].pop()
            print(temp)
            stack[query[1]].append(temp)
        except:
            pass
    elif query[0] == 2:
        try:
            stack[query[1]].pop()
        except:
            pass

Submission status: Accepted

Solution code:
def main():
    n, q = [int(x) for x in input().split(' ')]
    stacks = []
    for i in range(n):
        stacks.append([])
    for i in range(q):
        query = input()
        cmd = int(query[0])
        if cmd == 0:
            _, t, x = [int(x) for x in query.split(' ')]
            stacks[t].append(x)
        elif cmd == 1:
            _, t = [int(x) for x in query.split(' ')]
            if stacks[t]:
                print(str(stacks[t][-1]))
        elif cmd == 2:
            _, t = [int(x) for x in query.split(' ')]
            if stacks[t]:
                stacks[t].pop()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque
n, q = map(int, input().split())

deq = deque()
arrays = {str(i): deque() for i in range(n)}

lines = sys.stdin.readlines()
for i in range(q):
    query, *arg = lines[i].split()
    if query == '0':    # push t x
        arrays[arg[0]].append(arg[1])
    elif query == '1':  # top
        if len(arrays[arg[0]]):
            print(arrays[arg[0]][-1])
    elif query == '2':  # pop
        if len(arrays[arg[0]]):
            arrays[arg[0]].pop()
    else:
        raise AssertionError

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_2_A&lang=jp
# Stack

from collections import deque
import sys
input = sys.stdin.readline

def loop_proc():
    (n1,n2) = map(int,input().split())
    wl =[]
    for i in range(n1):
        wl.append(deque())
    for i in range(n2):
        l = list(map(int,input().split()))  
        if (l[0] == 0 ):
            wl[l[1]].append(l[2])
        elif (l[0] == 1 ):
            if ( len(wl[l[1]]) != 0 ):
                tl = wl[l[1]]
                print (tl[-1])
        elif (l[0] == 2 ):
            if ( len(wl[l[1]]) != 0 ):
                wl[l[1]].pop()
 

loop_proc()

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
stack = [[] for _ in range(n)]

for _ in range(q):
    query = list(map(int, input().split()))
    if query[0] == 0:
        stack[query[1]].append(query[2])
    elif query[0] == 1:
        if stack[query[1]] == []:
            pass
        else:
            print(stack[query[1]][-1])
    elif query[0] == 2:
        if stack[query[1]] == []:
            pass
        else:
            stack[query[1]].pop(-1)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
n = int(sys.stdin.readline().split()[0])
A = defaultdict(deque)
ans = []
for query in sys.stdin:
    if query[0] == '0':
        t, x = query[2:].split()
        A[t].append(x)
    elif query[0] == '1':
        if A[query[2:-1]]:
            ans.append(A[query[2:-1]][-1] + '\n')
    else:
        if A[query[2:-1]]:
            A[query[2:-1]].pop()
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
readline = open(0).readline
writelines = open(1, 'w').writelines
N, Q = map(int, readline().split())
ans = []
A = [[] for i in range(N)]
def push(t, x):
    A[t].append(x)
def top(t):
    A[t] and ans.append("%d\n" % A[t][-1])
def pop(t):
    A[t] and A[t].pop()

C = [push, top, pop].__getitem__
for i in range(Q):
    t, *a=map(int, readline().split())
    C(t)(*a)
writelines(ans)

Submission status: Accepted

Solution code:
from collections import deque

n, q = map(int, input().split())
S = [deque() for _ in range(n)]
for _ in range(q):
    query = list(map(int, input().split()))
    if query[0] == 0:
        S[query[1]].append(query[2])
    elif len(S[query[1]]) > 0:
        if query[0] == 1:
            print(S[query[1]][-1])
        elif query[0] == 2:
            S[query[1]].pop()

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from collections import defaultdict

stacks = defaultdict(list)   # Key String, Value List

n,q = map(int, sys.stdin.readline().split())
for i in range(q):
    query = sys.stdin.readline().split()  # use in string
    ope = query[0]
    stack_id = query[1]

    if ope == '0':      # Push
        stacks[stack_id].append(query[2])
    elif ope == '1':    # Top
        if stacks[stack_id]:
            print(stacks[stack_id][-1])
    elif ope == '2':    # Pop
        if stacks[stack_id]:
            stacks[stack_id].pop()

Submission status: Accepted

Solution code:
n, q = list(map(int, input().split()))
a = [[] for i in range(n)]
for _ in range(q):
  q = list(map(int, input().split()))
  if q[0]==0:
    a[q[1]].append(q[2])
  elif q[0]==1:
    if len(a[q[1]])>0:
      print(a[q[1]][-1])
  elif q[0]==2:
    if len(a[q[1]])>0:
      a[q[1]].pop()

Submission status: Runtime Error

Solution code:
n, q = list(map(int, input().split(' ')))
stacks = [[] for i in range(n)]

for i in range(q):
    op = list(map(int, input().split(' ')))
    if op[0] == 0:
        stacks[op[1]].append(op[2])
    elif op[0] == 1:
        if len(stacks[op[1]]) != 0:
            print(stacks[op[1]][-1])
    elif op[0] == 2:
        stacks[op[1]].pop()

Submission status: Runtime Error

Solution code:
from collections import deque

if __name__ == '__main__':
    n, q = input().split()
    n, q = int(n), int(q)
    S = [deque([]) for i in range(n)]

    for i in range(q):
        query = input().split()

        if query[0] == '0':
            S[int(query[1])].append(query[2])

        else:
            if len(S) == 0:
                pass
            elif query[0] == '1':
                print(S[int(query[1])][-1])
            else:
                S[int(query[1])].pop()

Submission status: Runtime Error

Solution code:
# AOJ TP2_2_A: Stack
# Python3 2018.6.24 bal4u

from collections import deque
n, q = map(int, input().split())
Q = []
for i in range(n): Q.append(deque())
for i in range(q):
	a = input().split()
	t = int(a[1])
	if   a[0] == '0': Q[t].append(a[2])   # push
	elif a[0] == '1': print(Q[t][-1])     # top
	else: Q[t].pop()                      # pop

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 24 18:23:54 2018
ITP22A
@author: maezawa
"""

n, q = list(map(int, input().split()))
a = [[] for _ in range(n)]

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 24 18:23:54 2018
ITP22A
@author: maezawa
"""

n, q = list(map(int, input().split()))
a = [[] for _ in range(n)]

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