Flights

Time limit3sMemory limit1024 MB

Summary
For each query, find the maximum altitude among a subset of parabolic missile trajectories (indexed by launch time) restricted to a horizontal range, and output the exact reduced fraction.
Level

Hard8 of 10

Topics
Geometry, Segment tree, Math, Binary search
Solved
No attempts yet

Statement

The battlefield is a straight line. Artillery launches ballistic missiles and aviation plans flights over the same line; you must compute the minimal safe altitude for every flight.

A ballistic missile is launched from the ground (altitude 00) at a point pp and flies along a vertically symmetric parabola whose highest point is (x, y)(x,\,y): its horizontal coordinate is xx and its peak altitude is yy. At a horizontal coordinate uu the missile altitude is

y(1−(u−x)2(x−p)2),y\left(1-\frac{(u-x)^2}{(x-p)^2}\right),

and the missile exists only along the arc above the ground, i.e. for u∈[p, 2x−p]u\in[p,\ 2x-p]; outside that range it has no trajectory.

Missiles are launched one per minute in input order, so missile ii is launched at minute ii. A flight is given by a time interval [t1, t2][t_1,\,t_2] and a space interval [x1, x2][x_1,\,x_2] (both inclusive). Its minimal safe altitude is the smallest altitude at or below which every missile launched during [t1, t2][t_1,\,t_2] stays throughout the horizontal range [x1, x2][x_1,\,x_2]. Equivalently, it is the maximum altitude reached by any such missile over u∈[x1, x2]u\in[x_1,\,x_2], counting only the part of each trajectory that is above the ground. If no such missile reaches any point of [x1, x2][x_1,\,x_2], the minimal safe altitude is 00.

Input

The first line contains one integer nn — the number of planned missile launches (1≤n≤500001\le n\le 50000).

Each of the next nn lines contains three integers pp, xx, yy describing one launch: the launch point pp and the highest point (x, y)(x,\,y) of the trajectory (0≤p<x≤500000\le p<x\le 50000, 0<y≤500<y\le 50). Missiles are launched one by one every minute in the order given; launch ii happens at minute ii.

The next line contains one integer mm — the number of planned flights (1≤m≤200001\le m\le 20000).

Each of the next mm lines contains four integers t1t_1, t2t_2, x1x_1, x2x_2: the time interval [t1, t2][t_1,\,t_2] (1≤t1≤t2≤n1\le t_1\le t_2\le n) and the space interval [x1, x2][x_1,\,x_2] (0≤x1≤x2≤500000\le x_1\le x_2\le 50000). Both intervals include their endpoints. Minute 11 is the first launch and minute nn is the last.

Output

For each flight, print on its own line the minimal safe altitude as an exact irreducible fraction p/q, where q≥1q\ge 1 and gcd⁡(p, q)=1\gcd(p,\,q)=1. The altitude is always a non-negative rational number, so p≥0p\ge 0; print a zero altitude as 0/1.

Examples4

  1. Example 1

    Input
    2
    10 30 10
    20 30 30
    4
    1 2 0 11
    1 2 20 25
    1 2 25 35
    1 2 45 100
    
    Expected output
    39/40
    45/2
    30/1
    35/8
    
  2. Example 2

    Input
    2
    0 10 10
    30 40 10
    6
    1 2 0 32
    1 1 19 35
    2 2 0 32
    1 2 15 35
    1 2 21 27
    1 2 2 100
    
    Expected output
    10/1
    19/10
    18/5
    15/2
    0/1
    10/1
    
  3. Example 3

    Input
    1
    0 5 7
    1
    1 1 0 10
    
    Expected output
    7/1
    
  4. Example 4

    Input
    1
    0 5 7
    1
    1 1 20 30
    
    Expected output
    0/1