Problem:


Score : 800 points

Problem Statement

There are N lines in the xy-plane. The i-th line is represented by A_ix+B_iy=C_i.
Any two lines among the N+2 lines, the above N lines plus the x-axis and y-axis, cross each other at exactly one point.

For each pair 1 \leq i < j \leq N, there is a car at the cross point of the i-th and j-th lines.
Even where three or more lines intersect at a point, a car is individually placed for each pair of lines.
That is, there will be k(k-1)/2 cars placed at the intersection of k lines.

Those cars are already very old, and can only be moved parallel to the x-axis or y-axis.

Takahashi will hold an exhibition of antique cars at a place on the xy-plane.
In order to avoid damaging the half-broken cars too much, he will select the place of the exhibition so that the total distance covered will be minimized when all the cars are moved to the place.
If such a place is not uniquely determined, among the places that satisfy the condition above, the place with the minimum x-coordinate will be selected.
If the place is still not uniquely determined, among the places that satisfy the two conditions above, the place with the minimum y-coordinate will be selected.

Find the place of the exhibition that will be selected.

Constraints

2 \leq N \leq 4 × 10^4

1 \leq |A_i|,|B_i| \leq 10^4(1 \leq i \leq N)

0 \leq |C_i| \leq 10^4(1 \leq i \leq N)

No two given lines are parallel.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1
:
A_N B_N C_N

Outputs

Print the x-coordinate and y-coordinate of the place of the exhibition that will be selected, in this order, with a space in between. The output will be judged as correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3
1 1 1
2 -1 2
-1 2 2

Sample Output 1

1.000000000000000 1.000000000000000

There is a car at each place shown by a blue circle in the figure. The place to be selected is shown by a purple circle.

Sample Input 2

4
1 1 2
1 -1 0
3 -1 -2
1 -3 4

Sample Output 2

-1.000000000000000 -1.000000000000000

Sample Input 3

7
1 7 8
-2 4 9
3 -8 -5
9 2 -14
6 7 5
-8 -9 3
3 8 10

Sample Output 3

-1.722222222222222 1.325000000000000



Sample input:
3
1 1 1
2 -1 2
-1 2 2


Sample output:
1.000000000000000 1.000000000000000

Submission status: Time Limit Exceeded

Solution code:
import os
import sys

import numpy as np


