Skip to content

Instantly share code, notes, and snippets.

@modos
Created April 26, 2023 05:14
Show Gist options
  • Save modos/17c7fe9e6f0f8b80a9783042808ea7bd to your computer and use it in GitHub Desktop.
Save modos/17c7fe9e6f0f8b80a9783042808ea7bd to your computer and use it in GitHub Desktop.
کمینه در بازه
n, q = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
LOG = 18
power = [0] * LOG
lg = [0] * (n + 1)
for i in range(2, n+1):
lg[i] = lg[int(i/2)] + 1
for i in range(LOG):
if i > 0 :
power[i] = 2 * power[i - 1]
else:
power[i] = 1
rmq = [[0] * n for i in range(LOG)]
def build():
for j in range(LOG):
for i in range(n):
if j == 0:
rmq[j][i] = A[i]
continue
if i + power[j] <= n:
rmq[j][i] = min(rmq[j - 1][i], rmq[j - 1][i + power[j - 1]])
#print(j, i, rmq[j][i])
def get(l, r) :
len = r - l
p = lg[len]
return min(rmq[p][l], rmq[p][r - power[p]])
build()
for i in range(q) :
l, r = list(map(int, input().split(" ")))
print(get(l, r + 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment