Advertisement
Guest User

Train

a guest
Nov 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. train = []
  2. counts = dict()
  3.  
  4. def push(cnt, name):
  5.     counts[name] = counts.get(name, 0) + cnt
  6.     if len(train) > 0 and train[-1][0] == name:
  7.         train[-1][1] += cnt
  8.     else:
  9.         train.append( [name, cnt] )
  10.  
  11. def pop(t):
  12.     while train[-1][1] < t:
  13.         t -= train[-1][1]
  14.         counts[train[-1][0]] -= train[-1][1]
  15.         train.pop()
  16.     train[-1][1] -= t
  17.     counts[train[-1][0]] -= t
  18.  
  19. n = int(input())
  20. for i in range(n):
  21.     lst = input().split()
  22.     if lst[0] == 'add':
  23.         cmd, cnt, loot = lst
  24.         cnt = int(cnt)
  25.         push( cnt, loot )
  26.     elif lst[0] == 'delete':
  27.         cmd, cnt = lst
  28.         cnt = int(cnt)
  29.         pop(cnt)
  30.     else:
  31.         cmd, loot = lst
  32.         print( counts.get(loot, 0) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement