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:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import heapq


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


###############################################################################


def main():
    qnum = intin()
    qlist = [input() for _ in range(qnum)]

    lalist = []
    ralist = []
    acount = 0
    lsum = 0
    rsum = 0
    bsum = 0

    for q in qlist:

        if q[0] == '1':
            q = q.split(' ')
            a = int(q[1])
            b = int(q[2])

            bsum += b

            if acount:
                _, prev_med = lalist[0]
                if acount & 1:
                    acount += 1
                    if a <= prev_med:
                        heapq.heappop(lalist)
                        heapq.heappush(lalist, (-a, a))
                        heapq.heappush(ralist, (prev_med, prev_med))
                        _, med = lalist[0]
                        lsum -= (abs(prev_med - med) * ((acount - 1) // 2))
                        lsum += abs(med - a)
                        rsum += (abs(prev_med - med) * (acount // 2))
                    else:
                        heapq.heappush(ralist, (a, a))
                        _, med = lalist[0]
                        rsum += abs(med - a)
                else:
                    acount += 1
                    if a < prev_med:
                        heapq.heappush(lalist, (-a, a))
                        _, med = lalist[0]
                        lsum += abs(med - a)
                    else:
                        _, tmp = heapq.heappop(ralist)
                        l = min(a, tmp)
                        r = max(a, tmp)
                        heapq.heappush(ralist, (r, r))
                        heapq.heappush(lalist, (-l, l))
                        _, med = lalist[0]
                        lsum += (abs(med - prev_med) * (acount // 2))
                        rsum -= (abs(med - prev_med) * (acount // 2))
                        rsum += abs(a - med)

            else:
                lalist.append((-a, a))
                acount += 1

        if q[0] == '2':
            _, med = lalist[0]
            print('%d %d' % (med, lsum + rsum + bsum))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
Q = int(input())
Y = []
D = []
S = [0] * Q
s = 0
for i in range(Q):
    a = list(map(int, input().split()))
    Y.append(a)
    if a[0] == 1:
        D.append(a[1])
        s += a[2]
    S[i] = s

D = sorted(list(set(D)))
INV = {}
for i in range(len(D)):
    INV[D[i]] = i

N = 17
X = [0] * (2**(N+1)-1)
C = [0] * (2**(N+1)-1)

def add(j, x):
    i = 2**N + j - 1
    while i >= 0:
        X[i] += x
        C[i] += 1
        i = (i-1) // 2

def rangesum(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += X[l-1]
            l += 1
        if r%2:
            r -= 1
            s += X[r-1]
        l >>= 1
        r >>= 1
    return s
def rangecnt(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += C[l-1]
            l += 1
        if r%2:
            r -= 1
            s += C[r-1]
        l >>= 1
        r >>= 1
    return s

c = 0
su = 0
CC = [0] * (2**N)
for i in range(Q):
    y = Y[i]
    if y[0] == 1:
        add(INV[y[1]], y[1])
        CC[INV[y[1]]] += 1
        c += 1
        su += y[1]
    else:
        l, r = 0, 2**N
        while True:
            m = (l+r)//2
            rc = rangecnt(0, m)
            if rc >= (c+1)//2:
                r = m
            elif rc + CC[m] <= (c-1)//2:
                l = m
            else:
                break
        rs = rangesum(0, m)
        print(D[m], D[m]*rc-rs+(su-rs)-(c-rc)*D[m]+S[i])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
Q = int(input())
Y = []
D = []
S = [0] * Q
s = 0
for i in range(Q):
    a = list(map(int, input().split()))
    Y.append(a)
    if a[0] == 1:
        D.append(a[1])
        s += a[2]
    S[i] = s

D = sorted(list(set(D)))
INV = {}
for i in range(len(D)):
    INV[D[i]] = i

N = 17
X = [0] * (2**(N+1)-1)
C = [0] * (2**(N+1)-1)

def add(j, x):
    i = 2**N + j - 1
    while i >= 0:
        X[i] += x
        C[i] += 1
        i = (i-1) // 2

def rangeof(i):
    s = (len(bin(i+1))-3)
    l = ((i+1) - (1<<s)) * (1<<N-s)
    r = l + (1<<N-s)
    return (l, r)

def rangesum(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += X[l-1]
            l += 1
        if r%2:
            r -= 1
            s += X[r-1]
        l >>= 1
        r >>= 1
    return s
def rangecnt(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += C[l-1]
            l += 1
        if r%2:
            r -= 1
            s += C[r-1]
        l >>= 1
        r >>= 1
    return s

c = 0
su = 0
CC = [0] * (2**N)
for i in range(Q):
    y = Y[i]
    if y[0] == 1:
        add(INV[y[1]], y[1])
        CC[INV[y[1]]] += 1
        c += 1
        su += y[1]
    else:
        l, r = 0, 2**N
        while True:
            m = (l+r)//2
            rc = rangecnt(0, m)
            if rc >= (c+1)//2:
                r = m
            elif rc + CC[m] <= (c-1)//2:
                l = m
            else:
                break
        rs = rangesum(0, m)
        print(D[m], D[m]*rc-rs+(su-rs)-(c-rc)*D[m]+S[i])

Submission status: Accepted

Solution code:
Q = int(input())
INF = 10 ** 9

queries = []
for _ in range(Q):
    queries.append(input())

from bisect import insort_left
from heapq import heappush, heappop

absum, bsum, c = 0, 0, 0
# left, right = -INF, INF
lefts, rights = [INF], [INF]
for query in queries:
    if query[0] == '2':
        # absum = sum(abs(a - ary[(c-1)//2]) for a in ary)
        # absum = asum - (c//2 + 1) * ary[(c-1)//2]
        # print(' '.join(map(str, (ary[(c-1)//2], absum + bsum))))
        # print(str(ary[(c-1)//2]) + ' ' + str(absum + bsum))
        print(-lefts[0], absum + bsum)
    else:
        _, a, b = map(int, query.split())
        if c % 2 == 0:
            if -lefts[0] <= a <= rights[0]:
                heappush(lefts, -a)
                heappush(rights, a)
                absum += 0
            elif a < -lefts[0]:
                heappush(lefts, -a)
                heappush(rights, -lefts[0])
                absum += -lefts[0] - a
            else:
                heappush(rights, a)
                heappush(lefts, -rights[0])
                absum += a - rights[0]
        else:
            if a < -lefts[0]:
                absum += -lefts[0] - a
                heappop(lefts)
                heappush(lefts, -a)
            else:
                absum += a - rights[0]
                heappop(rights)
                heappush(rights, a)
        # left, right = ary[(c-1)//2], ary[c - (c-1)//2 - 1]        
        bsum += b
        c += 1

Submission status: Accepted

Solution code:
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        t1 = heappushpop(L, -a[1])
        t2 = heappushpop(R, a[1])
        heappush(L, -t2)
        heappush(R, -t1)
        ans += a[2] + abs(t2+t1)
    else:
        print(-L[0], ans)

Submission status: Accepted

Solution code:
import heapq

Q = int(input())
b_sum = 0
a_list = []
left = []
right = []
res = 0

for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        b_sum += query[2]

        if not left:
            a = query[1]
            heapq.heappush(left, -a)
        else:
            res += abs(query[1] + left[0])
            if query[1] <= -left[0]:
                heapq.heappush(left, -query[1])
            else:
                heapq.heappush(right, query[1])

        if len(left) < len(right):
            x = heapq.heappop(right)
            y = -left[0]
            res += y - x
            heapq.heappush(left, -x)
        elif len(left) > len(right) + 1:
            x = - heapq.heappop(left)
            if len(right) != 0:
                y = right[0]
            else:
                y = 0
            heapq.heappush(right, x)
    else:
        print(-left[0], res + b_sum)

Submission status: Accepted

Solution code:
import heapq
Q = int(input())

lefts, rights = [], []  # lefts:aの左半分（中央値含む, 降順） rights:aの右半分
diff = 0    # aの中央値からの差の絶対値
bsum = 0    # bの累計

for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:   # 更新クエリ(1)の処理
        a = query[1]
        bsum += query[2]
        if len(lefts) == 0:
            heapq.heappush(lefts, -a)   # 最初はleftsに降順に格納（中央値）
        else:
            if a <= -lefts[0]:
                heapq.heappush(lefts, -a)   # 現在の中央値以下ならleftsに降順にpush
                diff -= a + lefts[0]        # 現在の中央値(-lefts[0])との差
            else:
                heapq.heappush(rights, a)   # 現在の中央値より大きいならrightsにpush
                diff += a + lefts[0]        # 現在の中央値(-lefts[0])との差
        # lefts が rights と同数か1多い状態をキープするため、ずれたら移動
        if len(lefts) < len(rights):    # 偶数状態からrightsにpushされ、rightsが1件多い、奇数状態
            med = heapq.heappop(rights) # 新しい中央値をrightsからpop
            diff -= med + lefts[0]      # 奇数なので、rightsの1件以外相殺されるから、新中央値と旧中央値の差を加える
            heapq.heappush(lefts, -med) # 新しい中央値をleftsにpush
        elif len(lefts) > len(rights) + 1:  # 奇数状態からleftsにpushされ、leftsが2件多い、偶数状態
            med = -heapq.heappop(lefts)      # これまでの中央値をleftsからpop（-1倍）
            # 偶数なので、中央値がずれても相殺されるから、diffは修正不要
            heapq.heappush(rights, med)     # これまでの中央値をrightsにpush
    else:   # 求値クエリ(2)の処理
        x = -lefts[0]       # 中央値がx
        fx = diff + bsum    # 中央値からの差の絶対値(diff)とbの累計(bsum)の和
        print(x, fx)

Submission status: Accepted

Solution code:
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from heapq import heappush,heappop
q=int(input())
L,R=[],[]
s,c=0,0
for _ in range(q):
    query=tuple(map(int,input().split()))
    if query[0]==1:
        _,a,b=query
        c+=b
        if len(L)==0 or a<=-L[0]:
            heappush(L,-a)
            s+=-L[0]-a
        else:
            heappush(R,a)
            s+=a+L[0]
        if len(R)>len(L):
            s+=-R[0]-L[0]
            heappush(L,-heappop(R))
        if len(L)-len(R)==2:
            heappush(R,-heappop(L))
    else:
        print(-L[0],s+c)

Submission status: Accepted

Solution code:
import heapq
N = int(input())
st = input()
n, a, b = map(int,st.split())

minvalue = b
left = [-a]
right = [a]
ptnum = 1
for i in range(N-1):
  st = input()
  if st[0] == '1':#入力クエリ
    n, a, b = map(int,st.split())
    if ptnum%2 == 1:#lrは一致
      l = - heapq.heappop(left)
      r = heapq.heappop(right)
      le = min(l,a)
      ri = max(a,r)
      heapq.heappush(left, -le)
      heapq.heappush(right, ri)
      minvalue += abs(l-a)
    else:#lr は必ずしも一致しない
      if a < -left[0]:#中央がlに
        heapq.heappush(left, -a)
        heapq.heappush(right, -left[0])
        minvalue -= a+left[0]
      elif a > right[0]:#中央がrに
        heapq.heappush(left, -right[0])
        heapq.heappush(right, a)
        minvalue += a-right[0]
      else:
        heapq.heappush(left, -a)
        heapq.heappush(right, a)
        
    minvalue += b
    ptnum += 1

  else:
    print(- left[0], minvalue)

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-t;a*=2*t-1;c=heappushpop([L,R][t],a);heappush([R,L][t],-c);B+=b+a-c-c
	else:w=-L[0];print(w,B+w*t)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
Q=int(input())
ans=0
from heapq import heappush,heappop
left=[]
right=[]
L=-1*float('inf')
R=float('inf')
for i in range(Q):
    q=input()
    if q=="2":
        print(" ".join([str(L),str(ans)]))
    else:
        s,a,b=map(int,q.split())
        ans+=b
        if L<=a<=R:
            heappush(left,-1*a)
            heappush(right,a)
            L=a
            R=a
        elif a<L:
            ans+=L-a
            x=heappop(left)
            heappush(right,-1*x)
            heappush(left,-1*a)
            heappush(left,-1*a)
        elif R<a:
            ans+=a-R
            x=heappop(right)
            heappush(left,-1*x)
            heappush(right,a)
            heappush(right,a)
        R=right[0]
        L=-1*left[0]

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Submission status: Accepted

Solution code:
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappushpop

Q = int(input())
high = []
low = []
sum_dh, sum_dl = 0, 0
sum_b = 0
buf = []

for qi, line in enumerate(sys.stdin):
    q = list(map(int, line.split()))
    if len(q) == 1:
        x = -low[0]
        ans_l = x * len(low) - sum_dl
        ans_h = sum_dh - x * len(high)
        buf.append('{} {}\n'.format(x, ans_l + ans_h + sum_b))
    else:
        _, a, b = q
        sum_b += b
        if len(low) == len(high):
            h = heappushpop(high, a)
            heappush(low, -h)
            sum_dh += a - h
            sum_dl += h
        else:
            l = -heappushpop(low, -a)
            heappush(high, l)
            sum_dl += a - l
            sum_dh += l

print(''.join(buf))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappushpop

Q = int(input())
high = []
low = []
sum_dh, sum_dl = 0, 0
sum_b = 0
MIN_X = -(10 ** 9)
buf = []

for qi, line in enumerate(sys.stdin):
    q = list(map(int, line.split()))
    if len(q) == 1:
        x = -low[0]
        dx = x - MIN_X
        ans_l = dx * len(low) - sum_dl
        ans_h = sum_dh - dx * len(high)
        buf.append('{} {}\n'.format(x, ans_l + ans_h + sum_b))
    else:
        _, a, b = q
        da = a - MIN_X
        sum_b += b
        if len(low) == len(high):
            h = heappushpop(high, a)
            heappush(low, -h)
            dh = h - MIN_X
            sum_dh += da - dh
            sum_dl += dh
        else:
            l = -heappushpop(low, -a)
            heappush(high, l)
            dl = l - MIN_X
            sum_dl += da - dl
            sum_dh += dl

print(''.join(buf))

Submission status: Accepted

Solution code:
Q = int(input())

import heapq
import sys
input = sys.stdin.readline

INF = 10**18
L = []
R = []
heapq.heappush(L,INF)
heapq.heappush(R,INF)
L_size = 0
R_size = 0
sum_b = 0
sum_L = 0
sum_R = 0

def f1(a,b):
  global sum_b,L_size,R_size,sum_L,sum_R
  sum_b += b
  L_max = -heapq.heappop(L)
  R_min = heapq.heappop(R)
  sum_L -= L_max
  sum_R -= R_min
  X = sorted([L_max,a,R_min])
  if L_size == R_size:
    heapq.heappush(L,-X[0])
    heapq.heappush(L,-X[1])
    heapq.heappush(R,X[2])
    L_size += 1
    sum_L += X[0] + X[1]
    sum_R += X[2]
  else:
    heapq.heappush(L,-X[0])
    heapq.heappush(R,X[1])
    heapq.heappush(R,X[2])
    R_size += 1
    sum_L += X[0]
    sum_R += X[1] + X[2]
  return

def f2():
  min_value = sum_b
  min_x = -L[0]
  if L_size == R_size:
    min_value += sum_R - sum_L
  else:
    min_value += sum_R - sum_L + min_x
  print(min_x,min_value)

for _ in range(Q):
  q = [int(x) for x in input().split()]
  if q[0]&1:
    f1(q[1],q[2])
  else:
    f2()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop

num_q = int(input())
x = 0
f = 0
low = []
high = []
odd = False
num_a = 0
 
for _ in range(num_q):
  q = input()
  if q[0] == '2':
    print(x, f)
  else:
    _, n, m = map(int, q.split(" "))
    if odd:
      if n >= -low[0]:
        f += m + n + low[0]
        heappush(high, n)
      else:
        f += m - n - low[0]
        heappush(low, -n)
        heappush(high, -heappop(low))
    else:
      f += m
      if num_a == 0:
        heappush(low, -n)
      else:
        if n > -low[0]:
          if n > high[0]:
            f += n - high[0]
          heappush(high, n)
          heappush(low, -heappop(high))
        else:
          f += - n - low[0]
          heappush(low, -n)

    odd = ~odd
    x = -low[0]
    num_a += 1

Submission status: Accepted

Solution code:
Q = int(input())

import heapq
import sys
input = sys.stdin.readline

INF = 10**18
L = []
R = []
heapq.heappush(L,INF)
heapq.heappush(R,INF)
L_size = 0
R_size = 0
sum_b = 0
sum_L = 0
sum_R = 0

def f1(a,b):
  global sum_b,L_size,R_size,sum_L,sum_R
  sum_b += b
  L_max = -heapq.heappop(L)
  R_min = heapq.heappop(R)
  sum_L -= L_max
  sum_R -= R_min
  X = sorted([L_max,a,R_min])
  if L_size == R_size:
    heapq.heappush(L,-X[0])
    heapq.heappush(L,-X[1])
    heapq.heappush(R,X[2])
    L_size += 1
    sum_L += X[0] + X[1]
    sum_R += X[2]
  else:
    heapq.heappush(L,-X[0])
    heapq.heappush(R,X[1])
    heapq.heappush(R,X[2])
    R_size += 1
    sum_L += X[0]
    sum_R += X[1] + X[2]
  return

def f2():
  min_value = sum_b
  min_x = -L[0]
  if L_size == R_size:
    min_value += sum_R - sum_L
  else:
    min_value += sum_R - sum_L + min_x
  print(min_x,min_value)

QUERY = [[int(x) for x in input().split()] for _ in range(Q)]
for q in QUERY:
  if q[0]&1:
    f1(q[1],q[2])
  else:
    f2()

Submission status: Accepted

Solution code:
import heapq
Q = int(input())

heapL, heapR = [], []
s = 0
x = None
for i in range(Q):
    q = list(map(int, input().split()))
    if len(q) == 1:
        print(x, s)
    else:
        _, a, b = q
        s += b
        if not heapL:
            heapq.heappush(heapL, -a)
        elif not heapR:
            s += abs(a + heapL[0])
            heapq.heappush(heapR, -heapq.heappushpop(heapL, -a))
        else:
            if len(heapL) > len(heapR):
                s += abs(a + heapL[0])
                heapq.heappush(heapR, -heapq.heappushpop(heapL, -a))
            else:
                heapq.heappush(heapL, -heapq.heappushpop(heapR, a))
                s += abs(a + heapL[0])
        x = -heapL[0]

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from heapq import heappush, heappop

def main():
    q = II()
    Q = []
    for _ in range(q):
        Q.append(LI())
    lows, highs = [], []
    slow, shigh = 0, 0
    bias = 0
    for q in Q:
        if q[0] == 2:
            odd = -lows[0] if len(highs) != len(lows) else 0
            ans = -lows[0], bias + shigh - slow + odd
            print(*ans)
        else:
            _, a, b = q
            bias += b
            if not lows:
                lows = [-a]
                slow = a
            else:
                if a <= -lows[0]:
                    heappush(lows, -a)
                    slow += a
                    if len(lows) > len(highs) + 1:
                        x = -heappop(lows)
                        heappush(highs, x)
                        slow -= x
                        shigh += x
                else:
                    heappush(highs, a)
                    shigh += a
                    if len(highs) > len(lows):
                        x = heappop(highs)
                        heappush(lows, -x)
                        shigh -= x
                        slow += x
    return

main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import sys
readline = sys.stdin.readline
ans = []
Q = int(readline())

QL = [10**10]
QR = [10**10]
R = 0

for i in range(Q):
    t, *A = map(int, input().split())
    if t == 1:
        a, b = A
        R += b
        if -QL[0] <= a <= QR[0]:
            heappush(QL, -a)
            heappush(QR, a)
        elif a <= -QL[0]:
            heappush(QL, -a)
            heappush(QL, -a)
            c = heappop(QL)
            R -= c + a
            heappush(QR, -c)
        elif QR[0] <= a:
            heappush(QR, a)
            heappush(QR, a)
            c = heappop(QR)
            R += a - c
            heappush(QL, -c)
    else:
        ans.append("%d %d\n" % (-QL[0], R))
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
import heapq


class PriorityQueue:
    def __init__(self):
        self.__heap = []
        self.__count = 0

    def empty(self) -> bool:
        return self.__count == 0

    def dequeue(self):
        if self.empty():
            raise Exception('empty')
        self.__count -= 1
        return heapq.heappop(self.__heap)

    def enqueue(self, v):
        self.__count += 1
        heapq.heappush(self.__heap, v)

    def __len__(self):
        return self.__count


def absolute_minima():
    Q = int(input())

    L, R = PriorityQueue(), PriorityQueue()
    sL, sR = 0, 0
    M = None
    B = 0
    N = 0

    for _ in range(Q):
        query = [int(s) for s in input().split()]

        if query[0] == 1:
            _, a, b = query
            B += b
            N += 1

            if M is None:
                M = a
            elif M < a:
                R.enqueue(a)
                sR += a
            else:
                L.enqueue(-a)
                sL += a

            while len(R)-len(L) > 1:
                L.enqueue(-M)
                sL += M

                M = R.dequeue()
                sR -= M

            while len(L) > len(R):
                R.enqueue(M)
                sR += M

                M = -L.dequeue()
                sL -= M

        else:
            s = - sL + sR + B
            if N % 2 == 0:
                s -= M
            print(M, s)


if __name__ == "__main__":
    absolute_minima()

Submission status: Accepted

Solution code:
Q = int(input())
Queries = [list(map(int, input().split())) for _ in range(Q)]

import heapq as hp
L = [-(-(10**9+1))] # 最小ヒープなので負を入れる
R = [10**9+1] # そのまま
hp.heapify(L)
hp.heapify(R)
odd = False
sum_b = 0
sum_l = 0
sum_r = 0

for q in Queries:
  if len(q)==1 and q[0]==2:
    print(*(-L[0], sum_b + abs(sum_l-sum_r)))
    
  else:
    _, a, b = q
    sum_b += b

    l = -hp.heappop(L)
    r = hp.heappop(R)
    
    X = sorted([l, r, a])
    if odd==False:
      sum_l -= l
      sum_r -= r
      hp.heappush(L,-X[0])
      hp.heappush(L,-X[1])
      hp.heappush(R,X[2])
      sum_l += X[0]
      sum_r += X[2]
      odd=True
    elif odd==True:
      sum_r -= r
      hp.heappush(L,-X[0])
      hp.heappush(R,X[1])
      hp.heappush(R,X[2])
      sum_l += X[0]
      sum_r += X[1] + X[2]
      odd=False

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
from heapq import heappush,heappop
q=int(input())
_,a,b=map(int,input().split())
Small=[-a]; Large=[]
cnt=1
med=a
c=b
s=0
for i in range(q-1):
    query=tuple(map(int,input().split()))
    if query[0]==1:
        _,a,b=query
        c+=b
        if cnt%2:
            s+=abs(a-med)
            if a<-Small[0]:
                heappush(Small,-a)
                heappush(Large,-heappop(Small))
            else:
                heappush(Large,a)
        else:
            if a<=-Small[0]:
                heappush(Small,-a)
                med=-Small[0]
                s+=med-a
            elif a<=Large[0]:
                heappush(Small,-a)
            else:
                heappush(Large,a)
                med=heappop(Large)
                s+=a-med
                heappush(Small,-med)
        cnt+=1
        med=-Small[0]
    else:
        print(med,s+c)

Submission status: Accepted

Solution code:
from heapq import *

q = int(input())
b_cur = 0
diff = 0
lefts, rights = [], []

for i in range(q):
    query = input()
    if len(query) > 1:
        _, a, b = map(int, query.split())
        b_cur += b
        if len(lefts) == 0:
            heappush(lefts, -a)
        else:
            diff += abs(a + lefts[0])
            if a <= -lefts[0]:
                heappush(lefts, -a)
            else:
                heappush(rights, a)

        if len(lefts) < len(rights):
            med_r = heappop(rights)
            med_l = -lefts[0]
            diff += med_l - med_r
            heappush(lefts, -med_r)
        elif len(lefts) > len(rights)+1:
            med_l = -heappop(lefts)
            med_r = rights[0] if len(rights) > 0 else 0
            heappush(rights, med_l)
            
    else:
        print(-lefts[0], b_cur+diff)