Travel between two islands
InterviewTime limit1sMemory limit16 MB
After each bridge joins two neighboring islands, report how many island pairs are mutually reachable and the total bridge crossings summed over those pairs.
- Level
Medium5 of 10
- Topics
- Union-find, Math
- Solved
- No attempts yet
Problem
islands numbered to stand in a row. No bridge connects them yet, so people can only travel by boat, and the government decided to build the bridges that join island to island . The bridges cannot all be built at once, so they are finished one at a time in a fixed order.
Each time a bridge is finished, the government wants two values.
- The number of island pairs with that can reach each other.
- The sum, over those pairs, of the smallest number of bridges you have to cross to get from island to island .
Report both values after every bridge.
Input
The first line contains the number of islands ().
Each of the next lines contains one integer (), meaning that the bridge joining island and island is built at that turn. No number appears twice.
Output
After each bridge is built, print the two values on one line, separated by a space. Print lines in total.