Advertisement
pankovamg

SparseTable

Feb 24th, 2023
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. def get(a, b):
  2. i = lg[b - a + 1]
  3. return min(st[i][a], st[i][b -(2**i) + 1])
  4.  
  5. n = int(input())
  6.  
  7. a = list(map(int, input().split()))
  8.  
  9. lg = [0] * (n + 1)
  10. for i in range(2, n+1):
  11. lg[i] = lg[i//2] + 1
  12.  
  13.  
  14. st = [[0] * n for i in range(lg[n] + 1)]
  15.  
  16. st[0] = a[:]
  17.  
  18. for i in range(1, lg[n]+1):
  19. x = 2**(i-1)
  20. for j in range(0, n-x):
  21. st[i][j] = min(st[i-1][j], st[i-1][j+x])
  22.  
  23.  
  24. m = int(input())
  25. for j in range(m):
  26. a, b = map(int, input().split())
  27. print(get(a-1, b-1), end = ' ')
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement