Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

Chef is working with the development of a new city Krewsko. He and other city planners are designing the transportation system of Krewsko. They are using Cartesian coordinates and Krewsko will only have roads that are parallel to either the x-axis or the y-axis.

It is clear that we need to add one set of traffic lights at every intersection of roads where a person has at least two other directions to continue their trip when arriving at the intersection. Your task is to count how many traffic light sets should be deployed. In particular, for each road you want to know how many traffic light sets will appear on that road.

Let's formalize how a road in the city is defined. A road is a segment on the plane with endpoints in X_{i,1}, Y_{i,1} and X_{i,2}, Y_{i,2} for i^{th} line. You are guaranteed that each line segment is either a vertical line or a horizontal line. An intersection is a point that belongs to two line segments: one horizontal and another vertical. We guarantee that no two vertical lines will share a point and no two horizontal lines share a point.

You must put a set of lights at each intersection of a horizontal and a vertical line segment where the point of intersection is not an endpoint of both segments.

------ Input ------ 

The first line of the input contains an integer N indicating number of roads.
The next N lines contain 4 integers each. The ^{i}th line contain X_{i,1}, Y_{i,1} and X_{i,2}, Y_{i,2}.

------ Output ------ 

The output consists of two lines. The first line contains a single
integer indicating the number of sets of lights that will be used
throughout all of Krewsko. The second line consists of N
integers. The i'th such integer indicates the number of traffic light
sets that will be deployed to the i'th road. There should be a single space between consecutive integers on this line.

------ Constraints ------ 

$1 ≤ N ≤ 10^{5}$
$1 ≤ X_{i,1} ≤ 10^{5}$
$1 ≤ Y_{i,1} ≤ 10^{5}$
$1 ≤ X_{i,2} ≤ 10^{5}$
$1 ≤ Y_{i,2} ≤ 10^{5}$
$The length of each road is positive and each road will either be a horizontal line segment or a vertical line segment.$

------ Subtasks ------ 

$Subtask #1 [40 points]: N ≤ 10000$
$Subtask #2 [60 points]: No additional constraints$

------ Example ------ 

Input:
4
2 1 2 6
1 3 5 3
4 1 4 4
1 5 5 5
Output:
3
2 2 1 1

Source: codechef | Difficulty: HARD
Tags: Geometry

Sweep line algorithms

Range queries

Segment trees revisited

Data structures


Solutions:
N = 10 ** 5 + 10

class Bit:

	def __init__(self, n):
		self.n = n
		self.arr = [0] * (n + 1)

	def update(self, i, v):
		i += 1
		while i <= self.n:
			self.arr[i] += v
			i += i & -i

	def getsum(self, i):
		s = 0
		i = i + 1
		while i > 0:
			s += self.arr[i]
			i -= i & -i
		return s

	def print(self):
		print(self.arr)

def horizontal(x1, y1, x2, y2):
	return y1 == y2

def main():
	tc = int(input())
	allowed = []
	blocked = []
	hevents = []
	vevents = []
	hendpoints = {}
	vendpoints = {}
	ans = [-1] * tc
	for t in range(tc):
		(x1, y1, x2, y2) = tuple(map(int, input().split()))
		if horizontal(x1, y1, x2, y2):
			if x1 > x2:
				(x1, x2) = (x2, x1)
			vevents.append((y1, (x1, x2), 'Q', t))
			hevents.append((x1, y1, 'A'))
			hevents.append((x2, y2, 'S'))
			hendpoints[x1, y1] = 1
			hendpoints[x2, y2] = 1
		else:
			if y1 > y2:
				(y1, y2) = (y2, y1)
			hevents.append((x1, (y1, y2), 'Q', t))
			vevents.append((y1, x1, 'A'))
			vevents.append((y2, x2, 'S'))
			vendpoints[y1, x1] = 1
			vendpoints[y2, x2] = 1
	hevents.sort(key=lambda x: (x[0], x[2]))
	vevents.sort(key=lambda x: (x[0], x[2]))
	bh = Bit(N)
	for e in hevents:
		if e[2] == 'A':
			bh.update(e[1], 1)
		if e[2] == 'Q':
			start = e[1][0] - 1
			end = e[1][1]
			if (e[0], start + 1) in hendpoints:
				start += 1
			if (e[0], end) in hendpoints:
				end -= 1
			sum1 = bh.getsum(end)
			sum2 = bh.getsum(start)
			ans[e[3]] = sum1 - sum2
		if e[2] == 'S':
			bh.update(e[1], -1)
	bv = Bit(N)
	for e in vevents:
		if e[2] == 'A':
			bv.update(e[1], 1)
		if e[2] == 'Q':
			start = e[1][0] - 1
			end = e[1][1]
			if (e[0], start + 1) in vendpoints:
				start += 1
			if (e[0], end) in vendpoints:
				end -= 1
			sum1 = bv.getsum(end)
			sum2 = bv.getsum(start)
			ans[e[3]] = sum1 - sum2
		if e[2] == 'S':
			bv.update(e[1], -1)
	total = sum(ans) // 2
	print(total)
	print(*ans, sep=' ')
main()