-
Notifications
You must be signed in to change notification settings - Fork 0
/
10868_1.py
46 lines (36 loc) · 1009 Bytes
/
10868_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from sys import stdin
input = stdin.readline
def solve():
N, M = map(int, input().split())
arr = [int(input()) for _ in range(N)]
def init_tree():
tree = [0] * (2 * N)
tree[N:2*N] = arr
for i in range(N - 1, 0, -1):
l, r = i << 1, (i << 1) | 1
tree[i] = tree[l] if tree[l] < tree[r] else tree[r]
return tree
inf = float('inf')
def query(start, end):
nonlocal tree
ret = inf
start += N
end += N
while start < end:
if start & 1:
if tree[start] < ret:
ret = tree[start]
start += 1
if end & 1:
end -= 1
if tree[end] < ret:
ret = tree[end]
start >>= 1
end >>= 1
return ret
tree = init_tree()
for _ in range(M):
a, b = map(int, input().split())
print(query(a-1, b))
if __name__ == '__main__':
solve()