output_description: Print the number of intersections in a line.
input_description: 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.
attempt: import bisect
  import sys
  from typing import List, Tuple, Set
  
  
  class BIT(object):
      def __init__(self, n: int) -> None:
          self.tree = [0] * (n + 1)
          self.n = n
  
      def add(self, i: int, v: int) -> None:
          while i <= self.n:
              self.tree[i] += v
              i += i & -i
  
      def _sum(self, i: int) -> int:
          ret = 0
          while i:
              ret += self.tree[i]
              i -= i & -i
          return ret
  
      def sum(self, l: int, h: int) -> int:
          return self._sum(h) - self._sum(l - 1)
  
  
  if __name__ == "__main__":
      n = int(input())
      vx: List[Tuple[int, int, int]] = []
      x_init_set: Set[int] = set()
      for _ in range(n):
          x1, y1, x2, y2 = map(lambda x: int(x), input().split())
          if x1 == x2:
              if y1 > y2:
                  y1, y2 = y2, y1
              vx.append((y1, -sys.maxsize, x1))
              vx.append((y2, sys.maxsize, x1))
              x_init_set.add(x1)
          else:
              if x1 > x2:
                  x1, x2 = x2, x1
              vx.append((y1, x1, x2))
      vx.sort()
  
      bit = BIT(len(x_init_set))
      xs = [-sys.maxsize] + sorted(x_init_set)
      ix = {v: i for i, v in enumerate(xs)}
      ans = 0
  
      for y, j, x2 in vx:
          if j == -sys.maxsize:
              bit.add(ix[x2], 1)
          elif j == sys.maxsize:
              bit.add(ix[x2], -1)
          else:
              i1 = bisect.bisect_left(xs, j)
              i2 = bisect.bisect(xs, x2) - 1
              ans += bit.sum(i1, i2)
  
      print(ans)
  
problem_description: Segment Intersections: Manhattan Geometry
  
  For given $n$ segments which are parallel to X-axis or Y-axis, find the number
  of intersections of them.
samples: [{"input": "6\n    2 2 2 5\n    1 3 5 3\n    4 1 4 4\n    5 2 7 2\n    6 1 6 3\n    6 5 6 7", "output": "3"}]