Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Segment Intersections: Manhattan Geometry

For given $n$ segments which are parallel to X-axis or Y-axis, find the number of intersections of them.

Input

In the first line, the number of segments $n$ is given. In the following $n$ lines, the $i$-th segment is given by coordinates of its end points in the following format:

$x_1 \; y_1 \; x_2 \; y_2$

The coordinates are given in integers.

Output

Print the number of intersections in a line.

Constraints

$1 \leq n \leq 100,000$

$ -1,000,000,000 \leq x_1, y_1, x_2, y_2 \leq 1,000,000,000$

Two parallel segments never overlap or touch.

The number of intersections $\leq 1,000,000$

Sample Input 1

6
2 2 2 5
1 3 5 3
4 1 4 4
5 2 7 2
6 1 6 3
6 5 6 7

Sample Output 1

3

Solution in C# (judge verdict: Accepted)

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

namespace ConsoleApplication1
{
    enum ST { Bottom, Left, Right, Top }

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

            Segment[] S = new Segment[n];
            EndPoint[] EP = new EndPoint[n * 2];

            for (int i = 0, k = 0; i < n; i++)
            {
                double[] input = Array.ConvertAll(Console.ReadLine().Split(' '), double.Parse);
                Point a = new Point(input[0], input[1]);
                Point b = new Point(input[2], input[3]);

                if (a.y == b.y)
                {
                    S[i] = a.x > b.x ? new Segment(b, a) : new Segment(a, b);

                    EP[k++] = new EndPoint(S[i].p1, i, ST.Left);
                    EP[k++] = new EndPoint(S[i].p2, i, ST.Right);
                }
                else
                {
                    S[i] = a.y > b.y ? new Segment(b, a) : new Segment(a, b);

                    EP[k++] = new EndPoint(S[i].p1, i, ST.Bottom);
                    EP[k++] = new EndPoint(S[i].p2, i, ST.Top);
                }
            }

            Array.Sort(EP);

            Console.WriteLine(ManhattanGeometry(S, EP));
        }

        static int ManhattanGeometry(Segment[] S, EndPoint[] EP)
        {
            var BT = new HashSet<double>();

            int cnt = 0, n = EP.Length;

            for (int i = 0; i < n; i++)
            {
                switch (EP[i].st)
                {
                    case ST.Top:
                        BT.Remove(EP[i].p.x);
                        break;
                    case ST.Bottom:
                        BT.Add(EP[i].p.x);
                        break;
                    case ST.Left:
                        double begin = S[EP[i].seg].p1.x;
                        double end = S[EP[i].seg].p2.x;
                        var vol = BT.Where(a => a >= begin && a <= end).Count();
                        cnt += vol;
                        break;
                }
            }
            return cnt;
        }
    }

    class EndPoint : IComparable
    {
        internal Point p;
        internal int seg;
        internal ST st;

        public EndPoint(Point p, int seg, ST st)
        {
            this.p = p;
            this.seg = seg;
            this.st = st;
        }

        public int CompareTo(object obj)
        {
            EndPoint other = obj as EndPoint;

            if (p.y < other.p.y) return -1;
            if (p.y > other.p.y) return 1;
            if (st < other.st) return -1;
            if (st > other.st) return 1;
            return 0;
        }
    }

    class Point : IComparable
    {
        static readonly double EPS = 1E-10;

        internal double x, y;

        public Point(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(Point p) { return x * p.x + y * p.y; }
        public double Cross(Point p) { return x * p.y - y * p.x; }
        public double Distance(Point p) { return (this - p).Abs; }

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

        public int CompareTo(object obj)
        {
            var other = obj as Point;
            if (other == null) return -1;

            ComparerX compX = new ComparerX();
            return compX.Compare(this, other);
        }

        public override bool Equals(object obj)
        {
            var other = obj as Point;
            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} {1}", x, y);
        }
    }

    class ComparerX : IComparer<Point>
    {
        static readonly double EPS = 1E-10;

        public int Compare(Point a, Point b)
        {
            if (a.x - b.x < -EPS) return -1;
            if (a.x - b.x > EPS) return 1;
            if (a.y - b.y < -EPS) return -1;
            if (a.y - b.y > EPS) return 1;
            return 0;
        }
    }

    class ComparerY : IComparer<Point>
    {
        static readonly double EPS = 1E-10;

        public int Compare(Point a, Point b)
        {
            if (a.y - b.y < -EPS) return -1;
            if (a.y - b.y > EPS) return 1;
            if (a.x - b.x < -EPS) return -1;
            if (a.x - b.x > EPS) return 1;
            return 0;
        }
    }

    class Segment
    {
        internal Point p1, p2;

        public Segment(Point p1, Point p2)
        {
            this.p1 = p1;
            this.p2 = p2;
        }
    }

    class Circle
    {
        internal Point p;
        internal double r;

        public Circle(Point p, double r)
        {
            this.p = p;
            this.r = r;
        }
    }
}