def solve(inp):
    def bit_add(arr, n, i, x):
        while i <= n:
            arr[i] += x
            i += i & -i

    def bit_sum(arr, i):
        result = 0
        while i > 0:
            result += arr[i]
            i ^= i & -i
        return result

    n = int(inp[0])
    lim = (n * (n - 1) // 2 + 1) // 2
    aaa = inp[1::3]
    bbb = inp[2::3]
    ccc = inp[3::3]

    inclines_x = []
    inclines_y = []
    for i in range(n):
        inclines_x.append((aaa[i] / bbb[i], i))
        inclines_y.append((bbb[i] / aaa[i], i))
    inclines_x.sort()
    inclines_y.sort()
    incline_rank_x = np.zeros(n, dtype=np.int64)
    incline_rank_y = np.zeros(n, dtype=np.int64)
    for k in range(n):
        incline_rank_x[inclines_x[k][1]] = n - k
        incline_rank_y[inclines_y[k][1]] = n - k

    def check_x(m):
        yyy = []
        for i in range(n):
            yyy.append(((ccc[i] - aaa[i] * m) / bbb[i], i))
        yyy.sort()

        bit = np.zeros(n + 1, dtype=np.int64)
        intersection_left = 0
        for j in range(n):
            i = yyy[j][1]
            k = incline_rank_x[i]
            intersection_left += bit_sum(bit, k)
            bit_add(bit, n + 1, k, 1)

        return intersection_left >= lim

    def check_y(m):
        xxx = []
        for i in range(n):
            xxx.append(((ccc[i] - bbb[i] * m) / aaa[i], i))
        xxx.sort()

        bit = np.zeros(n + 1, dtype=np.int64)
        intersection_left = 0
        for j in range(n):
            i = xxx[j][1]
            k = incline_rank_y[i]
            intersection_left += bit_sum(bit, k)
            bit_add(bit, n + 1, k, 1)

        return intersection_left >= lim

    l = -2.0 * 10 ** 8
    r = 2.0 * 10 ** 8
    while l + 1e-9 < r:
        m = (l + r) / 2
        if check_x(m):
            r = m
        else:
            l = m
    x = l

    l = -2.0 * 10 ** 8
    r = 2.0 * 10 ** 8
    while l + 1e-9 < r:
        m = (l + r) / 2
        if check_y(m):
            r = m
        else:
            l = m
    y = l

    return x, y


if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', '(f8[:],)')(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit('(f8[:],)', cache=True)(solve)

inp = np.fromstring(sys.stdin.buffer.read(), dtype=np.float64, sep=' ')
try:
    x, y = solve(inp)
    print(x, y)
except ZeroDivisionError:
    print('WA')
    exit()

Submission status: Time Limit Exceeded

Solution code:
class BIT():
    def __init__(self,n):
        self.BIT=[0]*(n+1)
        self.num=n

    def query(self,idx):
        res_sum = 0
        while idx > 0:
            res_sum += self.BIT[idx]
            idx -= idx&(-idx)
        return res_sum

    #Ai += x O(logN)
    def update(self,idx,x):
        while idx <= self.num:
            self.BIT[idx] += x
            idx += idx&(-idx)
        return

def comp(L):
    S=list(set(L))
    S.sort()
    return {i:e for e,i in enumerate(S)}

import sys,random

input=sys.stdin.readline

N=int(input())
A=[0]*N;B=[0]*N;C=[0]*N
for i in range(N):
    A[i],B[i],C[i]=map(int,input().split())

line=[(A[i]/B[i],i) for i in range(N)]
line.sort()
line=[line[i][1] for i in range(N)]

line2=[(B[i]/A[i],i) for i in range(N)]
line2.sort()
line2=[line2[i][1] for i in range(N)]

def cond(n):
    tempC=[C[i]-n*A[i] for i in range(N)]
    data=[tempC[i]/B[i] for i in range(N)]
    data=comp(data)
    bit=BIT(len(data))
    res=0
    for i in range(N):
        id2=data[tempC[line[i]]/B[line[i]]]
        res+=i-bit.query(id2)
        bit.update(id2+1,1)
    num=N*(N-1)//2
    return res>=(num+1)//2

def cond2(n):
    tempC=[C[i]-n*B[i] for i in range(N)]
    data=[tempC[i]/A[i] for i in range(N)]
    data=comp(data)
    bit=BIT(len(data))
    res=0
    for i in range(N):
        id2=data[tempC[line2[i]]/A[line2[i]]]
        res+=i-bit.query(id2)
        bit.update(id2+1,1)
    num=N*(N-1)//2
    return res>=(num+1)//2

start=-2*10**8
end=2*10**8
count=0
while end-start>10**-9:
    count+=1
    test=(end+start)/2
    if cond(test):
        end=test
    else:
        start=test

start2=-2*10**8
end2=2*10**8
while end2-start2>10**-9:
    test=(end2+start2)/2
    count+=1
    if cond2(test):
        end2=test
    else:
        start2=test

print(end,end2)
#print(count)

Submission status: Time Limit Exceeded

Solution code:
from functools import cmp_to_key

class BIT():
    def __init__(self,n):
        self.BIT=[0]*(n+1)
        self.num=n

    def query(self,idx):
        res_sum = 0
        while idx > 0:
            res_sum += self.BIT[idx]
            idx -= idx&(-idx)
        return res_sum

    #Ai += x O(logN)
    def update(self,idx,x):
        while idx <= self.num:
            self.BIT[idx] += x
            idx += idx&(-idx)
        return

def sort_func(ab0,ab1):
    a0, b0= ab0[0],ab0[1]
    a1, b1= ab1[0],ab1[1]
    if b0*b1>0:
        return -a1*b0 + a0*b1
    else:
        return a1*b0 - a0*b1

def comp(L):
    S=list(set(L))
    S.sort(key=cmp_to_key(sort_func))
    return {i:e for e,i in enumerate(S)}

import sys,random

input=sys.stdin.readline

N=int(input())
A=[0]*N;B=[0]*N;C=[0]*N
for i in range(N):
    A[i],B[i],C[i]=map(int,input().split())

line=[(A[i]/B[i],i) for i in range(N)]
line.sort()
line=[line[i][1] for i in range(N)]

line2=[(B[i]/A[i],i) for i in range(N)]
line2.sort()
line2=[line2[i][1] for i in range(N)]

def cond(n):
    tempC=[C[i]-n*A[i] for i in range(N)]
    data=[(tempC[i],B[i]) for i in range(N)]
    data=comp(data)
    bit=BIT(len(data))
    res=0
    for i in range(N):
        id2=data[(tempC[line[i]],B[line[i]])]
        res+=i-bit.query(id2)
        bit.update(id2+1,1)
    num=N*(N-1)//2
    return res>=(num+1)//2

def cond2(n):
    tempC=[C[i]-n*B[i] for i in range(N)]
    data=[(tempC[i],A[i]) for i in range(N)]
    data=comp(data)
    bit=BIT(len(data))
    res=0
    for i in range(N):
        id2=data[(tempC[line2[i]],A[line2[i]])]
        res+=i-bit.query(id2)
        bit.update(id2+1,1)
    num=N*(N-1)//2
    return res>=(num+1)//2

start=-2*10**8
end=2*10**8
count=0
while end-start>10**-9:
    count+=1
    test=(end+start)/2
    if cond(test):
        end=test
    else:
        start=test

start2=-2*10**8
end2=2*10**8
while end2-start2>10**-9:
    test=(end2+start2)/2
    count+=1
    if cond2(test):
        end2=test
    else:
        start2=test

print(end,end2)
#print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  # @lru_cache(None)
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return (int(x) for x in input().split())
def LI(): return list(Is())
def TI(): return tuple(Is())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(S())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
EPS = 1e-9
# ----------------------------------------------------------- #

class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):  # 1-index
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):  # 1-index
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    """
    # 使用例
    bit = BinaryIndexedTree(10)     # 要素数を与えてインスタンス化
    bit.add(2, 10)    # a2に10を加える
    bit.add(3, -6)    # a3に-6を加える
    print(bit.sum(6)) # a1～a6の合計を返す
    print(bit.sum(6) - bit.sum(3))  # a4～a6の合計を返す
    """

