Running Deeper C/C++/Java

Problems/USACO Problems and Solutions

TreasureSharky

Why Did the Cow Cross the Road III (Gold)

영어   

시간 제한메모리 제한제출정답맞은 사람정답 비율

2 초 512 MB 134 75 59 53.153%

문제

The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn.

As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns NN cows, conveniently identified with the integer IDs 1…N, so there are precisely 2N2N crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the ID of the cow for each crossing point, ultimately forming a sequence with 2N numbers in which each number appears exactly twice. He does not record which crossing points are entry points and which are exit points.

Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a,b) a "crossing" pair if cow a's path from entry to exit must cross cow b's path from entry to exit. Please help Farmer John count the total number of crossing pairs.

입력

The first line of input contains N (1 ≤ N ≤ 50,000), and the next 2N lines describe the cow IDs for the sequence of entry and exit points around the field.

출력

Please print the total number of crossing pairs.

 

This question is much harder. Simplified, the problem results to: 

for a sequence such as 1 2 3 4 2 3 4 5 5 1, for each number, find the number of integers that only appear once between the pair.

Using a fenwick/segmentated tree, this is simple.

'; document.write(x.substring(x.length-900,x.length;
Comments