Triple Attack

시간 제한3초메모리 제한2048 MB

요약
정렬된 배열과 q개의 구간 질의가 주어질 때, 선택한 값 중 어떤 세 개도 폭 z 이하의 구간에 들어가지 않도록 하는 각 구간의 최대 안전 부분집합 크기를 구한다.
난이도

어려움10점 중 8점

유형
그리디, 동적 계획법, 이분 탐색, 정렬
정답자
아직 제출이 없습니다

문제

Zeus is analyzing a replay of the fight to understand his opponent's attack patterns. The opponent has a special ability: if they land three attacks on a target within a time frame of zz, their third attack becomes a powerful, boosted attack.

To outplay his opponent, Zeus should not let his opponent trigger their boosted attack. Let Y=y_1,y_2,…,y_mY = \\{y\_1, y\_2, \ldots, y\_m\\} be the multiset of mm timestamps, where each y_iy\_i represents the time when the opponent's attack landed. We call YY to be safe if for every three timestamps y_i,y_j,y_k\\{y\_i, y\_j, y\_k\\} such that 1≤i<j<k≤m1 \le i < j < k \le m, it holds that max⁡(y_i,y_j,y_k)−min⁡(y_i,y_j,y_k)>z\max(y\_i, y\_j, y\_k) - \min(y\_i, y\_j, y\_k) > z, where zz is the duration of the time frame that is given to you as an input.

Zeus has a log of nn timestamps, x_1,x_2,…,x_nx\_1, x\_2, \ldots, x\_n, representing when the opponent's attacks landed. The timestamps are sorted in nondecreasing order of occurrence. In other words, x_i≤x_i+1x\_i \le x\_{i+1} for all 1≤i<n1 \le i < n.

Zeus has qq intervals of interest, denoted as two integers 1≤l≤r≤n1 \le l \le r \le n. For each interval, Zeus wants to find the maximum number of attacks among \[x_l,x_l+1,…,x_r]\[x\_l, x\_{l+1}, \ldots, x\_r] that he could have let through: In other words, Zeus wants to find a maximum size subset of the multiset x_l,x_l+1,…,x_r\\{x\_l, x\_{l+1}, \ldots, x\_r\\} such that the subset is safe.

입력

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤20,0001 \le t \le 20\\,000). The description of the test cases follows.

The first line contains two integers nn and zz (1≤n≤250,0001 \le n \le 250\\,000, 1≤z≤1091 \le z \le 10^9).

The second line contains nn integers x_1,x_2,…,x_nx\_1, x\_2, \ldots, x\_n (1≤x_i≤1091 \le x\_i \le 10^9) --- the timestamps of the opponent's attacks. It is guaranteed that the array xx is sorted, i.e., x_i≤x_i+1x\_i \le x\_{i+1} for all 1≤i<n1 \le i < n.

The third line contains a single integer qq (1≤q≤250,0001 \le q \le 250\\,000).

The next qq lines each contain two integers ll and rr (1≤l≤r≤n1 \le l \le r \le n) --- the endpoints of the interval.

It is guaranteed that the sum of nn over all test cases does not exceed 250,000250\\,000.

It is guaranteed that the sum of qq over all test cases does not exceed 250,000250\\,000.

출력

For each of the qq queries, print a single integer --- the maximum size of a safe subset of attack timestamps in the given interval.

힌트

In the first query of the first test case, we consider the timestamps 1,5,7,8,11,12\\{1, 5, 7, 8, 11, 12\\} with z=10z=10. The subset 1,5,12\\{1, 5, 12\\} is safe because its only triplet satisfies 12−1=11>1012 - 1 = 11 > 10. It's impossible to form a safe subset of size 44, hence the answer to this query is 33.

In the first query of the second test case, we consider the timestamps 1\\{1\\} with z=1z=1. The entire set 1\\{1\\} is safe because there are no triplets. Hence the answer to this query is 11.

In the second query of the second test case, we consider the timestamps 1,1,1,3,3,3\\{1,1,1,3,3,3\\} with z=1z=1.

The subset S=1,1,3,3S = \\{1,1,3,3\\} is safe because:

  • For the triple (i,j,k)=(1,2,3)(i,j,k) = (1,2,3), max⁡(1,1,3)−min⁡(1,1,3)=2>1\max(1,1,3) - \min(1,1,3) = 2 > 1.
  • For the triple (i,j,k)=(1,2,4)(i,j,k) = (1,2,4), max⁡(1,1,3)−min⁡(1,1,3)=2>1\max(1,1,3) - \min(1,1,3) = 2 > 1.
  • For the triple (i,j,k)=(1,3,4)(i,j,k) = (1,3,4), max⁡(1,3,3)−min⁡(1,3,3)=2>1\max(1,3,3) - \min(1,3,3) = 2 > 1.
  • For the triple (i,j,k)=(2,3,4)(i,j,k) = (2,3,4), max⁡(1,3,3)−min⁡(1,3,3)=2>1\max(1,3,3) - \min(1,3,3) = 2 > 1.

It's impossible to form a safe subset of size 55, hence the answer to this query is 44.

예제1

  1. 예제 1

    입력
    3
    6 10
    1 5 7 8 11 12
    6
    1 6
    1 5
    2 6
    1 4
    2 5
    3 6
    6 1
    1 1 1 3 3 3
    2
    3 3
    1 6
    12 15
    4 5 15 24 27 32 36 39 40 46 48 48
    20
    1 12
    1 11
    6 10
    1 8
    8 12
    11 12
    2 9
    3 8
    7 8
    7 10
    4 8
    9 12
    9 10
    2 12
    1 5
    3 12
    4 8
    3 7
    7 12
    10 11
    
    예상 출력
    3
    2
    2
    2
    2
    2
    1
    4
    6
    6
    2
    4
    2
    2
    5
    3
    2
    2
    2
    2
    2
    6
    4
    5
    2
    3
    2
    2