CF173E.Camping Groups

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 ii has a responsibility value rir_{i} and an age value aia_{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 kk .

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 xx and club member yy ?". It's possible for xx or yy to be the group leader.

输入格式

The first line contains two integers nn and kk ( 2<=n<=105,0<=k<=1092<=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 r1,r2,...,rnr_{1},r_{2},...,r_{n} ( 1<=ri<=1091<=r_{i}<=10^{9} ) separated by space: rir_{i} denotes the ii -th club member's responsibility. In the same way there are integers a1,a2,...,ana_{1},a_{2},...,a_{n} ( 1<=ai<=1091<=a_{i}<=10^{9} ) in the third line: aia_{i} denotes the ii -th club member's age.

The next line contains an integer qq denoting the number of questions that you should answer ( 1<=q<=1051<=q<=10^{5} ). The next qq lines describe the questions. Each line contains two space-separated integers xix_{i} and yiy_{i} ( 1<=xi,yi<=n,xiyi1<=x_{i},y_{i}<=n,x_{i}≠y_{i} ) — the indices of the club members that should end up in the same group.

输出格式

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

输入输出样例

  • 输入#1

    5 1
    1 5 4 1 2
    4 4 3 2 2
    4
    5 3
    2 3
    2 5
    4 1
    

    输出#1

    4
    3
    -1
    4
    

说明/提示

In the first query the largest group with members 3 and 5 is 1,3,4,5{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{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.

首页