Problem:
Intersection

For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.

s1 is formed by end points p0 and p1, and
s2 is formed by end points p2 and p3.

Input

The entire input looks like:

q (the number of queries)
1st query
2nd query
...
qth query

Each query consists of integer coordinates of end points of s1 and s2 in the following format:

xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3

Output

For each query, print "1" or "0".

Constraints

1 ≤ q ≤ 1000

-10000 ≤ xpi, ypi ≤ 10000

p0 ≠ p1 and
p2 ≠ p3.

Sample Input 1

3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0

Sample Output 1

1
1
0

Solution in Ruby (judge verdict: Wrong Answer)

#encoding : utf-8

require 'complex'

# Complex * Complex -> Double

def dot(c1, c2)
  (c1.conj * c2).real
end

# Complex * Complex -> Double
def cross(c1, c2)
  (c1.conj * c2).imag
end

# Complex * Complex * Complex -> Complex
# return the proj of vector p0p to vector p0p1
def proj(p0, p1, p)
  (p1 - p0) * dot(p1 - p0, p - p0) / ((p1 - p0).abs ** 2) + p0
end


def sign d
  eps = 1e-8
  d < -eps ? -1 : (d > eps ? 1 : 0)
end

# Complex * Complex * Complex -> Bool
# Return true if Point q is on segment p1p2
# otherwise return false
def on_seg(p1, p2, q)
  sign(cross(p1 - q, p2 - q)) == 0 &&
    sign(dot(p1 - q, p2 - q)) <= 0
end

def direct(p0, p1, p2)
  d = cross(p1 - p0, p2 - p0)
  if sign(d) == 1
    :COUNTER_CLOCKWISE
  elsif sign(d) == -1
    :CLOCKWISE
  else
    if on_seg(p2, p1, p0)
      :ONLINE_BACK
    elsif on_seg(p0, p2, p1)
      :ONLINE_FRONT
    else
      :ON_SEGMENT
    end
  end
end

# Complex * Complex * Complex * Complex -> :PARALL | :OVERLAP | Complex
# Compute the intersect point of two line p0p1 p2p3
# Return:
#       :PARALLEL if two line parallel
#       :OVERLAP if two line overlap
#       Point of the intersection
def intersect_line(p0, p1, p2, p3)
  v1 = Complex(p1 - p0)
  v2 = Complex(p3 - p2)
  if sign(cross(v1, v2)) == 0 and sign(cross(p2 - p0, p2 - p1)) == 0
    :OVERLAP
  elsif sign(cross(v1, v2)) == 0 and sign(cross(p2 - p0, p2 - p1)) != 0
    :PARALLEL
  else
    p2 + (p3 - p2) * ((cross(p1 - p0, p2 - p0)) /
      (cross(p3 - p0, p1 - p0) + cross(p1 - p0, p2 - p0)))
  end
end

def main
  t = gets.chomp.to_i
  (1..t).each do |ca|
    x1, y1, x2, y2, x3, y3, x4, y4 = gets.chomp.split(/\s/).collect {|x| x.to_f}
    p0, p1 = Complex(x1, y1), Complex(x2, y2)
    p2, p3 = Complex(x3, y3), Complex(x4, y4)
    int = intersect_line(p0, p1, p2, p3)
    if int == :OVERLAP or int == :PARALLEL
      puts "0"
    elsif on_seg(p0, p1, int) and on_seg(p2, p3, int)
      puts "1"
    else
      puts "0"
    end
  end
end

main