Problem p03040 (python)

Fixed submission:
import heapq

Q = int(input())
m = L = R = 0
l, r = [], []
n = 0
b = 0

for i in range(Q):
  query = list(map(int, input().split()))
  if len(query) == 3: # 更新クエリ
    b += query[2]
    new = query[1]
    if  n == 0:
      m = new
    elif n == 1:
      if new <= m:
        heapq.heappush(l, -new)
        heapq.heappush(r, m)
        L, R = new, m
        m = new
      else:
        heapq.heappush(l, -m)
        heapq.heappush(r, new)
        L, R = m, new
    elif n == 2:
      if new < m:
        m = -heapq.heappop(l)
        heapq.heappush(l, -new)
        L = new
      else:
        _r = heapq.heappop(r)
        if new < _r:
          m = new
          heapq.heappush(r, _r)
        else:
          m = _r
          heapq.heappush(r, new)
          R = new
    else:
      _l = - heapq.heappop(l)
      _r = heapq.heappop(r)
      if n%2:
        if new <= _l:
          heapq.heappush(l, -new)
          heapq.heappush(r, _r)
          heapq.heappush(r, m)
          m, L, R = _l, L+new, R+m
        elif new <= m:
          heapq.heappush(l, -_l)
          heapq.heappush(r, _r)
          heapq.heappush(r, m)
          m, L, R = new, L+new, R+m
        else:
          heapq.heappush(l, -_l)
          heapq.heappush(r, _r)
          heapq.heappush(r, new)
          L, R = L+m, R+new
      else:
        if new <= m:
          heapq.heappush(l, -_l)
          heapq.heappush(l, -new)
          heapq.heappush(r, _r)
          L += new - m
        elif new <= _r:
          heapq.heappush(l, -_l)
          heapq.heappush(l, -m)
          heapq.heappush(r, _r)
          m = new
        else:
          heapq.heappush(l, -_l)
          heapq.heappush(l, -m)
          heapq.heappush(r, new)
          m, R = _r, R+new-_r
    n += 1
  else: # 求値クエリ
    print(m, R-L+b)

Buggy submission (verdict: Runtime Error):
import heapq

Q = int(input())
m = L = R = 0
l, r = [], []
n = 0
b = 0

for i in range(Q):
  query = list(map(int, input().split()))
  if len(query) == 3: # 更新クエリ
    b += query[2]
    new = query[1]
    if  n == 0:
      m = new
    elif n == 1:
      if new <= m:
        heapq.heappush(l, -new)
        heapq.heappush(r, m)
        L, R = new, m
        m = new
      else:
        heapq.heappush(l, -m)
        heapq.heappush(r, new)
        L, R = m, new
    elif n == 2:
      if new < m:
        m = -heapq.heappop(l)
        heapq.heappush(l, -new)
        L = new
      else:
        _r = heapq.heappop(r)
        if new < _r:
          m = new
          heapq.heappush(r, _r)
        else:
          m = _r
          heapq.heappush(r, new)
          R = new
    else:
      _l = - heapq.heappop(l)
      _r = heapq.heappop(r)
      if n%2:
        if new <= _l:
          heapq.heappush(l, -new)
          heapq.heappush(r, _r)
          heapq.heappush(r, m)
          m, L, R = l, L+new, R+m
        elif new <= m:
          heapq.heappush(l, -_l)
          heapq.heappush(r, _r)
          heapq.heappush(r, m)
          m, L, R = new, L+new, R+m
        else:
          heapq.heappush(l, -_l)
          heapq.heappush(r, _r)
          heapq.heappush(r, new)
          L, R = L+m, R+new
      else:
        if new <= m:
          heapq.heappush(l, -_l)
          heapq.heappush(l, -new)
          heapq.heappush(r, _r)
          L += new - m
        elif new <= _r:
          heapq.heappush(l, -_l)
          heapq.heappush(l, -m)
          heapq.heappush(r, _r)
          m = new
        else:
          heapq.heappush(l, -_l)
          heapq.heappush(l, -m)
          heapq.heappush(r, new)
          m, R = _r, R+new-_r
    n += 1
  else: # 求値クエリ
    print(m, R-L+b)