module mod_fenwick_tree
  implicit none
  type t_fenwick_tree
    private
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_bit
    procedure :: release => release_bit
    procedure :: clear => clear
    procedure :: add => get_addition
    procedure :: sum => get_summation
    procedure :: range => get_partial_summation
    procedure :: val => get_value
    procedure :: lower_bound => get_lower_bound
    procedure :: update => get_updated
    procedure :: max => get_maximum
  end type t_fenwick_tree
contains
  subroutine init_bit(this,n)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    this%n = n
    allocate(this%arr(n))
    this%arr = 0
    do while (lshift(p,1) <= n)
      p = lshift(p,1)
    end do
    this%p = p
  end
  subroutine release_bit(this)
    class(t_fenwick_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine clear(this)
    class(t_fenwick_tree), intent(inout) :: this
    this%arr = 0
  end
  subroutine get_addition(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      this%arr(x) = this%arr(x)+v
      x = x+and(x,-x)
    end do
  end
  function get_summation(this,i) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: s
    s = -1
    if (i > this%n) return
    s = 0
    x = i
    do while (x > 0)
      s = s+this%arr(x)
      x = x-and(x,-x)
    end do
  end
  function get_partial_summation(this,l,r) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: l, r
    integer :: i, j
    integer :: s
    s = -1
    if (l > r) return
    s = 0
    i = l-1
    j = r
    do while (j > i)
      s = s+this%arr(j)
      j = j-and(j,-j)
    end do
    do while (i > j)
      s = s-this%arr(i)
      i = i-and(i,-i)
    end do
  end
  function get_value(this,i) result(v)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: v
    v = get_partial_summation(this,i,i)
  end
  function get_lower_bound(this,v) result(x)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: v
    integer :: x, k
    integer :: w
    x = 0
    if (v <= 0) return
    k = this%p
    w = v
    do while (k > 0)
      if (x+k <= this%n .and. this%arr(x+k) < w) then
        w = w-this%arr(x+k)
        x = x+k
      end if
      k = rshift(k,1)
    end do
    x = x+1
  end
  subroutine get_updated(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      if (this%arr(x) < v) this%arr(x) = v
      x = x+and(x,-x)
    end do
  end
  function get_maximum(this,i) result(m)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x, m
    x = i
    m = 0
    do while (x > 0)
      m = max(m,this%arr(x))
      x = x-and(x,-x)
    end do
  end
  function len_of_lis(a) result(m)
    integer, intent(in) :: a(:)
    type(t_fenwick_tree) :: bit
    integer :: m, n, i, t
    n = size(a)
    call init_bit(bit,n)
    m = 0
    do i = 1, n
      t = get_maximum(bit,a(i)-1)+1
      m = max(m,t)
      call get_updated(bit,a(i),t)
    end do
  end
  function inv_num(a) result(m)
    integer, intent(in) :: a(:)
    type(t_fenwick_tree) :: bit
    integer :: m, n, i, t
    n = size(a)
    call init_bit(bit,n)
    m = 0
    do i = 1, n
      call get_addition(bit,a(i),1)
      m = m+i-get_summation(bit,a(i))
    end do
  end
end module mod_fenwick_tree
program cartesian_coodinate
  use mod_fenwick_tree
  implicit none
  real(8), parameter :: tol = 1.e-11, inf = 5.e8
  integer :: n, i
  real(8) :: a(40000) = 0.d0, b(40000) = 0.d0, c(40000) = 0.d0
  character(64) :: x, y
  read(*,*) n
  do i = 1, n
    read(*,*) a(i), b(i), c(i)
  end do
  write(x,'(f32.16)') solve(a(1:n),b(1:n),c(1:n))
  write(y,'(f32.16)') solve(b(1:n),a(1:n),c(1:n))
  write(*,'(a,x,a)') trim(adjustl(x)), trim(adjustl(y))
contains
  function solve(a,b,c) result(l)
    type(t_fenwick_tree) :: bit
    real(8), intent(in) :: a(:), b(:), c(:)
    real(8) :: l, r, m, p(size(a),3)
    integer :: n, i, h(size(a)), inv
    l = -inf
    r = inf
    p = 0.d0
    n = size(a)
    call bit%init(n)
    do while (r-l > tol*max(1.d0,abs(l)))
      m = 0.5d0*(l+r)
      p(:,1) = (c-a*m)/b
      p(:,2) = -a/b
      call merge_sort(p(:,1:2))
      do i = 1, n
        p(i,3) = real(i,8)
      end do
      call merge_sort(p(:,2:3))
      h = nint(p(:,3))
      inv = 0
      call bit%clear()
      do i = 1, n
        inv = inv+bit%sum(h(i))
        call bit%add(h(i),1)
      end do
      if (n*(n-1)/2 <= 2*inv) then
        r = m
      else
        l = m
      end if
    end do
  end
  subroutine merge_sort(c)
    real(8), intent(inout) :: c(:,:)
    integer :: n, m, l, u, i
    n = size(c(:,1))
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(c(2*(i-1)*l+1:(2*i-1)*l,:),c((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
  end
  subroutine merger(c1,c2)
    real(8), intent(inout) :: c1(:,:), c2(:,:)
    real(8) :: c(size(c1(:,1))+size(c2(:,1)),size(c1(1,:)))
    integer :: i1, i2, n1, n2, i, n
    logical :: ok
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    n = size(c1(1,:))
    do while (i1 <= n1 .and. i2 <= n2)
      ok = .false.
      do i = 1, n-1
        if (c1(i1,i) < c2(i2,i)) then
          ok = .true.
          exit
        end if
      end do
      if (.not.ok .and. c1(i1,n) == c2(i2,n)) ok = .true.
      if (ok) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1 <= n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2 <= n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
  end
end program cartesian_coodinate

Measured memory_bytes: 3256