Count the camel pairs ranked in the same relative order in all three given bets.
Medium6Segment treeSortingDivide and conquerInterviewNo attempts yetTime limit10sMemory limit512 MBJaap, Jan, and Thijs went on a trip to the desert. After they came back from a camel ride, their guide invited them to a big camel race in the evening. The camels they rode also run in that race, and betting on the result is the local custom.
The most interesting bet is guessing the complete order in which the camels finish. It pays the most because it is the hardest to get right.
The three of them have already handed in their bets, but the race starts only an hour from now, so they are bored. They started wondering how many pairs of camels they put in the same order. If camel c comes before camel d in Jaap's bet, in Jan's bet, and in Thijs' bet, then all three put c and d in the same order. Count the pairs of camels for which this happened.
The input consists of:
The camels are numbered 1 through n. Each camel appears exactly once in each bet.
Output the number of pairs of camels that appear in the same order in all three bets. A pair {c,d} counts when all three put c before d, or all three put d before c.