Croc Champ 2012 - Round 1
E
Camping Groups
Codeforces
2600
原题
← D→
翻译中…

A club wants to take its members camping. In order to organize the event better the club directors decided to partition the members into several groups.

翻译中…

Club member $*i*$ has a responsibility value $*r*_{*i*}$ and an age value $*a*_{*i*}$. A group is a non-empty subset of club members with one member known as group leader. A group leader should be one of the most responsible members of the group (his responsibility value is not less than responsibility of any other group member) and his age absolute difference with any other group member should not exceed $*k*$.

翻译中…

Some club members are friends and want to be in the same group. They also like their group to be as large as possible. Now you should write a program that answers a series of questions like "What's the largest size of a group containing club member $*x*$ and club member $*y*$?". It's possible for $*x*$ or $*y*$ to be the group leader.

翻译中…

Input

翻译中…

The first line contains two integers $*n*$ and $*k*$ ($2 ≤ *n* ≤ 10^{5}, 0 ≤ *k* ≤ 10^{9}$) — the number of club members and the age restriction for one group.

翻译中…

The next line contains integer numbers $*r*_{1}, *r*_{2}, ..., *r*_{*n*}$ ($1 ≤ *r*_{*i*} ≤ 10^{9}$) separated by space: $*r*_{*i*}$ denotes the $*i*$-th club member's responsibility. In the same way there are integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ ($1 ≤ *a*_{*i*} ≤ 10^{9}$) in the third line: $*a*_{*i*}$ denotes the $*i*$-th club member's age.

翻译中…

The next line contains an integer $*q*$ denoting the number of questions that you should answer ($1 ≤ *q* ≤ 10^{5}$). The next $*q*$ lines describe the questions. Each line contains two space-separated integers $*x*_{*i*}$ and $*y*_{*i*}$ ($1 ≤ *x*_{*i*}, *y*_{*i*} ≤ *n*, *x*_{*i*} ≠ *y*_{*i*}$) — the indices of the club members that should end up in the same group.

翻译中…

Output

翻译中…

For each question print the maximum size of the group in a line. If making such a group is impossible print -1 instead.

翻译中…

Note

翻译中…

In the first query the largest group with members 3 and 5 is ${1, 3, 4, 5}$ where member 3 is the leader.

翻译中…

In the second query member 2 should be the leader so the group will be ${1, 2, 3}$.

翻译中…

In the third query the leader of the group should have age 3 so the only leader can be member 3, who is less responsible than member 2. So making a group is impossible.

翻译中…

The group for the fourth query is the same as first query.

翻译中…

样例

Input #1
5 1
1 5 4 1 2
4 4 3 2 2
4
5 3
2 3
2 5
4 1
Output #1
4
3
-1
4
InputCopy OutputCopy
题目信息
题号
173E
时间限制
2 s
内存限制
256 MB
标签
数据结构
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。