use Hashmap and hashset to categorize different lines, O(n^2) AC.