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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush=
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var Q = cin.ReadInt;
			for(int j = 0; j<Q; j++) {
				var d = cin.ReadIntArray(8);
				var ps = new P2[4];
				for(int k = 0; k<4; k++) {
					ps[k]=new P2(d[2*k], d[2*k+1]);
				}
				Console.WriteLine(isIntersect(ps[0], ps[1], ps[2], ps[3]) ? 1 : 0);
			}
		}
		public struct P2 {
			public decimal x { get; set; }
			public decimal y { get; set; }
			public P2(decimal xx, decimal yy) { x=xx; y=yy; }
			public static P2 operator +(P2 a, V2 b) => new P2(a.x+b.x, a.y+b.y);
			public static V2 operator -(P2 a, P2 b) => new V2(a.x-b.x, a.y-b.y);
			public decimal Abs2() => x*x+y*y;
			public override string ToString() => $"({x}, {y})";
		}
		public struct V2 {
			public decimal x { get; set; }
			public decimal y { get; set; }
			public V2(decimal xx, decimal yy) { x=xx; y=yy; }
			public static V2 operator -(V2 a, V2 b) => new V2(a.x-b.x, a.y-b.y);
			public static V2 operator +(V2 a, V2 b) => new V2(a.x+b.x, a.y+b.y);
			public static V2 operator *(V2 a, decimal k) => new V2(a.x*k, a.y*k);
			public static decimal outer(V2 a, V2 b) => a.x*b.y-b.x*a.y;
			public static decimal inner(V2 a, V2 b) => a.x*b.x+a.y*b.y;
			public decimal Abs2() => x*x+y*y;
			public override string ToString() => $"({x}, {y})";
		}
		static public bool isIntersect(P2 a1, P2 a2, P2 b1, P2 b2) {
			var parallel = V2.outer(a2-a1, b2-b1)==0m;
			if(parallel) {
				var willIntersect = V2.outer(a2-a1, b1-a1)==0m;
				if(!willIntersect) return false;
				var k1 = V2.inner(b1-a1, b1-a2);
				var k2 = V2.inner(b2-a1, b2-a2);
				if(k1<=0||k2<=0) return true;
				var l1 = V2.inner(a1-b1, a1-b2);
				var l2 = V2.inner(a2-b1, a2-b2);
				return l1<=0||l2<=0;
			} else
				return V2.outer(b1-a1, a2-a1)*V2.outer(b2-a2, a1-a2)>=0
				&&V2.outer(a1-b1, b2-b1)*V2.outer(a2-b2, b1-b2)>=0;
		}
	}
	static class ExtendClass {
		public static void Swap<T>(this T[] A, int x, int y) {
			var tmp = A[x];
			A[x]=A[y];
			A[y]=tmp;
		}
		public static string Join<T>(this IEnumerable<T> X, string separator = " ") => string.Join(separator, X);
		public static IEnumerable<int> Indices<T>(this T[] X) => Enumerable.Range(0, X.Length);
		public static IEnumerable<int> Indices<T>(this T[,] X, int dim)
			=> Enumerable.Range(0, X.GetLength(dim));
		public static string Join<T>(this T[,] X, string separatorD2 = ",", string separatorD1 = "\n")
			=> X.Indices(0).Select(
				i => X.Indices(1).Select(j => X[i, j]).Join(separatorD2)
			).Join(separatorD1);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator=separator;
			this._stream=stream;
			inputStream=new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count!=0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i<tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i<N; ++i)
				ret[i]=Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek()==-1; }
		}
	}
}