Under construction

Still being built. Things may change

Teleporters

Time limit2sMemory limit1024 MB

Summary
Given teleporters with frequencies in [L, R], find the minimum possible maximum frequency gap for Anna and Beka to meet, or report that they cannot.
Level

Hard8 of 10

Topics
Number theory, Math, Union-find
Solved
No attempts yet

Problem

Anna and Beka are at different points on a coordinate line and want to meet. Their only way to move is by teleporters.

There are NN teleporters. The ii-th one is located at coordinate cic_i and operates at frequency fif_i. Only the teleporters with frequency in the range [L,R][L, R] are available.

Using a teleporter takes one minute and moves the user to the reflection of their current coordinate across the teleporter's location. If the coordinate before use is x1x_1 and the coordinate after use is x2x_2, then (x1+x2)/2=ci(x_1 + x_2) / 2 = c_i.

Every minute, Anna and Beka must each use one of the available teleporters (not necessarily the same one). While they teleport, they feel discomfort equal to the absolute difference between the frequencies of the two teleporters they use. The travel difficulty is the maximum discomfort felt over the whole trip.

There are QQ scenarios. For each one, determine whether Anna and Beka can ever meet using the available teleporters. If they can, print the minimum possible travel difficulty. The total travel time does not affect the answer.

Each scenario is given by four integers:

  • AA: Anna's starting coordinate
  • BB: Beka's starting coordinate
  • LL: the minimum frequency of the available teleporters
  • RR: the maximum frequency of the available teleporters

Input

The first line contains two integers NN and QQ.

The second line contains NN integers c1,c2,…,cNc_1, c_2, \dots, c_N.

The third line contains NN integers f1,f2,…,fNf_1, f_2, \dots, f_N.

Each of the following QQ lines contains four integers AA, BB, LL, and RR describing one scenario. It is guaranteed that A≠BA \ne B.

Output

Print QQ space-separated integers on a single line. The jj-th integer is the answer for scenario jj.

Constraints

  • 2≤N≤500002 \le N \le 50000
  • 1≤Q≤500001 \le Q \le 50000
  • 1≤fi≤1091 \le f_i \le 10^9
  • −109≤ci,A,B≤109-10^9 \le c_i, A, B \le 10^9
  • 1≤L≤R≤1091 \le L \le R \le 10^9

Examples2

  1. Example 1

    Input
    4 3
    4 6 8 10
    7 1 9 4
    3 11 1 50
    3 11 1 5
    5 7 1 1
    
    Expected output
    2 3 -1
    
  2. Example 2

    Input
    3 3
    -2 1 -1
    10 1 3
    -6 6 20 20
    -6 6 0 20
    -6 6 2 20
    
    Expected output
    -1 2 7