This page is still under construction.

Parts of this page are still being built. What you see may change.

Travel between two islands

Interview

Time limit1sMemory limit16 MB

Summary
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

NN islands numbered 11 to NN stand in a row. No bridge connects them yet, so people can only travel by boat, and the government decided to build the N−1N-1 bridges that join island ii to island i+1i+1. 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 (i,j)(i, j) with i<ji < j that can reach each other.
  • The sum, over those pairs, of the smallest number of bridges you have to cross to get from island ii to island jj.

Report both values after every bridge.

Input

The first line contains the number of islands NN (2≤N≤1052 \le N \le 10^5).

Each of the next N−1N-1 lines contains one integer ii (1≤i<N1 \le i < N), meaning that the bridge joining island ii and island i+1i+1 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 N−1N-1 lines in total.

Examples5

  1. Example 1

    Input
    3
    1
    2
    
    Expected output
    1 1
    3 4
    
  2. Example 2

    Input
    2
    1
    
    Expected output
    1 1
    
  3. Example 3

    Input
    4
    1
    2
    3
    
    Expected output
    1 1
    3 4
    6 10
    
  4. Example 4

    Input
    4
    3
    2
    1
    
    Expected output
    1 1
    3 4
    6 10
    
  5. Example 5

    Input
    5
    2
    3
    1
    4
    
    Expected output
    1 1
    3 4
    6 10
    10 20