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

require 'matrix'
class BTree
  attr_accessor :root
  class Node
    attr_accessor :r, :l, :key, :p

    def initialize(key)
      @key = key
      @p, @r, @l = nil, nil, nil
    end
  end

  def initialize
    @root = nil
  end

  def insert(key)
    n = Node.new(key)
    y = nil
    x = @root

    while x != nil
      y = x
      x = if n.key < x.key
        x.l
      else
        x.r
      end
    end
    n.p = y

    if y == nil
      @root = n
    elsif n.key < y.key
      y.l = n
    else
      y.r = n
    end
  end

  def find(key)
    n = @root
    while n != nil
      return n if n.key == key
      n = if key < n.key
        n.l
      else
        n.r
      end
    end
    return nil
  end

  def remove(key)
    n = find(key)
    if n.l == nil && n.r == nil
      x = nil
    elsif n.l && n.r
      x = n.l
      while x.r
        x = x.r
      end

      x.p.r = nil
      x.l = n.l
      x.r = n.r
      x.p = n.p
    else
      if n.l
        x = n.l
      else
        x = n.r
      end
      x.p = n.p
    end

    if n.p == nil
      @root = x
    elsif n.key < n.p.key
      n.p.l = x
    else
      n.p.r = x
    end
  end

end

class EndPoint
  attr_accessor :type, :p, :seg
  def initialize(p, type, seg)
     @type, @p, @seg = type, p, seg
  end
end

ep = []
s = []
gets.to_i.times do |i|
  x1, y1, x2, y2 = gets.split.map &:to_i
  s[i] = {}
  if x1 == x2
    if y1 > y2
      s[i]["TOP"] = EndPoint.new(Vector[x1, y1], "TOP", i)
      s[i]["BOTTOM"] = EndPoint.new(Vector[x2, y2], "BOTTOM", i)
      ep << s[i]["TOP"]
      ep << s[i]["BOTTOM"]
    else
      s[i]["BOTTOM"] = EndPoint.new(Vector[x1, y1], "BOTTOM", i)
      s[i]["TOP"] = EndPoint.new(Vector[x2, y2], "TOP", i)
      ep << s[i]["TOP"]
      ep << s[i]["BOTTOM"]
    end
  else
    if x1 > x2
      s[i]["RIGHT"] = EndPoint.new(Vector[x1, y1], "RIGHT", i)
      s[i]["LEFT"] = EndPoint.new(Vector[x2, y2], "LEFT", i)
      ep << s[i]["RIGHT"]
      ep << s[i]["LEFT"]
    else
      s[i]["LEFT"] = EndPoint.new(Vector[x1, y1], "LEFT", i)
      s[i]["RIGHT"] = EndPoint.new(Vector[x2, y2], "RIGHT", i)
      ep << s[i]["RIGHT"]
      ep << s[i]["LEFT"]
    end
  end
end

ep.sort! { |a, b| a.p[1] <=> b.p[1] }
bt = BTree.new
result = 0
while !ep.empty?
  n = ep.shift
  if n.type == "TOP"
    bt.remove(n.p[0])
  elsif n.type == "BOTTOM"
    bt.insert(n.p[0])
  elsif n.type == "LEFT"
    l = s[n.seg]["LEFT"].p[0]
    r = s[n.seg]["RIGHT"].p[0]
    (l..r).each do |i|
      result += 1 if bt.find(i)
    end
  end
end

puts result