Segment tree query for maximum quantity

I gave an array Aof n integers and a Qrequest in the form X Dfor each request I have to find the maximum element in the basement[Ax , A(x-D),A(x-2D)..]

For instance:

A = [1,2,3,4,5,6,17,8]
we have query 7 2
Sub Array [17,5,3,1] Ans=17

How can we solve this with time complexity better than O(Q*N), since no index is updated, can it be deleted offline using any technique

I do not think it Square Decompositionwill work here.

+4
source share
1 answer

D , sqrt(N). x, x - d, x - 2 * d,... sqrt(N) . , O(sqrt(N)) .

D <= sqrt(N). sqrt(N) D. . D f[i] = max(a[i], f[i - d]) i ( ). (X, D), D = d, f[X].

O((N + Q) * sqrt(N)). .

+1

Source: https://habr.com/ru/post/1661862/


All Articles