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 C# (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static readonly double EPS = 1E-10;

        static void Main(string[] args)
        {
            int q = int.Parse(Console.ReadLine());

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < q; i++)
            {
                double[] input = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse);
                Pt a = new Pt(input[0], input[1]);
                Pt b = new Pt(input[2], input[3]);
                Pt c = new Pt(input[4], input[5]);
                Pt d = new Pt(input[6], input[7]);

                sb.AppendLine(IsIntersect(a, b, c, d) ? "1" : "0");
            }
            Console.Write(sb);
        }

        static bool IsIntersect(Pt p1,Pt p2,Pt p3,Pt p4)
        {
            return CCW(p1, p2, p3) * CCW(p1, p2, p4) <= 0 &&
                   CCW(p3, p4, p1) * CCW(p3, p4, p2) <= 0;
        }

        static int CCW(Pt p0, Pt p1, Pt p2)
        {
            Pt a = p1 - p0, b = p2 - p0;

            if (a.Cross(b) > EPS) return 1;
            if (a.Cross(b) < -EPS) return -1;
            return 0;
        }
    }

    class Pt : IComparable<Pt>
    {
        static readonly double EPS = 1E-10;

        internal double x, y;

        public Pt(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public double Abs { get { return Math.Sqrt(Norm); } }
        public double Norm { get { return x * x + y * y; } }

        public double Dot(Pt p) { return x * p.x + y * p.y; }
        public double Cross(Pt p) { return x * p.y - y * p.x; }

        public static Pt operator +(Pt a, Pt b) { return new Pt(a.x + b.x, a.y + b.y); }
        public static Pt operator -(Pt a, Pt b) { return new Pt(a.x - b.x, a.y - b.y); }
        public static Pt operator *(Pt p, double d) { return new Pt(p.x * d, p.y * d); }
        public static Pt operator /(Pt p, double d) { return new Pt(p.x / d, p.y / d); }
        public static bool operator <(Pt a, Pt b) { return Math.Abs(a.x - b.x) > EPS ? a.x < b.x : a.y < b.y; }
        public static bool operator >(Pt a, Pt b) { return Math.Abs(a.x - b.x) > EPS ? a.x > b.x : a.y > b.y; }
        public static bool operator ==(Pt a, Pt b) { return Math.Abs(a.x - b.x) < EPS && Math.Abs(a.y - b.y) < EPS; }
        public static bool operator !=(Pt a, Pt b) { return Math.Abs(a.x - b.x) >= EPS || Math.Abs(a.y - b.y) >= EPS; }

        public int CompareTo(Pt other)
        {
            return x != other.x ? (int)(x - other.x) : (int)(y - other.y);
        }

        public override bool Equals(object obj)
        {
            Pt other = (Pt)obj;
            return Math.Abs(x - other.x) < EPS && Math.Abs(y - other.y) < EPS;
        }

        public override int GetHashCode()
        {
            return x.GetHashCode() ^ y.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("{0:f10} {1:f10}", x, y);
        }
    }

    class Seg
    {
        internal Pt p1, p2;

        public Seg(Pt a, Pt b)
        {
            p1 = a;
            p2 = b;
        }
    }
}