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 collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size
        self.total = 0

    def add(self, i, w):
        x = i + 1
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        self.total += w
        return

    def sum(self, i):
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res

    def interval_sum(self, i, j):  # i <= x < j の区間
        return self.sum(j - 1) - self.sum(i - 1)

    def search(self, k):
        if k > self.total:
            return -1
        if k == 0:
            return 0
        step = 1 << (self.size.bit_length() - 1)
        now_index = 0
        ret = 0
        while step:
            if now_index + step < self.size and ret + self.bit[now_index + step - 1] < k:
                ret += self.bit[now_index + step - 1]
                now_index += step
            step >>= 1
        # now_indexを伸ばしいって、sumがk以上に達する直前まで伸ばし続けるならreturnのところでnow_index - 1。
        # その場合、bit.sum(now_index - 1) <= k < bit.sum(now_index)
        # 達してすぐのindexであれば-1しない
        return now_index


q = I()
query = []
A = []
for _ in range(q):
    l = LI()
    if l[0] == 1:
        A += [l[1]]
    query += [l]

ind_to_co = sorted(set(A))
co_to_ind = {}
for i, x in enumerate(ind_to_co):
    co_to_ind[x] = i

bit1 = BIT(len(A))
bit2 = BIT(len(A))
n = 0
B = 0
for l in query:
    if l[0] == 1:
        B += l[2]
        bit1.add(co_to_ind[l[1]], 1)
        bit2.add(co_to_ind[l[1]], l[1])
        n += 1
    else:
        center_ind = bit1.search(n // 2 + bool(n % 2))
        d = n // 2 + bool(n % 2)
        min_val = bit2.interval_sum(center_ind + 1, bit2.size) - bit1.interval_sum(center_ind + 1, bit1.size) * \
                  ind_to_co[center_ind] + bit1.sum(center_ind - 1) * ind_to_co[center_ind] - bit2.sum(center_ind - 1) + B
        print(ind_to_co[center_ind], min_val)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


class BIT:
    def __init__(self, size):
        self.bit = [0] * size
        self.size = size
        self.total = 0

    def add(self, i, w):
        x = i + 1
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        self.total += w
        return

    def sum(self, i):
        res = 0
        x = i + 1
        while x:
            res += self.bit[x - 1]
            x -= x & -x
        return res

    def interval_sum(self, i, j):  # i <= x < j の区間
        return self.sum(j - 1) - self.sum(i - 1)

    def search(self, k):
        if k > self.total:
            return -1
        if k == 0:
            return 0
        step = 1 << (self.size.bit_length() - 1)
        now_index = 0
        ret = 0
        while step:
            if now_index + step < self.size and ret + self.bit[now_index + step - 1] < k:
                ret += self.bit[now_index + step - 1]
                now_index += step
            step >>= 1
        # now_indexを伸ばしいって、sumがk以上に達する直前まで伸ばし続けるならreturnのところでnow_index - 1。
        # その場合、bit.sum(now_index - 1) <= k < bit.sum(now_index)
        # 達してすぐのindexであれば-1しない
        return now_index


q = I()
query = []
A = []
for _ in range(q):
    l = LI()
    if l[0] == 1:
        A += [l[1]]
    query += [l]

ind_to_co = sorted(set(A))
co_to_ind = {}
for i, x in enumerate(ind_to_co):
    co_to_ind[x] = i

bit1 = BIT(len(A))
bit2 = BIT(len(A))
n = 0
B = 0
for l in query:
    if l[0] == 1:
        B += l[2]
        bit1.add(co_to_ind[l[1]], 1)
        bit2.add(co_to_ind[l[1]], l[1])
        n += 1
    else:
        min_ind = bit1.search(n // 2 + bool(n % 2))
        d = n // 2 + bool(n % 2)
        min_val = bit2.interval_sum(min_ind + 1, bit2.size) - bit1.interval_sum(min_ind + 1, bit1.size) * ind_to_co[
            min_ind] \
                  + bit1.sum(min_ind - 1) * ind_to_co[min_ind] - bit2.sum(min_ind - 1) + B
        print(ind_to_co[min_ind], min_val)

Submission status: Accepted

Solution code:
def main():
    import heapq
    left=[10**20]
    right=[10**20]
    heapq.heapify(left)
    heapq.heapify(right)
    q=int(input())
    ans=0
    cnt=0
    left_sum=0
    right_sum=0
    for _ in range(q):
        temp=input()
        if temp[0]=="1":
            cnt+=1
            x,a,b=map(int,temp.split())
            l=-heapq.heappop(left)
            r=heapq.heappop(right)
            left_sum-=l
            right_sum-=r
            ans+=b
            ll,aa,rr=sorted([l,a,r])
            heapq.heappush(right,rr)
            right_sum+=rr
            heapq.heappush(left,-ll)
            left_sum+=ll
            if len(left)==len(right):
                heapq.heappush(left,-aa)
                left_sum+=aa
            else:
                heapq.heappush(right,aa)
                right_sum+=aa
        else:
            i=-heapq.heappop(left)
            left_sum-=i
            print(i,ans+(right_sum-(len(right)-1)*i)+((len(left)-1)*i-left_sum))
            heapq.heappush(left,-i)
            left_sum+=i
main()

Submission status: Accepted

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

q=int(input())
query=[list(map(int,input().split())) for _ in range(q)]
inf=10**12
left=[inf]
left_sum=0
right=[inf]
right_sum=0

cnt=0
b=0
for i in range(q):
    if query[i][0]==1:
        a=query[i][1]
        b+=query[i][2]
        if cnt==0:
            d=right[0]
            if a>d:
                heappush(left,-heappop(right))
                heappush(right,a)
                right_sum+=a-d
                left_sum-=d
            else:
                heappush(left,-a)
                left_sum-=a
            cnt=1
        else:
            c=-left[0]
            if a<c:
                heappush(right,-heappop(left))
                heappush(left,-a)
                right_sum+=c
                left_sum+=c-a
            else:
                heappush(right,a)
                right_sum+=a
            cnt=0
    else:
        x=-left[0]
        if cnt==0:
            print(x,b+right_sum+left_sum)
        else:
            print(x,b+x+right_sum+left_sum)

Submission status: Accepted

Solution code:
from heapq import heappop,heappush
q=int(input())
query=[list(map(int,input().split())) for _ in range(q)]
inf=10**12
left=[inf]
left_sum=0
right=[inf]
right_sum=0

cnt=0
b=0
for i in range(q):
    if query[i][0]==1:
        a=query[i][1]
        b+=query[i][2]
        if cnt==0:
            d=right[0]
            if a>d:
                heappush(left,-heappop(right))
                heappush(right,a)
                right_sum+=a-d
                left_sum-=d
            else:
                heappush(left,-a)
                left_sum-=a
            cnt=1
        else:
            c=-left[0]
            if a<c:
                heappush(right,-heappop(left))
                heappush(left,-a)
                right_sum+=c
                left_sum+=c-a
            else:
                heappush(right,a)
                right_sum+=a
            cnt=0
    else:
        x=-left[0]
        if cnt==0:
            print(x,b+right_sum+left_sum)
        else:
            print(x,b+x+right_sum+left_sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop, heapify

N=input()

hq1=[]  #中央値より小さい値を入れるヒープキュー
hq2=[]  #中央値より大きい値を入れるヒープキュー
fx=0     #f(x)の最小値
for _ in range(N):
    q=map(int, sys.stdin.readline().split()) 

    if q[0]==1:
        q_type=q[0]
        a=q[1]
        b=q[2]
        heappush(hq1,a*-1)
        heappush(hq2,a)
        if hq2[0]<hq1[0]*-1:
            hq1_max=heappop(hq1)*-1
            hq2_min=heappop(hq2)
            heappush(hq1,hq2_min*-1)
            heappush(hq2,hq1_max)
            fx+=abs(hq1_max-hq2_min)
        fx+=b
    elif q[0]==2:
        print hq1[0]*-1,fx

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 10**18
#A
def A():
    a, b = LI()
    if a <= 5:
        print(0)
    elif 6 <= a <= 12:
        print(b // 2)
    else:
        print(b)
    return

#B
def B():
    r, d, x = LI()
    for i in range(10):
        print(x * r - d)
        x = x * r - d
    return

#C
def C():
    n, m = LI()
    imo = [0 for i in range(n+1)]
    for _ in range(m):
        l, r = LI_()
        imo[l] += 1
        imo[r + 1] -= 1
    for i in range(1,n):
        imo[i] += imo[i - 1]
    ans = 0
    #print(imo)
    for i in imo:
        if i == m:
            ans += 1
    print(ans)
    return

#D
def D():
    n, m = LI()
    a = LI()
    bc = LIR(m)
    bc.sort(key=lambda x: x[1], reverse=True)
    a.sort()
    index = 0
    suma = 0
    for b, c in bc:
        if index + b >= n:
            if a[n - 1] <= c:
                suma += (n - index) * c
            else: 
                br = bisect_left(a, c)
                if br > index:
                    suma += (br - index) * c
                    index = br
                    break
                else:
                    break
            index = n
            break
        if a[index + b - 1] < c:
            suma += b * c
            index += b
        else:
            if a[index] < c:
                bl = bisect_left(a, c)
                suma += (bl - index) * c
                index = bl
            else:
                break
    suma += sum(a[index:])
    print(suma)

    return

#E
def E():
    n, m, k = LI()
    def cmb(n, r, mod):
        if ( r<0 or r>n ):
            return 0
        r = min(r, n-r)
        return g1[n] * g2[r] * g2[n-r] % mod

    g1 = [1, 1] 
    g2 = [1, 1]
    inverse = [0, 1] 

    for i in range( 2, n*m + 1 ):
        g1.append( ( g1[-1] * i ) % mod )
        inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
        g2.append((g2[-1] * inverse[-1]) % mod)
        
    ans = 0
    for d in range(1,n+1):
        ans += d*cmb(n * m - 2, k - 2, mod) * (n - d) * m ** 2 
    for d in range(1,m+1):
        ans += d*cmb(n * m - 2, k - 2, mod) * (m - d) * n ** 2
    print(ans%mod)
    

    
    return

#F
def F():
    q = II()
    small = []
    big = []
    len_small = 0
    len_big = 0
    ans = 0
    for i in range(q):
        query = LI()

        if query[0] == 1:
            a, b = query[1:3]
            ans += b
            if len_small == 0:
                heappush(small, -a)
                len_small += 1
                continue
            if len_big == 0:
                num_small = -heappop(small)
                lis = [a, num_small]
                lis.sort()
                heappush(small, -lis[0])
                heappush(big, lis[1])
                len_big += 1
                if lis[0] == a:
                    ans += num_small - lis[0]
                else:
                    ans += (a - num_small)
                continue
            num_small = -heappop(small)
            num_big = heappop(big)
            lis = [a, num_big, num_small]
            lis.sort()
            if len_big != len_small:
                heappush(small, -lis[0])
                heappush(big, lis[1])
                heappush(big, lis[2])
                if lis[0] == a:
                    ans -= (num_small - a) * (len_small - 1)
                    ans += (num_small - a) * (len_big + 1)
                else:
                    ans += a - num_small
                len_big += 1
            else:
                heappush(small, -lis[0])
                heappush(small, -lis[1])
                heappush(big, lis[2])
                if lis[0] == a:
                    ans += num_small - a
                elif lis[1] == a:
                    ans += (a - num_small) * len_small
                    ans -= (a - num_small) * len_big
                else:
                    ans += (num_big - num_small) * len_small
                    ans -= (num_big - num_small) * len_big
                    ans += a - num_big
                len_small += 1

        else:
            a = -small[0]
            print(a, ans)
    return


#Solve
if __name__ == '__main__':
    F()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 10**18
#A
def A():
    a, b = LI()
    if a <= 5:
        print(0)
    elif 6 <= a <= 12:
        print(b // 2)
    else:
        print(b)
    return

#B
def B():
    r, d, x = LI()
    for i in range(10):
        print(x * r - d)
        x = x * r - d
    return

#C
def C():
    n, m = LI()
    imo = [0 for i in range(n+1)]
    for _ in range(m):
        l, r = LI_()
        imo[l] += 1
        imo[r + 1] -= 1
    for i in range(1,n):
        imo[i] += imo[i - 1]
    ans = 0
    #print(imo)
    for i in imo:
        if i == m:
            ans += 1
    print(ans)
    return

#D
def D():
    n, m = LI()
    a = LI()
    bc = LIR(m)
    bc.sort(key=lambda x: x[1], reverse=True)
    a.sort()
    index = 0
    suma = 0
    for b, c in bc:
        if index + b >= n:
            if a[n - 1] <= c:
                suma += (n - index) * c
            else: 
                br = bisect_left(a, c)
                if br > index:
                    suma += (br - index) * c
                    index = br
                    break
                else:
                    break
            index = n
            break
        if a[index + b - 1] < c:
            suma += b * c
            index += b
        else:
            if a[index] < c:
                bl = bisect_left(a, c)
                suma += (bl - index) * c
                index = bl
            else:
                break
    suma += sum(a[index:])
    print(suma)

    return

#E
def E():
    n, m, k = LI()
    def cmb(n, r, mod):
        if ( r<0 or r>n ):
            return 0
        r = min(r, n-r)
        return g1[n] * g2[r] * g2[n-r] % mod

    g1 = [1, 1] 
    g2 = [1, 1]
    inverse = [0, 1] 

    for i in range( 2, n*m + 1 ):
        g1.append( ( g1[-1] * i ) % mod )
        inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
        g2.append((g2[-1] * inverse[-1]) % mod)
        
    ans = 0
    for d in range(1,n+1):
        ans += d*cmb(n * m - 2, k - 2, mod) * (n - d) * m ** 2 
    for d in range(1,m+1):
        ans += d*cmb(n * m - 2, k - 2, mod) * (m - d) * n ** 2
    print(ans%mod)
    

    
    return

#F
def F():
    q = II()
    small = [inf]
    big = [inf]
    len_small = 0
    len_big = 0
    ans = 0
    sum_small = 0
    sum_big = 0
    for i in range(q):
        query = LI()

        if query[0] == 1:
            a, b = query[1:3]
            ans += b
            num_small = -heappop(small)
            num_big = heappop(big)
            sum_small -= num_small
            sum_big -= num_big
            lis = [a, num_big, num_small]
            lis.sort()
            if len_big != len_small:
                heappush(small, -lis[0])
                heappush(big, lis[1])
                heappush(big, lis[2])
                len_big += 1
                sum_small += lis[0]
                sum_big += lis[1] + lis[2]
            else:
                heappush(small, -lis[0])
                heappush(small, -lis[1])
                heappush(big, lis[2])
                len_small += 1
                sum_small += lis[0] + lis[1]
                sum_big += lis[2]

        else:
            a = -small[0]
            print(a, ans + sum_big - sum_small + a * (len_big != len_small))
    return


#Solve
if __name__ == '__main__':
    F()

Submission status: Accepted

Solution code:
def main():
    from heapq import heappush, heappop, heappushpop
    import sys

    input = sys.stdin.readline

    class Median:
        def __init__(self):
            self.b = 0
            self.l = []
            self.sum_l = 0
            self.r = []
            self.sum_r = 0

        def update(self, a: int, b: int) -> None:
            self.b += b

            size = len(self.l) + len(self.r)
            if size % 2 == 0:
                x = heappushpop(self.r, a)
                self.sum_r += a - x
                heappush(self.l, -x)
                self.sum_l += x
            else:
                x = heappushpop(self.l, -a)
                self.sum_l += a + x  # a - (-x)
                heappush(self.r, -x)
                self.sum_r -= x  # += (-x)

        def query(self):
            median = -self.l[0]
            ret = (abs(len(self.l) * median - self.sum_l)
                   + abs(self.sum_r - len(self.r) * median)
                   + self.b)
            return median, ret

    med = Median()
    n = int(input())
    for _ in range(n):
        q = iter(input().rstrip().split())
        if next(q) == '1':
            med.update(*map(int, q))
        else:
            print(*med.query())


if __name__ == '__main__':
    main()

Submission status: Accepted

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

q = int(input())
a_sum = b_sum = 0
INF = 10**10
queue_l = [INF]
queue_r = [INF]
for i in range(q):
    s = input()
    if s[0] == '1':
        # update query
        _, a, b = map(int, s.split())

        ql_max = -queue_l[0]
        qr_min = queue_r[0]
        if a < ql_max:
            heappop(queue_l)
            heappush(queue_l, -a)
            heappush(queue_l, -a)
            heappush(queue_r, ql_max)
            a_sum += ql_max - a
        elif a > qr_min:
            heappop(queue_r)
            heappush(queue_r, a)
            heappush(queue_r, a)
            heappush(queue_l, -qr_min)
            a_sum += a - qr_min
        else:
            heappush(queue_l, -a)
            heappush(queue_r, a)

        b_sum += b
    else:
        # evaluation query
        ql_max = -queue_l[0]
        print(ql_max, a_sum + b_sum)

Submission status: Accepted

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


def compress_coordinate(x: list, key=None, reverse=False):
    zipped = {}
    unzipped = {}
    for i, xi in enumerate(sorted(x, key=None, reverse=reverse)):
        zipped[xi] = i + 1
        unzipped[i + 1] = xi
    return zipped, unzipped


class BIT1():
    """
    Binary Indexed Tree (1-indexed)
    """
    def __init__(self, n):
        self.n = n
        self.bit = [0] * (self.n + 1)
        self.data = [0] * (self.n + 1)

    def add(self, idx, x):
        # add x to idx-th element
        # idx: 1-indexed
        self.data[idx] += x
        while idx <= self.n:
            self.bit[idx] += x
            idx += (idx & (-idx))

    def sum(self, idx):
        # get sum of [1, idx]
        # idx: 1-indexed
        s = 0
        while idx:
            s += self.bit[idx]
            idx -= (idx & (-idx))
        return s

    def bisect_left(self, w):
        # condition : always all element is not minus
        # return minimum idx where bit.sum(idx) >= w
        if w <= 0:
            return 0
        idx = 0  # self.bit[idx] < w
        k = 1 << ((self.n).bit_length() - 1)
        while k > 0:
            if idx + k <= self.n and self.bit[idx + k] < w:
                w -= self.bit[idx + k]
                idx += k
            k = k >> 1
        return idx + 1


q = int(input())
query = []
coord = set()
for _ in range(q):
    s = input()
    if s[0] == '1':
        # update query
        _, a, b = map(int, s.split())
        coord.add(a)
        query.append((1, a, b))
    else:
        # evaluation query
        query.append((2, ))

zipped, unzipped = compress_coordinate(list(coord))
a_sum = b_sum = 0
bit = BIT1(len(coord))
for i in range(q):
    if query[i][0] == 1:
        a = query[i][1]
        b = query[i][2]

        if i > 0:
            n = bit.sum(len(coord))
            idx1 = bit.bisect_left(n // 2)
            idx2 = bit.bisect_left(n // 2 + 1)
            if a < unzipped[idx1]:
                a_sum += unzipped[idx1] - a
            elif a > unzipped[idx2]:
                a_sum += a - unzipped[idx2]

        b_sum += b
        bit.add(zipped[a], 2)
    else:
        n = bit.sum(len(coord))
        idx1 = bit.bisect_left(n // 2)
        print(unzipped[idx1], b_sum + a_sum)

Submission status: Accepted

Solution code:
class MultiSet:
    def __init__(self, n):
        self.N = n
        self.n = 1<<n.bit_length()
        self.data = [0] * (self.n + 1)
        self.num = 0
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s
    def add(self, i, x):#iをx個追加
        while i <= self.n:
            self.data[i] += x
            i += i & -i
        self.num+=1
    def search(self, k):#k番目の数を取得
        if k<1:
            return 0
        if k >self.num:
            return self.N+1
        x,sx = 0,0
        step = self.n
        while step:
            y = x+step
            sy = sx+self.data[y]
            if sy < k:
                x,sx = y,sy
            step >>= 1
        return x+1
    def show(self):#debug用
        res = []
        for i in range(1, self.N+1):
            c = self.sum(i)-self.sum(i-1)
            for _ in range(c):
                res.append(i)
        return res
class BIT:
    def __init__(self, n):
        self.N = n
        self.n = 1<<n.bit_length()
        self.data = [0] * (self.n + 1)
        self.S = 0
        
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        self.S+=x
        while i <= self.n:
            self.data[i] += x
            i += i & -i
import bisect
Q = int(input())
compress_list = []
query = []
for _ in range(Q):
    c, *ab = map(int, input().split())
    if c == 1:
        a, b = ab
        compress_list.append(a)
        query.append((c, a, b))
    else:
        query.append([c])
compress_list.sort()
M = MultiSet(len(compress_list))
B = BIT(len(compress_list))
S = 0
for q in query:
    if q[0] == 1:
        a, b = q[1], q[2]
        S+=b
        i = bisect.bisect_right(compress_list, a)
        M.add(i, 1)
        B.add(i, a)
    else:
        if M.num%2 == 0:
            k = M.num//2
            i = M.search(k)
            n1 = M.sum(i-1)
            x = compress_list[i-1]
            s = B.S
            s1 = B.sum(i-1)+(k-n1)*x
            s2 = s-s1
            f = s2-s1+S
        else:
            k = M.num//2+1
            i = M.search(k)
            n1 = M.sum(i-1)
            x = compress_list[i-1]
            s = B.S
            s1 = B.sum(i-1)+(k-n1)*x
            s2 = s-s1
            f = s2-s1+x+S
        print(x, f)

Submission status: Accepted

Solution code:
import heapq


class DynamicMedian():
    """値をO(logN)で追加し、中央値をO(1)で求める
    add(val): valを追加する
    median_low(): 小さい方の中央値(low median)を求める
    median_high(): 大きい方の中央値(high median)を求める
    """
    def __init__(self):
        self.l_q = []  # 中央値以下の値を降順で格納する
        self.r_q = []  # 中央値以上の値を昇順で格納する
        self.l_sum = 0
        self.r_sum = 0
        
    def add(self, val):
        """valを追加する"""
        if len(self.l_q) == len(self.r_q):
            self.l_sum += val
            val = -heapq.heappushpop(self.l_q, -val)
            self.l_sum -= val
            heapq.heappush(self.r_q, val)
            self.r_sum += val
        else:
            self.r_sum += val
            val = heapq.heappushpop(self.r_q, val)
            self.r_sum -= val
            heapq.heappush(self.l_q, -val)
            self.l_sum += val
            
    def median_low(self):
        """小さい方の中央値を求める"""
        if len(self.l_q) + 1 == len(self.r_q):
            return self.r_q[0]
        else:
            return -self.l_q[0]

    def median_high(self):
        """大きい方の中央値を求める"""
        return self.r_q[0]

    def minimum_query(self):
        """キューに追加されている値 a1,...,aN に対して、|x-a1| + |x-a2| + ⋯ + |x-aN|の最小値を求める
        x = (a1,...,aN の中央値) となる"""
        res1 = (len(self.l_q) * self.median_high() - self.l_sum)
        res2 = (self.r_sum - len(self.r_q) * self.median_high())
        return res1 + res2


q = int(input())
info = [list(map(int, input().split())) for i in range(q)]
dm = DynamicMedian()

b = 0
for i in range(q):
    if info[i][0] == 1:
        _, tmp_a, tmp_b = info[i]
        dm.add(tmp_a)
        b += tmp_b
    else:
        print(dm.median_low(), dm.minimum_query() + b)

Submission status: Accepted

Solution code:
import heapq


class DynamicMedian():
    """値をO(logN)で追加し、中央値をO(1)で求める
    add(val): valを追加する
    median_low(): 小さい方の中央値(low median)を求める
    median_high(): 大きい方の中央値(high median)を求める
    """
    def __init__(self):
        self.l_q = []  # 中央値以下の値を降順で格納する
        self.r_q = []  # 中央値以上の値を昇順で格納する
        self.l_sum = 0
        self.r_sum = 0
        
    def add(self, val):
        """valを追加する"""
        if len(self.l_q) == len(self.r_q):
            self.l_sum += val
            val = -heapq.heappushpop(self.l_q, -val)
            self.l_sum -= val
            heapq.heappush(self.r_q, val)
            self.r_sum += val
        else:
            self.r_sum += val
            val = heapq.heappushpop(self.r_q, val)
            self.r_sum -= val
            heapq.heappush(self.l_q, -val)
            self.l_sum += val
            
    def median_low(self):
        """小さい方の中央値を求める"""
        if len(self.l_q) + 1 == len(self.r_q):
            return self.r_q[0]
        else:
            return -self.l_q[0]

    def median_high(self):
        """大きい方の中央値を求める"""
        return self.r_q[0]

    def minimum_query(self):
        """キューに追加されている値 a1,...,aN に対して、|x-a1| + |x-a2| + ⋯ + |x-aN|の最小値を求める
        x = (a1,...,aN の中央値) となる"""
        res1 = (len(self.l_q) * self.median_high() - self.l_sum)
        res2 = (self.r_sum - len(self.r_q) * self.median_high())
        return res1 + res2


q = int(input())
info = [list(map(int, input().split())) for i in range(q)]
dm = DynamicMedian()

b = 0
for i in range(q):
    if info[i][0] == 1:
        _, tmp_a, tmp_b = info[i]
        dm.add(tmp_a)
        b += tmp_b
    else:
        print(dm.median_low(), dm.minimum_query() + b)