Length of arithmetic progressions

3.6

25 votes
Advanced Data Structures, Data Structures, Segment Trees
Problem

You are given an integer array A of size N. You are also given Q queries. In each query, you are given three integers L, R, and D respectively.

You are required to determine the length of the largest contiguous segment in the indices range [L,R] of A that forms an arithmetic progression with a common difference of D.

Note: The segment whose length is 1 always forms an arithmetic progression of a common difference D.

Input format

  • First line: Two space-separated integers N and Q respectively
  • Second line: N space-separated integers denoting elements of A
  • Next Q lines: Three space-separated integers L, R, and D (1LRN) respectively

Output format

Print Q lines representing the answer such as the ith line denotes the answer for the ith query.

Constraints

1N200000

1Ai200000

200000D200000

 

Sample Input
5 2
1 2 3 5 5
1 5 1
4 4 3
Sample Output
3
1
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

For the first query, [1,2,3] forms an AP with difference 1.

For the second query, [5] forms an AP.

Editor Image

?