CF173E.Camping Groups
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
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 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.
一个俱乐部希望组织其成员去露营。为了更好地组织此次活动,俱乐部负责人决定将成员划分为若干个小组。
俱乐部成员 i 具有一个责任值 ri 和一个年龄值 ai。一个小组是非空的成员子集,其中恰好有一名成员担任小组组长。组长必须是该小组中责任值最高者之一(即其责任值不小于该小组中任何其他成员的责任值),且其与该小组中任意其他成员的年龄绝对差值不超过 k。
部分俱乐部成员是朋友,希望被分到同一小组;他们还希望所在小组尽可能大。现在你需要编写一个程序,回答一系列形如“包含俱乐部成员 x 和俱乐部成员 y 的小组的最大可能大小是多少?”的问题。x 或 y 均可担任该小组的组长。
输入格式
The first line contains two integers n and k (2 ≤ n ≤ 105, 0 ≤ k ≤ 109) — 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 ≤ 109) 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 ≤ 109) 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 ≤ 105). 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.
第一行包含两个整数 n 和 k(2≤n≤105,0≤k≤109)—— 分别表示俱乐部成员人数以及每组的年龄限制。
第二行包含 n 个用空格分隔的整数 r1,r2,…,rn(1≤ri≤109):ri 表示第 i 位俱乐部成员的责任值。类似地,第三行包含 n 个用空格分隔的整数 a1,a2,…,an(1≤ai≤109):ai 表示第 i 位俱乐部成员的年龄。
第四行包含一个整数 q,表示你需要回答的问题数量(1≤q≤105)。接下来的 q 行描述各个问题,每行包含两个用空格分隔的整数 xi 和 yi(1≤xi,yi≤n,且 xi=yi)—— 表示编号为 xi 和 yi 的两位俱乐部成员必须被分到同一组。
输出格式
For each question print the maximum size of the group in a line. If making such a group is impossible print -1 instead.
对于每个问题,在一行中输出该组的最大大小。如果无法组成这样的组,则输出 -1。
输入输出样例
输入#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} 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.
在第一次查询中,包含成员 3 和 5 的最大组为 {1,3,4,5},其中成员 3 是组长。
在第二次查询中,成员 2 应为组长,因此该组为 {1,2,3}。
在第三次查询中,该组的组长年龄应为 3,因此唯一可能的组长是成员 3;但成员 3 的责任心低于成员 2。因此无法组成这样的组。
第四次查询所对应的组与第一次查询相同。
输入解题思路,AI测评打分。不知道怎么写?