#HK4012. 「eJOI2023」Teleporters

「eJOI2023」Teleporters

Description

Anna and Beka are at different points on a coordinate line, planning to meet. Their only means of movement is through the use of teleporters.

There are NN teleporters, with the i-th teleporter located at coordinate c[i]c[i] and operating at a frequency denoted as f[i]f[i]. However, not all of them are currently available; only those within the frequency range [L,R][L, R] can be used.

Using a teleporter takes a minute and transports its user to a coordinate that is a reflection of the original coordinate around the teleporters location. In other words, if the original coordinate was x1x_1, then after using teleporter ii, the resulting coordinate x2x_2 will satisfy the equation (x1+x2)/2=c[i](x_1 + x_2)/2 = c[i].

Each minute, Anna and Beka must use one of the available teleporters (not necessarily different ones). They will communicate during teleportation and experience discomfort equal to the absolute difference of the frequencies of the teleporters they are using. The overall difficulty of the travel is defined as the maximum discomfort they have experienced.

You will be asked about QQ different scenarios, and for each one, your task is to determine whether Anna and Beka can ever meet using available teleporters, and if so, what the minimum possible travel difficulty is. A single scenario is described 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

For each scenario, print the minimum travel difficulty if they can meet and 1-1 otherwise. Please note that the total travel time is irrelevant for the purposes of this task.

Input Format

The first line contains two integers: NN and QQ.

The second line contains NN integers: c[1],c[2],,c[N]c[1], c[2], \ldots , c[N].

The third line contains NN integers: f[1],f[2],,f[N]f[1], f[2], \ldots , f[N].

Each of the following QQ lines describes one scenario with four integers: AA, BB, LL and RR (AB)(A \neq B).

Output Format

Print QQ space-separated integers in a single line: answers to the scenarios 1,2,,Q1, 2, \ldots ,Q.

4 3
4 6 8 10
7 1 9 4
3 11 1 50
3 11 1 5
5 7 1 1
2 3 -1

In the first scenario, if Anna uses teleporter 22 and Beka uses teleporter 44, they will meet at coordinate 99 with a discomfort of 14=3|1 - 4| = 3.

A better solution is if Anna uses teleporter 11 and Beka uses teleporter 33 instead; in this case, they meet at F=5F = 5 and experience a discomfort of 79=2|7 - 9| = 2.

In the second scenario, the better option is no longer available due to restriction on the frequency range.

In the third scenario, there is only one available teleporter, and meeting isn't possible.

3 3
-2 1 -1
10 1 3
-6 6 20 20
-6 6 0 20
-6 6 2 20
-1 2 7

Coordinates can be negative.

Constraints

  • 2N500002 \leq N \leq 50\,000
  • 1Q500001 \leq Q \leq 50\,000
  • 1f[i]1091 \leq f [i] \leq 10^9
  • 109c[i],A,B109-10^9 \leq c[i], A, B \leq 10^9
  • 1LR101 \leq L \leq R \leq 10

Subtasks

  1. (11 points) N,Q10N , Q \leq 10; c[i],f[i]50|c[i]|, f [i] \leq 50 for each 1iN1 \leq i \leq N.
  2. (10 points) N100N \leq 100; L=1L = 1; R=100R = 100; c[i],f[i]100|c[i]|, f[i] \leq 100 for each 1iN1 \leq i \leq N.
  3. (5 points) N=2N = 2; L=1L = 1; R=109R = 10^9
  4. (9 points) N1000N \leq 1\,000; L=1L = 1; R=109R = 10^9; f[i]=1f[i] = 1 for each 1iN1 \leq i \leq N.
  5. (6 points) L=1L = 1; R=109R = 10^9; f[i]=1f[i] = 1 for each 1iN1 \leq i \leq N.
  6. (7 points) N1000N \leq 1\,000; L=1L = 1; R=109R = 10^9
  7. (17 points) L=1L = 1; R=109R = 10^9
  8. (8 points) L=1L = 1
  9. (14 points) N,Q20000N , Q \leq 20\,000
  10. (13 points) No additional constraints.