def compress(A):
    dic = {element: i for i, element in enumerate(sorted(set(A)), start=1)}
    return [dic[a] for a in A]

def meguru_bisect(ng, ok, check):
    """
    初期値のng,okを受け取り, check=>Trueを満たす最大値(最小値)を返す
    条件を満たす最大値を求める場合、初期値は(ng:上限の外, ok:下限の外)   例) 10**9+1, -1
    条件を満たす最小値を求める場合、初期値は(ng:下限の外, ok:上限の外)   例) -1, 10**9+1
    上限の外や下限の外が返ってくる場合は条件を満たす値が範囲内になかったことを示すので、例外処理が必要
    """
    # while abs(ok - ng) > 1:
    count = 0
    while abs(ok - ng) > EPS or count > 70:
        count += 1
        # mid = (ok + ng) // 2
        mid = (ok + ng) / 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok

def check_x(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    X = compress([(c-b*num)/a for a, b, c in X_ABC])
    for i, x in enumerate(X, start=1):
        bit.add(x, 1)
        inversion_num += i - bit.sum(x)
    return inversion_num >= m

def check_y(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    Y = compress([(c-a*num)/b for a, b, c in Y_ABC])
    for i, y in enumerate(Y, start=1):
        bit.add(y, 1)
        inversion_num += i - bit.sum(y)
    return inversion_num >= m

n = I()
ABC = TIR(n)
m = (n*(n-1)//2+1)//2
X_ABC = sorted(ABC, key=lambda abc: abc[1]/abc[0])  # 傾きb/aの昇順
Y_ABC = sorted(ABC, key=lambda abc: abc[0]/abc[1])  # 傾きa/bの昇順
mid_x = meguru_bisect(-10**9, 10**9, check_x)
mid_y = meguru_bisect(-10**9, 10**9, check_y)

print(mid_y, mid_x)  # ????

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  # @lru_cache(None)
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return (int(x) for x in input().split())
def LI(): return list(Is())
def TI(): return tuple(Is())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(S())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
EPS = 1e-9
# ----------------------------------------------------------- #

class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):  # 1-index
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):  # 1-index
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    """
    # 使用例
    bit = BinaryIndexedTree(10)     # 要素数を与えてインスタンス化
    bit.add(2, 10)    # a2に10を加える
    bit.add(3, -6)    # a3に-6を加える
    print(bit.sum(6)) # a1～a6の合計を返す
    print(bit.sum(6) - bit.sum(3))  # a4～a6の合計を返す
    """

def compress(A):
    dic = {element: i for i, element in enumerate(sorted(set(A)), start=1)}
    return [dic[a] for a in A]

def meguru_bisect(ng, ok, check):
    """
    初期値のng,okを受け取り, check=>Trueを満たす最大値(最小値)を返す
    条件を満たす最大値を求める場合、初期値は(ng:上限の外, ok:下限の外)   例) 10**9+1, -1
    条件を満たす最小値を求める場合、初期値は(ng:下限の外, ok:上限の外)   例) -1, 10**9+1
    上限の外や下限の外が返ってくる場合は条件を満たす値が範囲内になかったことを示すので、例外処理が必要
    """
    # while abs(ok - ng) > 1:
    count = 0
    while abs(ok - ng) > EPS or count > 70:
        count += 1
        # mid = (ok + ng) // 2
        mid = (ok + ng) / 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok

def check_x(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    X = compress([(c-b*num)/a for a, b, c in X_ABC])
    for i, x in enumerate(X, start=1):
        bit.add(x, 1)
        inversion_num += i - bit.sum(x)
    return inversion_num >= m

def check_y(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    Y = compress([(c-a*num)/b for a, b, c in Y_ABC])
    for i, y in enumerate(Y, start=1):
        bit.add(y, 1)
        inversion_num += i - bit.sum(y)
    return inversion_num >= m

n = I()
ABC = TIR(n)
m = (n*(n-1)//2+1)//2
X_ABC = sorted(ABC, key=lambda abc: abc[1]/abc[0])  # 傾きb/aの昇順
Y_ABC = sorted(ABC, key=lambda abc: abc[0]/abc[1])  # 傾きa/bの昇順
mid_x = meguru_bisect(-10**8-1, 10**8+1, check_x)
mid_y = meguru_bisect(-10**8-1, 10**8+1, check_y)

print(mid_y, mid_x)  # ????

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  # @lru_cache(None)
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return (int(x) for x in input().split())
def LI(): return list(Is())
def TI(): return tuple(Is())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(S())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
EPS = 9*1e-10
# ----------------------------------------------------------- #

class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):  # 1-index
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):  # 1-index
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    """
    # 使用例
    bit = BinaryIndexedTree(10)     # 要素数を与えてインスタンス化
    bit.add(2, 10)    # a2に10を加える
    bit.add(3, -6)    # a3に-6を加える
    print(bit.sum(6)) # a1～a6の合計を返す
    print(bit.sum(6) - bit.sum(3))  # a4～a6の合計を返す
    """

def compress(A):
    dic = {element: i for i, element in enumerate(sorted(set(A)), start=1)}
    return [dic[a] for a in A]

def meguru_bisect(ng, ok, check):
    """
    初期値のng,okを受け取り, check=>Trueを満たす最大値(最小値)を返す
    条件を満たす最大値を求める場合、初期値は(ng:上限の外, ok:下限の外)   例) 10**9+1, -1
    条件を満たす最小値を求める場合、初期値は(ng:下限の外, ok:上限の外)   例) -1, 10**9+1
    上限の外や下限の外が返ってくる場合は条件を満たす値が範囲内になかったことを示すので、例外処理が必要
    """
    # while abs(ok - ng) > 1:
    while abs(ok - ng) > EPS:
        # mid = (ok + ng) // 2
        mid = (ok + ng) / 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok

def check_x(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    X = compress([(c-b*num)/a for a, b, c in X_ABC])
    for i, x in enumerate(X, start=1):
        bit.add(x, 1)
        inversion_num += i - bit.sum(x)
    return inversion_num >= m

def check_y(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    Y = compress([(c-a*num)/b for a, b, c in Y_ABC])
    for i, y in enumerate(Y, start=1):
        bit.add(y, 1)
        inversion_num += i - bit.sum(y)
    return inversion_num >= m

n = I()
ABC = TIR(n)
m = (n*(n-1)//2+1)//2
X_ABC = sorted(ABC, key=lambda abc: abc[1]/abc[0])  # 傾きb/aの昇順
Y_ABC = sorted(ABC, key=lambda abc: abc[0]/abc[1])  # 傾きa/bの昇順
mid_x = meguru_bisect(-10**8-1, 10**8+1, check_x)
mid_y = meguru_bisect(-10**8-1, 10**8+1, check_y)

print(mid_y, mid_x)  # ????

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  # @lru_cache(None)
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return (int(x) for x in input().split())
def LI(): return list(Is())
def TI(): return tuple(Is())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(S())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
EPS = 1e-10
# ----------------------------------------------------------- #

class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):  # 1-index
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):  # 1-index
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    """
    # 使用例
    bit = BinaryIndexedTree(10)     # 要素数を与えてインスタンス化
    bit.add(2, 10)    # a2に10を加える
    bit.add(3, -6)    # a3に-6を加える
    print(bit.sum(6)) # a1～a6の合計を返す
    print(bit.sum(6) - bit.sum(3))  # a4～a6の合計を返す
    """

def compress(A):
    dic = {element: i for i, element in enumerate(sorted(set(A)), start=1)}
    return [dic[a] for a in A]

def meguru_bisect(ng, ok, check):
    """
    初期値のng,okを受け取り, check=>Trueを満たす最大値(最小値)を返す
    条件を満たす最大値を求める場合、初期値は(ng:上限の外, ok:下限の外)   例) 10**9+1, -1
    条件を満たす最小値を求める場合、初期値は(ng:下限の外, ok:上限の外)   例) -1, 10**9+1
    上限の外や下限の外が返ってくる場合は条件を満たす値が範囲内になかったことを示すので、例外処理が必要
    """
    # while abs(ok - ng) > 1:
    while abs(ok - ng) > EPS:
        # mid = (ok + ng) // 2
        mid = (ok + ng) / 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok

def check_x(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    X = compress([(c-b*num)/a for a, b, c in X_ABC])
    for i, x in enumerate(X, start=1):
        bit.add(x, 1)
        inversion_num += i - bit.sum(x)
    return inversion_num >= m

def check_y(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    Y = compress([(c-a*num)/b for a, b, c in Y_ABC])
    for i, y in enumerate(Y, start=1):
        bit.add(y, 1)
        inversion_num += i - bit.sum(y)
    return inversion_num >= m

n = I()
ABC = TIR(n)
m = (n*(n-1)//2+1)//2
X_ABC = sorted(ABC, key=lambda abc: abc[1]/abc[0])  # 傾きb/aの昇順
Y_ABC = sorted(ABC, key=lambda abc: abc[0]/abc[1])  # 傾きa/bの昇順
mid_x = meguru_bisect(-10**9, 10**9, check_x)
mid_y = meguru_bisect(-10**9, 10**9, check_y)

print(mid_y, mid_x)  # ????

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  # @lru_cache(None)
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return (int(x) for x in input().split())
def LI(): return list(Is())
def TI(): return tuple(Is())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(S())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
EPS = 1e-10
# ----------------------------------------------------------- #

class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):  # 1-index
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):  # 1-index
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    """
    # 使用例
    bit = BinaryIndexedTree(10)     # 要素数を与えてインスタンス化
    bit.add(2, 10)    # a2に10を加える
    bit.add(3, -6)    # a3に-6を加える
    print(bit.sum(6)) # a1～a6の合計を返す
    print(bit.sum(6) - bit.sum(3))  # a4～a6の合計を返す
    """

def compress(A):
    dic = {element: i for i, element in enumerate(sorted(set(A)), start=1)}
    return [dic[a] for a in A]

def meguru_bisect(ng, ok, check):
    """
    初期値のng,okを受け取り, check=>Trueを満たす最大値(最小値)を返す
    条件を満たす最大値を求める場合、初期値は(ng:上限の外, ok:下限の外)   例) 10**9+1, -1
    条件を満たす最小値を求める場合、初期値は(ng:下限の外, ok:上限の外)   例) -1, 10**9+1
    上限の外や下限の外が返ってくる場合は条件を満たす値が範囲内になかったことを示すので、例外処理が必要
    """
    # while abs(ok - ng) > 1:
    for _ in range(70):
        # mid = (ok + ng) // 2
        mid = (ok + ng) / 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok

def check_x(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    X = compress([(c-b*num)/a for a, b, c in X_ABC])
    for i, x in enumerate(X, start=1):
        bit.add(x, 1)
        inversion_num += i - bit.sum(x)
    return inversion_num >= m

def check_y(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    Y = compress([(c-a*num)/b for a, b, c in Y_ABC])
    for i, y in enumerate(Y, start=1):
        bit.add(y, 1)
        inversion_num += i - bit.sum(y)
    return inversion_num >= m

n = I()
ABC = TIR(n)
m = (n*(n-1)//2+1)//2
X_ABC = sorted(ABC, key=lambda abc: abc[1]/abc[0])  # 傾きb/aの昇順
Y_ABC = sorted(ABC, key=lambda abc: abc[0]/abc[1])  # 傾きa/bの昇順
mid_x = meguru_bisect(-10**9, 10**9, check_x)
mid_y = meguru_bisect(-10**9, 10**9, check_y)

print(mid_y, mid_x)  # ????

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  # @lru_cache(None)
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return (int(x) for x in input().split())
def LI(): return list(Is())
def TI(): return tuple(Is())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(S())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
EPS = 1e-10
# ----------------------------------------------------------- #

class BinaryIndexedTree:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):  # 1-index
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):  # 1-index
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    """
    # 使用例
    bit = BinaryIndexedTree(10)     # 要素数を与えてインスタンス化
    bit.add(2, 10)    # a2に10を加える
    bit.add(3, -6)    # a3に-6を加える
    print(bit.sum(6)) # a1～a6の合計を返す
    print(bit.sum(6) - bit.sum(3))  # a4～a6の合計を返す
    """

def compress(A):
    dic = {element: i for i, element in enumerate(sorted(set(A)), start=1)}
    return [dic[a] for a in A]

def meguru_bisect(ng, ok, check):
    """
    初期値のng,okを受け取り, check=>Trueを満たす最大値(最小値)を返す
    条件を満たす最大値を求める場合、初期値は(ng:上限の外, ok:下限の外)   例) 10**9+1, -1
    条件を満たす最小値を求める場合、初期値は(ng:下限の外, ok:上限の外)   例) -1, 10**9+1
    上限の外や下限の外が返ってくる場合は条件を満たす値が範囲内になかったことを示すので、例外処理が必要
    """
    # while abs(ok - ng) > 1:
    for _ in range(80):
        # mid = (ok + ng) // 2
        mid = (ok + ng) / 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok

def check_x(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    X = compress([(c-b*num)/a for a, b, c in X_ABC])
    for i, x in enumerate(X, start=1):
        bit.add(x, 1)
        inversion_num += i - bit.sum(x)
    return inversion_num >= m

def check_y(num):
    bit = BinaryIndexedTree(n)
    inversion_num = 0
    Y = compress([(c-a*num)/b for a, b, c in Y_ABC])
    for i, y in enumerate(Y, start=1):
        bit.add(y, 1)
        inversion_num += i - bit.sum(y)
    return inversion_num >= m

n = I()
ABC = TIR(n)
m = (n*(n-1)//2+1)//2
X_ABC = sorted(ABC, key=lambda abc: abc[1]/abc[0])  # 傾きb/aの昇順
Y_ABC = sorted(ABC, key=lambda abc: abc[0]/abc[1])  # 傾きa/bの昇順
mid_x = meguru_bisect(-10**9, 10**9, check_x)
mid_y = meguru_bisect(-10**9, 10**9, check_y)

print(mid_y, mid_x)  # ????

Submission status: Time Limit Exceeded

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

class BIT:
    #1-indexed
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
        self.p = 2**(n.bit_length() - 1)
        self.dep = n.bit_length()
    def get(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
    
    def bl(self, v):
        if v <= 0:
            return -1
        s = 0
        k = self.p
        for _ in range(self.dep):
            if s + k <= self.size and self.tree[s+k] < v:
                s += k
                v -= self.tree[s+k]
            k //= 2
        return s + 1
    
    def reset(self):
        self.tree = [0]*(self.size+1)

N = int(readline())
INF = 2e8 + 10
EPS = 1e-9

Points = [tuple(map(int, readline().split())) for _ in range(N)]
Points.sort(key = lambda x: -x[0]/x[1])
XA = [-a/b for a, b, _ in Points]
XB = [c/b for _, b, c in Points]
Points.sort(key = lambda x: -x[1]/x[0])
YA = [-b/a for a, b, _ in Points]
YB = [c/a for a, _, c in Points]
con = -(-N*(N-1)//4)
T = BIT(N)

ng = -INF
ok = INF

for _ in range(60):
    med = (ok+ng)/2
    T.reset()
    res = 0
    for idx in sorted(range(N), key = lambda x: XA[x]*med + XB[x]):
        res += T.get(idx+1)
        T.add(idx+1, 1)
    if res >= con:
        ok = med
    else:
        ng = med

X = ok

ng = -INF
ok = INF

for _ in range(60):
    med = (ok+ng)/2
    T.reset()
    res = 0
    for idx in sorted(range(N), key = lambda x: YA[x]*med + YB[x]):
        res += T.get(idx+1)
        T.add(idx+1, 1)
    if res >= con:
        ok = med
    else:
        ng = med

Y = ok

print(X, Y)

Submission status: Time Limit Exceeded

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

class BIT:
    #1-indexed
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
        self.p = 2**(n.bit_length() - 1)
        self.dep = n.bit_length()
    def get(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
    
    def bl(self, v):
        if v <= 0:
            return -1
        s = 0
        k = self.p
        for _ in range(self.dep):
            if s + k <= self.size and self.tree[s+k] < v:
                s += k
                v -= self.tree[s+k]
            k //= 2
        return s + 1
    
    def reset(self):
        self.tree = [0]*(self.size+1)

N = int(readline())
INF = 2e8 + 10
EPS = 1e-9

Points = [tuple(map(int, readline().split())) for _ in range(N)]
XA = [-a/b for a, b, _ in Points]
XB = [c/b for _, b, c in Points]
YA = [-b/a for a, b, _ in Points]
YB = [c/a for a, _, c in Points]
con = -(-N*(N-1)//4)
T = BIT(N)

ds = sorted(range(N), key = lambda x: XA[x])
rd = [None]*N
for i in range(N):
    rd[ds[i]] = i

ng = -INF
ok = INF

for _ in range(60):
    med = (ok+ng)/2
    T.reset()
    res = 0
    for idx in sorted(range(N), key = lambda x: XA[x]*med + XB[x]):
        res += T.get(rd[idx]+1)
        T.add(rd[idx]+1, 1)
    if res >= con:
        ok = med
    else:
        ng = med

X = ok

ds = sorted(range(N), key = lambda x: YA[x])

rd = [None]*N
for i in range(N):
    rd[ds[i]] = i

ng = -INF
ok = INF

for _ in range(60):
    med = (ok+ng)/2
    T.reset()
    res = 0
    for idx in sorted(range(N), key = lambda x: YA[x]*med + YB[x]):
        res += T.get(rd[idx]+1)
        T.add(rd[idx]+1, 1)
    if res >= con:
        ok = med
    else:
        ng = med

Y = ok

print(X, Y)