MAX = int(1e5) # Max size of array
tree = [0] * (4 * MAX) # Segment tree
lazy = [0] * (4 * MAX) # Lazy array to propagate updates
# Function to build the tree
def build(node, start, end):
if start == end:
# Leaf node will have a single element
tree[node] = 0
else:
mid = (start + end) // 2
# Recur for the 2 children
build(2 * node, start, mid)
build(2 * node + 1, mid + 1, end)
# Internal node will have the minimum of both of its children
tree[node] = min(tree[2 * node], tree[2 * node + 1])
# Function to update a node
def update(node, start, end, l, r, val):
if lazy[node] != 0:
# This node needs to be updated
tree[node] += lazy[node] # Update it
if start != end:
lazy[node * 2] += lazy[node] # Mark child as lazy
lazy[node * 2 + 1] += lazy[node] # Mark child as lazy
lazy[node] = 0 # Reset it
if start > end or start > r or end < l:
return # Current segment is not within range [l, r]
if start >= l and end <= r:
# Segment is fully within range
tree[node] += val
if start != end:
# Not leaf node
lazy[node * 2] += val
lazy[node * 2 + 1] += val
return
mid = (start + end) // 2
update(node * 2, start, mid, l, r, val) # Updating left child
update(node * 2 + 1, mid + 1, end, l, r, val) # Updating right child
tree[node] = min(tree[node * 2], tree[node * 2 + 1]) # Updating root with min value
# Function to query the tree
def query(node, start, end, l, r):
if start > end or start > r or end < l:
return MAX # Out of range
if lazy[node] != 0:
# This node needs to be updated
tree[node] += lazy[node] # Update it
if start != end:
lazy[node * 2] += lazy[node] # Mark child as lazy
lazy[node * 2 + 1] += lazy[node] # Mark child as lazy
lazy[node] = 0 # Reset it
if start >= l and end <= r: # Current segment is totally within range [l, r]
return tree[node]
mid = (start + end) // 2
p1 = query(node * 2, start, mid, l, r) # Query left child
p2 = query(node * 2 + 1, mid + 1, end, l, r) # Query right child
return min(p1, p2)
# Main function
def main():
n = 5
build(1, 0, n - 1)
# Define the queries
queries = [
[1, 0, 3, 3],
[2, 1, 2],
[1, 1, 4, 4],
[2, 1, 3],
[2, 1, 4],
[2, 3, 5]
]
for q in queries:
type = q[0]
if type == 1:
l, r, x = q[1], q[2], q[3]
update(1, 0, n - 1, l, r - 1, x) # Update the array
elif type == 2:
l, r = q[1], q[2]
ans = query(1, 0, n - 1, l, r - 1) # Query the array
print(ans)
main()