Falling Portals

시간 제한2초메모리 제한512 MB

요약
세계 i는 속도 i로 아래로 떨어지고, 높이가 같아지면 소가 다른 세계로 이동한다. i에서 Q_i로 가는 최단 시간을 기약분수로 구하거나 불가능하면 -1을 출력한다.
난이도

어려움10점 중 9점

유형
기하, 그래프, 최단 경로, 정수론
정답자
아직 제출이 없습니다

문제

There are NN (2≤N≤2⋅1052\le N\le 2\cdot 10^5) worlds, each with a portal. Initially, world ii (for 1≤i≤N1 \leq i \leq N) is at xx-coordinate ii, and yy-coordinate A_iA\_i (1≤A_i≤1091\le A\_i\le 10^9). There is also a cow on each world. At time 00, all yy-coordinates are distinct and the worlds start falling: world ii moves continuously in the negative-yy direction at a speed of ii units per second.

At any time when two worlds are at the same yy-coordinate (possibly a fractional time), the portals "align", meaning that a cow on one of the worlds can choose to travel instantaneously to the other world.

For each ii, the cow on world ii wants to travel to world Q_iQ\_i (Q_i≠iQ\_i\neq i). Help each cow determine how long her journey will take, if she travels optimally.

Each query output should be a fraction a/ba/b where aa and bb are positive and relatively prime integers, or −1-1 if it the journey is impossible.

입력

The first line of input contains a single integer N.N.

The next line contains NN space-separated integers A_1,A_2,…,A_N.A\_1,A\_2,\ldots,A\_N.

The next line contains NN space-separated integers Q_1,Q_2,…,Q_N.Q\_1,Q\_2,\ldots,Q\_N.

출력

Print NN lines, the ii-th of which contains the journey length for cow i.i.

힌트

Consider the answer for the cow originally on world 2. At time 22 worlds 1 and 2 align, so the cow can travel to world 1. At time 72\frac{7}{2} worlds 1 and 3 align, so the cow can travel to world 3.

예제1

  1. 예제 1

    입력
    4
    3 5 10 2
    3 3 2 1
    
    예상 출력
    7/2
    7/2
    5/1
    -1