#!/usr/bin/python
import time
def time_func(func, arg):
        t1=time.time()
        ret=func(arg)
        t2=time.time()
        return str(arg)+" "+ str((t2-t1)*1000.0)

# We are assuming that the dictionary operations that we execute in this code each
# require O(1) time, in particular, d[x]=y, x in d, iterating through the keys in 
# a dictionary (we should verify this!). These are very big assumptions!

def squares_2(n): # = 1+O(n^2) = O(n^2)
        file_name="points_"+str(n)+".txt"
	return squares(file_name)

 # O(n^2)
def squares(file_name): # = 1+1+8n+1+n+7n^2+1+1 (exercise, prove this is O(n^2))
	f=open(file_name,"r") # 1
	points = {} # 1
	for line in f: # n*(cost of for) + n*cost of the body = n+n*7 = 8n
		line=line.rstrip('\n') # cost = 1 (assuming short lines)
		val = line.split() # cost = 1 (assuming short lines)
		(x,y)=(int(val[0]), int(val[1])) # cost = 1
		try:
			points[(x,y)]=points[(x,y)]+1 # cost = 3
		except:
			points[(x,y)]=1 # cost = 1
	
	squares = 0 # 1

	# n*1 + n* cost of body
	# n*1+n*(7n)= n+7n^2
	for (x1,y1) in points:

		# n * 1 + n * cost of body =  n*1 + n*6 = 7n
		for (x2,y2) in points:

			# cost of condition + cost of body
			# 2+ 4 =6
			if x1==x2 and y1<y2: 
				side=y2-y1 # cost=1
				if (x1+side,y1) in points and (x1+side, y2) in points: 
					# cost of condition + cost of body
					# = 3
					squares=squares+1 # 1
	print squares # cost = 1
	return squares # cost = 1

file=open("squares.data","w")
sizes=[1000,3000,5000,10000,20000]
for n in sizes:
        file.write(time_func(squares_2,n)+"\n")
file.close()
