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: Accepted)

using System;

class Program
{

	static readonly int COUNTER_CLOCKWISE = 1;
	static readonly int CLOCKWISE = -1;
	static readonly int ONLINE_BACK = 2;
	static readonly int ONLINE_FRONT = -2;
	static readonly int ON_SEGMENT = 0;

	public class Vector
	{
		public double x, y;

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

		public static Vector operator- (Vector v1, Vector v2)
		{
			return new Vector (v1.x - v2.x, v1.y - v2.y);
		}
	}

	public class Segment
	{
		public Vector p1, p2;

		public Segment (Vector v1, Vector v2)
		{
			p1 = v1;
			p2 = v2;
		}
	}

	public static double dot (Vector v1, Vector v2)
	{
		return v1.x * v2.x + v1.y * v2.y;
	}

	public static double cross (Vector v1, Vector v2)
	{
		return v1.x * v2.y - v1.y * v2.x;
	}

	static double norm (Vector v)
	{
		return v.x * v.x + v.y * v.y;
	}

	static bool intersect (Vector p1, Vector p2, Vector p3, Vector p4)
	{
		return (ccw (p1, p2, p3) * ccw (p1, p2, p4) <= 0 &&
		ccw (p3, p4, p1) * ccw (p3, p4, p2) <= 0);
	}

	static bool intersect (Segment s1, Segment s2)
	{
		return intersect (s1.p1, s1.p2, s2.p1, s2.p2);
	}

	static int ccw (Vector p1, Vector p2, Vector p3)
	{
		Vector a = p2 - p1;
		Vector b = p3 - p1;
		if (cross (a, b) > Double.Epsilon) {
			return COUNTER_CLOCKWISE;
		}
		if (cross (a, b) < -Double.Epsilon) {
			return CLOCKWISE;
		}
		if (dot (a, b) < -Double.Epsilon) {
			return ONLINE_BACK;
		}
		if (norm (a) < norm (b)) {
			return ONLINE_FRONT;
		}
		return ON_SEGMENT;
	}

	public static void Main ()
	{
		int n = int.Parse (Console.ReadLine ());
		for (int i = 0; i < n; i++) {
			string[] s = Console.ReadLine ().Split (' ');
			Segment seg1, seg2;
			Vector p1, p2, p3, p4;
			p1 = new Vector (int.Parse (s [0]), int.Parse (s [1]));
			p2 = new Vector (int.Parse (s [2]), int.Parse (s [3]));
			p3 = new Vector (int.Parse (s [4]), int.Parse (s [5]));
			p4 = new Vector (int.Parse (s [6]), int.Parse (s [7]));

			seg1 = new Segment (p1, p2);
			seg2 = new Segment (p3, p4);

			if(intersect(seg1, seg2))
			  {
			Console.WriteLine ("1");
			  } else {
			Console.WriteLine ("0");
			  }


		}
	}
}