#P3660. Crossing Paths

    ID: 16911 Type: Default 1000ms 256MiB

Crossing Paths

Crossing Paths

Farmer John's farm is very unique. Around the perimeter of the main field, there is a large circular road. Every morning, the cows cross this road as they head into the field, and every evening they all cross again to return to the barn. Each cow always crosses the road in the same manner every day. In fact, each cow has two distinct crossing points: one where she enters the field and another where she exits. Note that for each cow, the two crossing points are different and all crossing points (across all cows) are distinct.

Farmer John has \(N\) cows, identified with the IDs \(1, 2, \dots, N\). Therefore, there are \(2N\) crossing points on the circle. Farmer John records these crossing points by scanning clockwise along the circular road and writing down the cow's ID at each crossing point, forming a sequence of \(2N\) numbers in which every number appears exactly twice. However, he does not record which point is the entry and which is the exit.

A pair of cows \((a,b)\) is called a crossing pair if the chord (line connecting the two points) that corresponds to cow \(a\)'s crossing and the chord corresponding to cow \(b\)'s crossing must intersect when drawn inside the circle. In other words, if we denote the two positions (in clockwise order) for cow \(a\) as \(a_1\) and \(a_2\) and those for cow \(b\) as \(b_1\) and \(b_2\), then without loss of generality if \(a_1 < b_1\), the pair \((a,b)\) is crossing if and only if:

[ a_1 < b_1 < a_2 < b_2 ]

Your task is to calculate the total number of crossing pairs among all cows.

inputFormat

The first line contains a single integer \(N\) (where \(1 \leq N\)) representing the number of cows. The second line contains \(2N\) space-separated integers which form the sequence of crossing points recorded in clockwise order. Each integer is between \(1\) and \(N\) (inclusive) and appears exactly twice.

outputFormat

Output a single integer, the total number of crossing pairs.

sample

4
3 2 4 4 1 3 2 1
3