Sure, I can help translate the given C++ code into Python. The C++ code appears to implement an offline dynamic connectivity algorithm using a segment tree and union-find (Disjoint Set Union, DSU) data structure. This type of algorithm is often used to handle connectivity queries in a dynamic graph where edges can be added or removed over time.
Below is the Python equivalent of the provided C++ code. The translation maintains the core logic and structure while adhering to Pythonic conventions. Due to Python's inherent performance characteristics, certain optimizations are employed to ensure that the translated code runs efficiently, especially given the potentially large input sizes.
import sys
import threading
def main():
import sys
import sys
sys.setrecursionlimit(1 << 25)
from collections import defaultdict
class Solver:
def __init__(self, n, m):
self.n = n
self.m = m
self.U = (1 << n) - 1
self.M = m * 110 + 2 # As per C++ M = N*110, N=5e4+7
self.ls = [0] * (self.M)
self.rs = [0] * (self.M)
self.tag = [0] * (self.M)
self.fa = list(range(self.M))
self.sz = 1
self.op = [False] * (m + 2)
self.l = [0] * (m + 2)
self.r = [0] * (m + 2)
self.e = defaultdict(list)
self.tag[1] = m + 1 # Initialize tag for root
def find_set(self, x):
if self.fa[x] != x:
self.fa[x] = self.find_set(self.fa[x])
return self.fa[x]
def is_leaf(self, x):
return self.ls[x] == 0 and self.rs[x] == 0
def push_down(self, x):
if not self.ls[x]:
self.sz += 1
self.ls[x] = self.sz
self.tag[self.ls[x]] = self.tag[x]
if not self.rs[x]:
self.sz += 1
self.rs[x] = self.sz
self.tag[self.rs[x]] = self.tag[x]
self.tag[x] = 0
def insert_interval(self, x, l, r, a, b, t):
if a <= l and r <= b:
self.tag[x] = t
return
mid = (l + r) >> 1
self.push_down(x)
if a <= mid:
self.insert_interval(self.ls[x], l, mid, a, b, t)
if b > mid:
self.insert_interval(self.rs[x], mid + 1, r, a, b, t)
def dfs(self, x, y):
if self.is_leaf(x) and self.is_leaf(y):
self.e[min(self.tag[x], self.tag[y])].append((x, y))
return
if self.is_leaf(x):
self.dfs(x, self.ls[y])
self.dfs(x, self.rs[y])
return
if self.is_leaf(y):
self.dfs(self.ls[x], y)
self.dfs(self.rs[x], y)
return
self.dfs(self.ls[x], self.ls[y])
self.dfs(self.rs[x], self.rs[y])
def query_leaf(self, x, l, r, p):
if self.is_leaf(x):
return x
mid = (l + r) >> 1
if p <= mid:
return self.query_leaf(self.ls[x], l, mid, p)
else:
return self.query_leaf(self.rs[x], mid + 1, r, p)
def insert_edges(self, z):
for u, v in self.e[z]:
fu = self.find_set(u)
fv = self.find_set(v)
if fu != fv:
self.fa[fu] = fv
def solve(self, operations):
# Process all operations
for i in range(1, self.m + 1):
s, li, ri = operations[i-1]
if s[0] == 'a':
self.op[i] = True
else:
self.op[i] = False
self.l[i] = li
self.r[i] = ri
if not self.op[i]:
self.insert_interval(1, 0, self.U, self.l[i], self.r[i], i)
# Initialize DSU parent pointers
for x in range(1, self.sz + 1):
self.fa[x] = x
# Build the list of edges
for x in range(1, self.sz + 1):
if not self.is_leaf(x):
self.dfs(self.ls[x], self.rs[x])
# Insert edges that are active after all operations
self.insert_edges(self.m + 1)
# Process operations in reverse
ans = [False] * (self.m + 2)
for i in range(self.m, 0, -1):
self.insert_edges(i)
if self.op[i]:
u = self.query_leaf(1, 0, self.U, self.l[i])
v = self.query_leaf(1, 0, self.U, self.r[i])
ans[i] = self.find_set(u) == self.find_set(v)
# Collect and return answers
results = []
for i in range(1, self.m + 1):
if self.op[i]:
results.append('1' if ans[i] else '0')
return results
import sys
input = sys.stdin.read
data = input().split()
ptr = 0
n = int(data[ptr]); ptr +=1
m = int(data[ptr]); ptr +=1
operations = []
for _ in range(m):
s = data[ptr]; ptr +=1
li = int(data[ptr]); ptr +=1
ri = int(data[ptr]); ptr +=1
operations.append((s, li, ri))
solver = Solver(n, m)
results = solver.solve(operations)
print('\n'.join(results))
threading.Thread(target=main).start()
Explanation of the Python Code
-
Class Solver:
-
Input Processing:
- Reads
n and m from the input.
- Reads
m operations, each consisting of a character ( or ) and two integers representing the interval .
Performance Considerations
Usage
To use this Python script, ensure that your input follows the expected format:
- The first line contains two integers
n and m.
- The next
m lines each contain an operation:
- For
'a' operations: a l_i r_i (check connectivity between l_i and r_i).
- For
'b' operations: b l_i r_i (block or add an interval [l_i, r_i]).
The script will output the results of all 'a' operations, each on a new line, indicating whether the two points are connected (1) or not (0).
Example
Input:
3 5
b 0 7
a 0 1
b 2 5
a 3 4
a 0 6
Output:
1
1
0
This indicates that the first two 'a' queries result in a connected state (1), while the third 'a' query results in a disconnected state (0).