Here's a Python version of the code, with comments explaining each part to help you understand it better:
import sys
import threading
import collections
def main():
sys.setrecursionlimit(1 << 25) # Increase recursion limit
n, m = map(int, sys.stdin.readline().split())
U = (1 << n) - 1 # Maximum value based on n
sz = 1 # Size of the segment tree
tag = {} # Tag at each node of the segment tree
ls = {} # Left child of each node
rs = {} # Right child of each node
fa = {} # Union-find parent array
e = collections.defaultdict(list) # Edges to connect in union-find at each time step
l = [0] * (m + 2) # Left values for commands
r = [0] * (m + 2) # Right values for commands
op = [0] * (m + 2) # Operation type: 1 for 'add', 0 for 'block'
ans = [0] * (m + 2) # Answers to 'add' queries
tag[1] = m + 1 # Initialize root tag to a value greater than any command index
# Union-find 'find' function with path compression
def find(x):
if fa[x] != x:
fa[x] = find(fa[x])
return fa[x]
# Function to check if a node is a leaf in the segment tree
def lea(x):
return ls.get(x, 0) == 0 and rs.get(x, 0) == 0
# Function to push down tags to children in the segment tree
def pd(x):
nonlocal sz
if ls.get(x, 0) == 0:
sz += 1
ls[x] = sz
tag[sz] = tag[x]
if rs.get(x, 0) == 0:
sz += 1
rs[x] = sz
tag[sz] = tag[x]
tag[x] = 0 # Clear the tag after pushing down
# Function to update the segment tree with 'block' commands
def I(x, l_range, r_range, a, b, t):
if a <= l_range and r_range <= b:
tag[x] = t # Set the tag to the command index
return
pd(x) # Push down the tag to children
mid = (l_range + r_range) >> 1
if a <= mid:
I(ls[x], l_range, mid, a, b, t)
if b > mid:
I(rs[x], mid + 1, r_range, a, b, t)
# Depth-first search to collect edges to be connected in union-find
def dfs(x, y):
if lea(x) and lea(y):
z = min(tag[x], tag[y]) # Earliest time when both nodes are unblocked
e[z].append((x, y)) # Add edge to be connected at time z
return
if lea(x):
dfs(x, ls.get(y, 0))
dfs(x, rs.get(y, 0))
return
if lea(y):
dfs(ls.get(x, 0), y)
dfs(rs.get(x, 0), y)
return
dfs(ls.get(x, 0), ls.get(y, 0))
dfs(rs.get(x, 0), rs.get(y, 0))
# Query function to find the leaf node corresponding to a position
def Q(x, l_range, r_range, p):
if lea(x):
return x
mid = (l_range + r_range) >> 1
if p <= mid:
return Q(ls[x], l_range, mid, p)
else:
return Q(rs[x], mid + 1, r_range, p)
# Function to connect edges at time z in union-find
def ins(z):
for u, v in e[z]:
p1 = find(u)
p2 = find(v)
if p1 != p2:
fa[p1] = p2 # Union the two sets
# Read all commands
input_lines = []
for _ in range(m):
input_lines.append(sys.stdin.readline())
# Process each command
for idx, s_line in enumerate(input_lines):
tokens = s_line.strip().split()
s = tokens[0]
l[idx + 1] = int(tokens[1])
r[idx + 1] = int(tokens[2])
i = idx + 1
op[i] = 1 if s[0] == 'a' else 0 # 'add' command
if s[0] == 'b': # 'block' command
I(1, 0, U, l[i], r[i], i) # Update the segment tree
# Initialize union-find parents to themselves
for x in range(1, sz + 1):
fa[x] = x
# Build the edges to connect by traversing the segment tree
for x in range(1, sz + 1):
if not lea(x):
dfs(ls[x], rs[x])
ins(m + 1) # Process initial edges before any 'block'
# Process commands in reverse to answer queries correctly
for i in range(m, 0, -1):
ins(i) # Connect edges that become available at time i
if op[i]:
u = Q(1, 0, U, l[i]) # Find leaf node for position l[i]
v = Q(1, 0, U, r[i]) # Find leaf node for position r[i]
ans[i] = 1 if find(u) == find(v) else 0 # Check if connected
# Output the answers to 'add' queries
for i in range(1, m + 1):
if op[i]:
print(ans[i])
# Use threading to avoid exceeding the recursion depth limit
threading.Thread(target=main).start()
Explanation:
By converting the code to Python and adding comments, you should now have a clearer understanding of how each part works and how they fit together. This code maintains the logic and structure of the original C++ code while adapting it to Python's syntax and features.