Problem:


Score : 600 points

Problem Statement

There is a function f(x), which is initially a constant function f(x) = 0.

We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).

An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

All values in input are integers.

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

-10^9 \leq a, b \leq 10^9

The first query is an update query.

Input

Input is given from Standard Input in the following format:

Q
Query_1
:
Query_Q

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.

Sample Input 1

4
1 4 2
2
1 1 -8
2

Sample Output 1

4 2
1 -3

In the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.

In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \leq x \leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1.

Sample Input 2

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2

-1000000000 3000000000



Sample input:
4
1 4 2
2
1 1 -8
2


Sample output:
4 2
1 -3

Submission status: Accepted

Solution code:

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

Q = int(input())
#Q = 100000
q = []
for i in range(Q):
	q.append(list(map(int, input().split())))
	#q.append([1, (i*17) % 10000, i])

a_list = []
a_list2 = []
b_sum = 0
a_sum = 0
a_sum_half = 0
length = 0
for i in range(Q):
	if len(q[i]) == 3:
		if len(a_list2) == 0:
			D = True
		else:
			x = heapq.heappop(a_list2)
			heapq.heappush(a_list2,x)
			D = (x <= q[i][1])
		length += 1
		if length % 2 == 0 and not D:
			a_sum_half += q[i][1]
			heapq.heappush(a_list, -q[i][1])
		elif length % 2 == 0 and D:
			heapq.heappush(a_list2, q[i][1])
			x = heapq.heappop(a_list2)
			heapq.heappush(a_list, -x)
			a_sum_half += x
		elif length % 2 == 1 and not D:
			a_sum_half += q[i][1]
			heapq.heappush(a_list, -q[i][1])
			x = heapq.heappop(a_list)
			a_sum_half += x
			heapq.heappush(a_list2, -x)
		else:
			heapq.heappush(a_list2, q[i][1])
		a_sum += q[i][1]
		b_sum += q[i][2]
	else:
		if length % 2 == 0:
			argmin_x = -heapq.heappop(a_list)
			heapq.heappush(a_list, -argmin_x)
		else:
			argmin_x = heapq.heappop(a_list2)
			heapq.heappush(a_list2, argmin_x)
		min_x = argmin_x*(length//2) - a_sum_half
		min_x += (a_sum - a_sum_half) - (argmin_x*(length - length//2))
		min_x += b_sum
		print("{0} {1}".format(argmin_x, min_x))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
Q = int(input())
X = [list(map(int, input().split())) for _ in range(Q)]

left_q = []
right_q = []
q_sum = 0
const = 0

for q, *w in X:
    if q == 1:
        x, y = w
        const += y
        
        if len(left_q) == 0 or x <= -left_q[0]:
            heappush(left_q, -x)
            q_sum += -left_q[0] - x
        else:
            heappush(right_q, x)
            q_sum += left_q[0] + x
            
        if len(right_q) > len(left_q):
            q_sum += -right_q[0] - left_q[0]
            heappush(left_q, -heappop(right_q))
        
        if len(left_q) - len(right_q) == 2:
            heappush(right_q, -heappop(left_q))
    else:
        print(-left_q[0], q_sum + const)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
import sys

input = sys.stdin.readline


class median_o1:
    def __init__(self):
        self.count = 0
        self.al = []
        self.ar = []
        self.sl = 0
        self.sr = 0
        self.b = 0

    def update_b(self, b):
        self.b += b

    def insert(self, x):
        if self.count % 2 == 0:
            if self.count == 0:
                heappush(self.al, -x)
                self.sl += x
            else:
                t = heappop(self.ar)
                self.sr -= t
                tl, tr = min(x, t), max(x, t)
                heappush(self.al, -tl)
                self.sl += tl
                heappush(self.ar, tr)
                self.sr += tr
        else:
            t = heappop(self.al)
            t *= (-1)
            self.sl -= t
            tl, tr = min(x, t), max(x, t)
            heappush(self.ar, tr)
            self.sr += tr
            heappush(self.al, -tl)
            self.sl += tl
        self.count += 1

    def get_median(self):
        return -self.al[0]

    def calc_min_value(self):
        # print(self.al, self.ar, self.b, self.sl, self.sr)
        m = self.get_median()
        return (m * ((self.count + 1) // 2) - self.sl) + (self.sr - m * (self.count // 2)) + self.b


q = int(input())

m = median_o1()

for _ in range(q):
    q_typ, *x = map(int, input().split())
    # 1, [a, b]
    # 2, []
    if q_typ == 1:
        m.update_b(x[1])
        m.insert(x[0])
    else:
        print(m.get_median(), m.calc_min_value())

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
import sys
input = sys.stdin.readline


def main():
    Q = int(input())
    y = 0
    lheap = []
    rheap = []
    lsum = 0
    rsum = 0
    for _ in range(Q):
        q = input()
        if q[0] == '1':
            # update
            _, a, b = map(int, q.split())
            y += b
            if len(lheap) == 0 or -lheap[0] <= a <= rheap[0]:
                heappush(lheap, -a)
                heappush(rheap, a)
                lsum += a
                rsum += a
            elif a < -lheap[0]:
                mov = -heappop(lheap)
                heappush(rheap, mov)
                heappush(lheap, -a)
                heappush(lheap, -a)
                lsum += a*2 - mov
                rsum += mov
            elif rheap[0] < a:
                mov = heappop(rheap)
                heappush(lheap, -mov)
                heappush(rheap, a)
                heappush(rheap, a)
                lsum += mov
                rsum += a*2 - mov
        else:
            # inquiry
            x = -lheap[0]
            minimum = abs(x * len(lheap) - lsum) // 2 + \
                abs(x * len(rheap) - rsum) // 2 + y
            print(x, minimum)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from heapq import heappop, heappush


def main():
    Q = int(input())
    y = 0
    lheap = []
    rheap = []
    lsum = 0
    rsum = 0
    for _ in range(Q):
        q = input()
        if q[0] == '1':
            # update
            _, a, b = map(int, q.split())
            y += b
            if len(lheap) == 0 or -lheap[0] <= a <= rheap[0]:
                heappush(lheap, -a)
                heappush(rheap, a)
                lsum += a
                rsum += a
            elif a < -lheap[0]:
                mov = -heappop(lheap)
                heappush(rheap, mov)
                heappush(lheap, -a)
                heappush(lheap, -a)
                lsum += a*2 - mov
                rsum += mov
            elif rheap[0] < a:
                mov = heappop(rheap)
                heappush(lheap, -mov)
                heappush(rheap, a)
                heappush(rheap, a)
                lsum += mov
                rsum += a*2 - mov
        else:
            # inquiry
            x = -lheap[0]
            minimum = abs(x * len(lheap) - lsum) // 2 + \
                abs(x * len(rheap) - rsum) // 2 + y
            print(x, minimum)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq
q=int(input())
large=[]
small=[]
l_s=0
s_s=0
c=0
ll=0
ls=0
cc=0
for i in range(q):
    s=input()
    if s[0]=='1':
        cc=(cc+1)%2
        _,a,b=map(int,s.split())
        c+=b
        if large==[] and small==[]:
            heapq.heappush(small, -1*a)
            s_s+=a
            ls+=1
        elif a> small[0]*-1:
            heapq.heappush(large, a)
            l_s-=a
            ll+=1
        else:
            x=heapq.heappop(small)
            heapq.heappush(small, -1*a)
            heapq.heappush(large, -1*x)
            s_s=s_s+a+x
            l_s+=x
            ll+=1
        if not (ll<=ls<=ll+1):
            x=heapq.heappop(large)
            heapq.heappush(small, -1*x)
            s_s=s_s+x
            l_s+=x
            ll-=1
            ls+=1
        #print(large,small)
    else:
        #print(s_s,l_s,c,cc)
        print(small[0]*-1,c-s_s-l_s-small[0]*cc)

Submission status: Accepted

Solution code:
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline

Q = int(input())
ss = input()
t, A, B = map(int, ss.split())
PQ1, PQ2 = [-A], [A]
fr = to = A
minF = 0
accB = B
anss = []
for _ in range(Q-1):
    ss = input()
    if ss[0] == '1':
        t, A, B = map(int, ss.split())
        if fr <= A <= to:
            heappush(PQ1, -A)
            heappush(PQ2, A)
        elif A < fr:
            p = -heappop(PQ1)
            heappush(PQ2, p)
            heappush(PQ1, -A)
            heappush(PQ1, -A)
            minF += fr-A
        else:
            p = heappop(PQ2)
            heappush(PQ1, -p)
            heappush(PQ2, A)
            heappush(PQ2, A)
            minF += A-to
        fr = -heappop(PQ1)
        to = heappop(PQ2)
        heappush(PQ1, -fr)
        heappush(PQ2, to)
        accB += B
    else:
        anss.append((fr, minF+accB))

print('\n'.join(map(lambda x: str(x[0])+' '+str(x[1]), anss)))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappush, heappop


# 中央値以上と以下をheapで管理
upper_asc = []
lower_dsc = []

q = ni()
ans = 0

for i in range(q):
    query = list(map(int, ns().split()))
    if query[0] == 1:
        a, b = query[1], query[2]
        heappush(upper_asc, a)
        heappush(lower_dsc, -a)

        if i == 0:
            ans += b
            continue

        x, y = heappop(upper_asc), -heappop(lower_dsc)

        ans += (y - x) + b

        heappush(upper_asc, y)
        heappush(lower_dsc, -x)

    else:
        print(-lower_dsc[0], ans)

Submission status: Accepted

Solution code:
from heapq import heappop,heappush
import sys
input = sys.stdin.readline
def resolve():
    N = int(input())
    b = 0
    left = []
    right = []
    sumleft = sumright = mid = 0
    for _ in range(N):
        q = list(map(int,input().split()))
        if q[0]==1:#登録
            a = q[1]
            b = b + q[2]
            if not left or -left[0]>a:
                heappush(left,-a)
                sumleft = sumleft + a
                if (len(left)-len(right))>=2:
                    move = -heappop(left)
                    heappush(right,move)
                    sumleft = sumleft-move
                    sumright = sumright+move
            else:
                heappush(right,a)
                sumright = sumright + a
                if len(left)<len(right):
                    move = heappop(right)
                    heappush(left,-move)
                    sumright = sumright-move
                    sumleft = sumleft+move
            mid = -left[0]
        else:#表示
            ans = (len(left)-len(right))*mid-sumleft+sumright + b
            print(mid,ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
Q = int(input())
qs = [tuple(map(int,input().split())) for i in range(Q)]

import heapq
class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

lq = Heapq([],True)
rq = Heapq([])
ans = 0

for q in qs:
    if q[0] == 2:
        print(lq.top(), ans)
        continue
    _,a,b = q
    ans += b
    lq.push(a)
    rq.push(a)
    if lq.top() > rq.top():
        l = lq.pop()
        r = rq.pop()
        ans += abs(l-r)
        lq.push(r)
        rq.push(l)

Submission status: Accepted

Solution code:
import heapq
Q = int(input())
qs = [tuple(map(int,input().split())) for i in range(Q)]

lq = []
rq = []
heapq.heapify(lq)
heapq.heapify(rq)
ans = 0

for q in qs:
    if q[0] == 2:
        print(-lq[0], ans)
        continue
    _,a,b = q
    ans += b
    heapq.heappush(lq, -a)
    heapq.heappush(rq, a)
    if -lq[0] > rq[0]:
        l = -heapq.heappop(lq)
        r = heapq.heappop(rq)
        ans += abs(l-r)
        heapq.heappush(lq,-r)
        heapq.heappush(rq,l)

Submission status: Accepted

Solution code:
from heapq import heappush, heappushpop

Q = int(input())

b = 0
la = []
sl = 0
ra = []
sr = 0

cnt = 0
for _ in range(Q):
    q = tuple(map(int, input().split()))
    # print(la, ra)
    if q[0] == 2:
        # 求値
        x = -la[0]
        cl = (cnt + 1) // 2
        cr = cnt - cl
        print(x, (cl * x - sl) + (sr - cr * x) + b)
    else:
        # 更新
        b += q[2]
        cnt += 1
        if cnt % 2 == 1:
            sr += q[1]
            x = heappushpop(ra, q[1])
            sr -= x
            sl += x
            heappush(la, -x)
        else:
            sl += q[1]
            x = heappushpop(la, -q[1])
            x *= -1
            sl -= x
            sr += x
            heappush(ra, x)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush, heappushpop

Q = int(input())

b = 0
la = []
sl = 0
ra = []
sr = 0

cnt = 0
for _ in range(Q):
    q = input().split()
    # print(la, ra)
    if q[0] == '2':
        # 求値
        x = -la[0]
        print(x, ((cnt + 1) // 2 * x - sl) + (sr - cnt // 2 * x) + b)
    else:
        # 更新
        x, y = map(int, q[1:])
        b += y
        cnt += 1
        if cnt % 2 == 1:
            sr += x
            x = heappushpop(ra, x)
            sr -= x
            sl += x
            heappush(la, -x)
        else:
            sl += x
            x = heappushpop(la, -x)
            x *= -1
            sl -= x
            sr += x
            heappush(ra, x)

Submission status: Accepted

Solution code:
def main():
	def Technique_often_used():    
		# enumerate: for i, a in enumerate(a_list):
		# q, mod = divmod(a, b)
		# Functional Programming: filter, map, reduce
		pass
	def naming_conventions():
		# [s_1, s_2, s_3, ..., s_k] is named s_k
		# [[s_11, s_12, ..., s_1k], ..,[s_j1, s_j2, ..., s_jk] is named s_jk
		# [[b1,c1],[b2,c2],..,[bm,cm]] is named bc
		# if there is something strange in a list then I add _ to the list's name (ex:s_jk_)
		pass
	def visual_studio_code_shortcut():    
		# visual studio code shortcut https://qiita.com/TakahiRoyte/items/cdab6fca64da386a690b
		# delete line: Ctrl+Shift+k
		# choose same words: Ctrl+Shift+l
		pass
	import sys
	from itertools import combinations, permutations # https://docs.python.org/ja/3/library/itertools.html
	from collections import deque, Counter # https://docs.python.org/ja/3/library/collections.html#collections.deque
	from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
	from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
	from functools import reduce
	from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
	def gcds(numbers):
		return reduce(gcd, numbers)
	def lcm(x, y):
		return (x * y) // gcd(x, y)
	def lcms(numbers): #
		return reduce(lcm, numbers, 1)

	# set the inputs
		# open(0).read() is a convenient method: 
		# ex) n, m, *x = map(int, open(0).read().split())
		#     min(x[::2]) - max(x[1::2])
		# ex2) *x, = map(int, open(0).read().split())
		#     don't forget to add comma after *x if only one variable is used
		# ex1) n, k = R()
		# ex2) v = list(R())
		# ex3) bc = [list(R()) for i in range(m)]

	R = lambda: map(int, input().split())
	q = int(input())
	queries = [list(R()) for i in range(q)]

	# q = 4
	# queries=[[1,4,2],[1,5,3],[1,-9,5],[1,1,-8],[2]]

	l = []
	r = []
	ans = 0
	for query in queries:
		if query[0] == 1:
			a = heappushpop(l,-query[1])
			b = heappushpop(r, query[1])
			heappush(l,-b)
			heappush(r,-a)
			ans += abs(b+a) + query[2]
		else:
			print(-l[0],ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import zip_longest
from heapq import heappush, heappop

# 入力
Q = int(input())

q, a, b = (
    zip_longest(*(map(int, input().split()) for _ in range(Q))) if Q else
    ((), ())
)

INF = 10**18
L = []
R = []
heappush(L, INF)
heappush(R, INF)
S = 0
res = []

for i, A, B in zip(q, a, b):
    if i == 1:
        S += B
        if len(L) == len(R):
            if A < -L[0]:
                S += -L[0] - A
                heappush(L, -A)
            elif A <= R[0]:
                heappush(L, -A)
            else:
                S += A - R[0]
                t = heappop(R)
                heappush(L, -t)
                heappush(R, A)
        else:
            if A < -L[0]:
                S += -L[0] - A
                t = -heappop(L)
                heappush(R, t)
                heappush(L, -A)
            else:
                S += A - (-L[0])
                heappush(R, A)
    else:
        res.append((-L[0], S))

ans = '\n'.join(
    ' '.join(map(str, r))
    for r in res
)

# 出力
print(ans)

Submission status: Accepted

Solution code:
def Technique_often_used():    
    # enumerate: for i, a in enumerate(a_list):
    # q, mod = divmod(a, b)
    # Functional Programming: filter, map, reduce
    pass
def naming_conventions():
    # [s_1, s_2, s_3, ..., s_k] is named s_k
    # [[s_11, s_12, ..., s_1k], ..,[s_j1, s_j2, ..., s_jk] is named s_jk
    # [[b1,c1],[b2,c2],..,[bm,cm]] is named bc
    # if there is something strange in a list then I add _ to the list's name (ex:s_jk_)
    pass
def visual_studio_code_shortcut():    
    # visual studio code shortcut https://qiita.com/TakahiRoyte/items/cdab6fca64da386a690b
    # delete line: Ctrl+Shift+k
    # choose same words: Ctrl+Shift+l
    pass
import sys
from itertools import combinations, permutations # https://docs.python.org/ja/3/library/itertools.html
from collections import deque, Counter # https://docs.python.org/ja/3/library/collections.html#collections.deque
from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
from functools import reduce
from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers): #
    return reduce(lcm, numbers, 1)

# set the inputs
    # open(0).read() is a convenient method: 
    # ex) n, m, *x = map(int, open(0).read().split())
    #     min(x[::2]) - max(x[1::2])
    # ex2) *x, = map(int, open(0).read().split())
    #     don't forget to add comma after *x if only one variable is used
    # ex1) n, k = R()
    # ex2) v = list(R())
    # ex3) bc = [list(R()) for i in range(m)]

R = lambda: map(int, input().split())
q = int(input())
queries = [list(R()) for i in range(q)]

# q = 4
# queries=[[1,4,2],[1,5,3],[1,-9,5],[1,1,-8],[2]]

l = []
r = []
ans = 0
for query in queries:
    if query[0] == 1:
        a = heappushpop(l,-query[1])
        b = heappushpop(r, query[1])
        heappush(l,-b)
        heappush(r,-a)
        ans += abs(b+a) + query[2]
    else:
        print(-l[0],ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappushpop
import sys

input = sys.stdin.readline

Q = int(input())

update_count = 0
al = []
sl = 0
ar = []
sr = 0
b = 0
for _ in range(Q):
    # print(f'al:{al}, sl:{sl}')
    # print(f'ar:{ar},sr:{sr}')
    # print(f'b:{b}')
    # print()
    s = tuple(map(int, input().split()))
    if s[0] == 1:
        # 更新クエリ
        b += s[2]
        if update_count % 2 == 0:  # 左に追加
            x = heappushpop(ar, s[1])  # 右に追加してから,最小値を吐かせる
            sr += s[1] - x
            sl += x
            x *= (-1)
            heappush(al, x)  # 左に追加
        else:  # 右に追加
            x = heappushpop(al, -s[1])  # 左に追加してから,最小値を吐かせる
            x *= (-1)
            sl += s[1] - x
            sr += x
            heappush(ar, x)  # 右に追加
        update_count += 1
        # 左キューalの最大値が中央値になっている
        # 左キューにheappushする際,負値に変換したので,heappopで最大値*(-1)が求まる
    else:
        # 求値クエリ
        x = -al[0]  # 中央値
        cnt_l = (update_count + 1) // 2
        cnt_r = update_count - cnt_l
        print(x, (cnt_l * x - sl) + (sr - cnt_r * x) + b)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import sys

input = sys.stdin.readline

Q = int(input())

update_count = 0
al = []
sl = 0
ar = []
sr = 0
b = 0
for _ in range(Q):
    # print(f'al:{al}, sl:{sl}')
    # print(f'ar:{ar},sr:{sr}')
    # print(f'b:{b}')
    # print()
    s = tuple(map(int, input().split()))
    if s[0] == 1:
        # 更新クエリ
        b += s[2]
        if update_count % 2 == 0:
            # 左に追加
            heappush(ar, s[1])  # 右に追加
            sr += s[1]
            x = heappop(ar)  # 右の最小値
            sr -= x
            heappush(al, -x)  # 左に追加
            sl += x
        else:
            # 右に追加
            heappush(al, -s[1])  # 左に追加
            sl += s[1]
            x = heappop(al)  # 左の最小値
            sl -= (-x)
            heappush(ar, -x)  # 右に追加
            sr += (-x)
        update_count += 1
        # 左キューalの最大値が中央値になっている
        # 左キューにheappushする際,負値に変換したので,heappopで最大値*(-1)が求まる
    else:
        # 求値クエリ
        x = heappop(al)  # 中央値
        heappush(al, x)
        x *= (-1)
        cnt_l = (update_count + 1) // 2
        cnt_r = update_count - cnt_l
        print(x, (cnt_l * x - sl) + (sr - cnt_r * x) + b)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

Q = int(input())

update_count = 0
al = []
sl = 0
ar = []
sr = 0
b = 0
for _ in range(Q):
    # print(f'al:{al}, sl:{sl}')
    # print(f'ar:{ar},sr:{sr}')
    # print(f'b:{b}')
    # print()
    s = tuple(map(int, input().split()))
    if s[0] == 1:
        # 更新クエリ
        b += s[2]
        if update_count % 2 == 0:
            # 左に追加
            heappush(ar, s[1])  # 右に追加
            sr += s[1]
            x = heappop(ar)  # 右の最小値
            sr -= x
            heappush(al, -x)  # 左に追加
            sl += x
        else:
            # 右に追加
            heappush(al, -s[1])  # 左に追加
            sl += s[1]
            x = heappop(al)  # 左の最小値
            sl -= (-x)
            heappush(ar, -x)  # 右に追加
            sr += (-x)
        update_count += 1
        # 左キューalの最大値が中央値になっている
        # 左キューにheappushする際,負値に変換したので,heappopで最大値*(-1)が求まる
    else:
        # 求値クエリ
        x = heappop(al)  # 中央値
        heappush(al, x)
        x *= (-1)
        cnt_l = (update_count + 1) // 2
        cnt_r = update_count - cnt_l
        print(x, (cnt_l * x - sl) + (sr - cnt_r * x) + b)

Submission status: Accepted

Solution code:
from heapq import heappop,heappush
Q=int(input())
M=[input() for i in range(Q)]
A=[]
#heappush(A,-10**10)
B=[]
#heappush(B,10**10)
a=0
b=0
sumb=0
n=0
for q in M:
    if q[0]=='1':
        num,x,y=map(int,q.split())
        sumb+=y
        n+=1
        m=(n+1)//2
        if n==1:
            heappush(A,-x)
            a+=x
            #print(a, b, sumb, n, m, A, B)
            continue
        if -A[0]>=x:
            heappush(A,-x)
            a+=x
            if len(A)>m:
                z=heappop(A)
                a+=z
                heappush(B,-z)
                b+=-z
        else:
            heappush(B,x)
            b+=x
            if len(B)>n-m:
                z=heappop(B)
                b-=z
                heappush(A,-z)
                a+=z
        #print(a,b,sumb,n,m,A,B)
    else:
        if n%2==0:
            print(-A[0],sumb+b-a)
        else:
            print(-A[0],sumb+b-a-A[0])

Submission status: Accepted

Solution code:
import heapq
import sys
input = sys.stdin.readline
Q = int(input())
Query = [[int(_) for _ in input().split()] for _ in range(Q)]
H1 = []
H2 = []
a = b = sum1 = sum2 = 0
for query in Query:
    if query[0] == 1:
        m0 = query[1]
        if len(H1) == len(H2):
            if len(H2):
                m1 = heapq.heappop(H2)
                sum2 -= m1
                m0, m1 = sorted([m0, m1])
                heapq.heappush(H1, -m0)
                heapq.heappush(H2, m1)
                sum1 -= m0
                sum2 += m1
            else:
                heapq.heappush(H1, -m0)
                sum1 -= m0
        else:
            m1 = -heapq.heappop(H1)
            sum1 += m1
            m0, m1 = sorted([m0, m1])
            heapq.heappush(H1, -m0)
            heapq.heappush(H2, m1)
            sum1 -= m0
            sum2 += m1
        b += query[2]
    else:
        x = -H1[0]
        print(x, x * len(H1) + sum1 + sum2 - x * len(H2) + b)

Submission status: Accepted

Solution code:
from heapq import*
L,R=[],[]
B=t=0
Q,*E=open(0)
for e in E:
	if' 'in e:_,a,b=map(int,e.split());t^=1;a*=2*t-1;c=heappushpop([L,R][t],a);heappush([R,L][t],-c);B+=b+a-c-c
	else:print(-L[0],B-L[0]*t)

Submission status: Accepted

Solution code:
# youtube
from heapq import *

q = int(input())
sum_a = 0
sum_b = 0
L = []
R = []
for _ in range(q):
    qi = list(map(int, input().split()))
    if qi[0] == 1:
        a = qi[1]
        heappush(L, -a)
        heappush(R, a)
        if -L[0] > R[0]:
            LtoR = -heappop(L)
            RtoL = heappop(R)
            sum_a += LtoR - RtoL
            heappush(L, -RtoL)
            heappush(R, LtoR)
        sum_b += qi[2]
    else:
        print(-L[0], sum_a + sum_b)

Submission status: Accepted

Solution code:
import heapq
import sys

inp = sys.stdin.readline

Q = int(input())
B = 0

q, a, b = map(int, inp().split())
L, R = [-a], [a]
L_sum, R_sum = a, a
B = b

for i in range(Q - 1):
    q = list(map(int, inp().split()))
    if len(q) == 3:
        A = q[1]
        B += q[2]

        if A <= -L[0]:
            heapq.heappush(L, -A)
            M = -heapq.heappushpop(L, -A)
            heapq.heappush(R, M)

            L_sum += (2 * A - M)
            R_sum += M
        else:
            heapq.heappush(R, A)
            M = heapq.heappushpop(R, A)
            heapq.heappush(L, -M)

            R_sum += (2 * A - M)
            L_sum += M
    else:
        N = len(L)
        X = -L[0]
        print(X, (abs(L_sum - X * N) + abs(R_sum - X * N)) // 2 + B)

Submission status: Accepted

Solution code:
from heapq import*

class median_tree(object):
    def __init__(self):
        self.lower_than_med=[]
        self.higher_than_med=[]
        self.count=0
        self.low=0
        self.high=0
        self.median=0
    def push(self,x):
        self.count=1-self.count
        if self.count:
            y=heappushpop(self.higher_than_med,x)
            heappush(self.lower_than_med,-y)
            self.low +=-y
            self.high+=x-y
        else:
            y=heappushpop(self.lower_than_med,-x)
            heappush(self.higher_than_med,-y)
            self.low -=x+y
            self.high-=y
        self.median=-self.lower_than_med[0]

from heapq import*
n=int(input())
m=median_tree()

B=0
for i in range(n):
    I=input()
    if I[0]=="1":
        a,b,c=map(int,I.split())
        B+=c
        m.push(b)
    else:
        print(m.median,m.count*m.median+B+m.low+m.high)

Submission status: Accepted

Solution code:
from heapq import*

class median_tree(object):
    def __init__(self):
        self.lower_than_med=[]
        self.higher_than_med=[]
        self.count=0
        self.low=0
        self.high=0
        self.median=0
    def push(self,x):
        self.count=1-self.count
        if self.count:
            y=heappushpop(self.higher_than_med,x)
            heappush(self.lower_than_med,-y)
            self.low +=-y
            self.high+=x-y
        else:
            y=heappushpop(self.lower_than_med,-x)
            heappush(self.higher_than_med,-y)
            self.low -=x+y
            self.high-=y
        self.median=-self.lower_than_med[0]

from heapq import*
n=int(input())
m=median_tree()

B=0
for i in range(n):
    I=input()
    if I[0]=="1":
        a,b,c=map(int,I.split())
        B+=c
        m.push(b)
    else:
        print(m.median,m.count%2*m.median+B+m.low+m.high)

Submission status: Accepted

Solution code:
from heapq import*
L,R=[],[]
n=int(input())
B=0
cnt=0
l,r=0,0
for i in range(n):
    I=input()
    if I[0]=="1":
        a,b,c=map(int,I.split())
        B+=c
        cnt+=1
        
        if cnt%2:
            x=heappushpop(R,b)
            r+=b-x
            heappush(L,-x)
            l+=-x
        else:
            x=heappushpop(L,-b)
            l+=-b-x
            heappush(R,-x)
            r+=-x
        #print(L,R,l,r)
        #print(b,x)
    else:
        med=-L[0]
#        print(L,R)
#        print(l,r)
        print(med,cnt%2*med+B+l+r)

Submission status: Accepted

Solution code:
import heapq as h
q_num=int(input())
bsum=0
high=[]
low=[]
asum=0
for iii in range(q_num):
  q=[int(_) for _ in input().split()]
  if q[0]==1:
    bsum+=q[2]
    if len(low)==0:
      h.heappush(low,-q[1])
    else:
      asum+=abs(q[1]+low[0])
      if -low[0]>=q[1]:
        h.heappush(low,-q[1])
      else:
        h.heappush(high,q[1])
      
    if len(low)>=len(high)+2:
      ltoh=h.heappop(low)
      h.heappush(high, -ltoh)
    elif len(high)>=len(low)+1:
      ltoh=h.heappop(high)
      asum+=-low[0]-ltoh
      h.heappush(low, -ltoh)
  else:
    print(-low[0],asum+bsum)