Kotlin Heroes: Episode 10 |
---|

Finished |

The following languages are only available languages for the problems from the contest

Kotlin Heroes: Episode 10:

- Kotlin 1.7.20
- Kotlin 1.9.21

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

*special problem

*2400

No tag edit access

The problem statement has recently been changed. View the changes.

×
G. Observation Towers

time limit per test

3 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputConsider a segment of an OX axis from $$$1$$$ to $$$n$$$. There are $$$k$$$ observation towers on this segment.

Each tower has two parameters — the coordinate $$$x_i$$$ and the height $$$h_i$$$. The coordinates of all towers are distinct. From tower $$$i$$$, you can see point $$$j$$$ if $$$|x_i - j| \le h_i$$$ (where $$$|a|$$$ is an absolute value of $$$a$$$).

You can increase the height of any tower by $$$1$$$ for one coin. The height of each tower can be increased any number of times (including zero).

You need to process $$$q$$$ independent queries. In the $$$i$$$-th query, two different points $$$l$$$ and $$$r$$$ are given. You need to calculate the minimum number of coins required to be able to see both of these points (from one tower or from different towers).

Input

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le n$$$) — the length of the segment of an OX axis and the number of observation towers.

The second line contains $$$k$$$ integers $$$x_1, x_2, \dots, x_k$$$ ($$$1 \le x_i \le n$$$) — the coordinates of the observation towers. The coordinates of all towers are distinct.

The third line contains $$$k$$$ integers $$$h_1, h_2, \dots, h_k$$$ ($$$0 \le h_i \le n$$$) — the heights of the observation towers.

The fourth line contains a single integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) — the number of queries.

Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) — the description of the next query.

Output

For each query, output a single integer — the minimum number of coins required to be able to see both points (from one tower or from different towers).

Examples

Input

20 32 15 106 2 031 2010 113 4

Output

3 1 0

Input

10 22 91 234 55 61 10

Output

2 2 0

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/24/2024 02:25:50 (l3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|