#P5465. [PKUSC2018] 星际穿越

[PKUSC2018] 星际穿越

Description

There are nn planets, numbered from 1 to nn. They are located in the same galaxy, which can be modeled as a number line. Each planet is a point on the number line. In particular, the coordinate of planet ii is ii.

At the beginning, due to technological limits, the residents of these nn planets could not communicate with each other, so they did not know each other existed. Now, these planets have independently developed tools for interstellar travel and interstellar communication. For planet ii, by sending a powerful signal, it successfully made contact with all planets with indices in [li,i1][l_i,i-1] (planet 1 did not send any signal). Any two planets that make contact will build a bidirectional portal. For two planets u,vu,v that have a portal between them, residents on uu can spend 1 unit of time to teleport to vv, and residents on vv can also spend 1 unit of time to teleport to uu. Let dist(x,y)dist(x,y) denote the minimum time needed to travel from planet xx to planet yy through a sequence of portals between planets.

Now there are qq interstellar merchants. The initial position of the ii-th merchant is xix_i, and his destination is one planet among [li,ri][l_i,r_i], where it is guaranteed that li<ri<xil_i<r_i<x_i. He will choose a planet yy in this interval uniformly at random (each planet has the same probability of being chosen as the destination), and then reach planet yy through portals, using the minimum possible time. The merchant wants to know the expected time he spends, i.e., compute $\frac{1}{r_i-l_i+1}{\sum_{y=l_i}^{r_i}{dist(x_i,y)}}$.

Input Format

The first line contains a positive integer nn, representing the number of planets.

The second line contains n1n-1 positive integers. The ii-th positive integer is li+1l_{i+1}, meaning that all planets with indices in the interval [li+1,i][l_{i+1},i] have already made contact with planet i+1i+1, and they can transmit to each other with cost 1. It is guaranteed that li+1il_{i+1}\leq i.

The third line contains a positive integer qq, representing the number of queries.

The next qq lines each contain three numbers li,ri,xil_i,r_i,x_i, meaning: choose a planet yy uniformly at random in [li,ri][l_i,r_i], and ask for the expected value of dist(xi,y)dist(x_i,y). It is guaranteed that li<ri<xil_i<r_i<x_i.

Output Format

For each query, note that the answer must be a rational number, so output it in the form p/qp/q, requiring gcd(p,q)=1\gcd(p,q)=1.

If the answer is an integer mm, output m/1m/1.

7
1 1 2 1 4 6
5
3 4 6
1 5 7
1 2 4
1 2 6
1 3 5
3/2
13/5
3/2
2/1
1/1

Hint

The undirected graph corresponding to the sample is as follows: ex

For 20%20\% of the testdata, n100n \leq 100.

For another 25%25\% of the testdata, n2000n\leq 2000.

For another 25%25\% of the testdata, n5000n\leq 5000.

For 100%100\% of the testdata, n,q3×105n,q\leq 3\times 10^5.

Translated by ChatGPT 5