$re = '/\\\\\[(.*?)\\\\\]/ms';
$str = 'Here\'s the explanation of prefix sum variations along with their corresponding Python code:
### 1. **Basic Prefix Sum (1D Array)**
- **Description**: This is the most common variation where you compute a running total of an array. The prefix sum at index `i` is the sum of all elements from index `0` to `i`.
- **Use Case**: Range sum queries in constant time.
- **Formula**:
\\[
\\text{prefix}[i] = \\text{prefix}[i-1] + \\text{arr}[i]
\\]
- **Query**: To get the sum of elements between two indices `l` and `r`, the formula becomes:
\\[
\\text{sum}(l, r) = \\text{prefix}[r] - \\text{prefix}[l-1]
\\]
**Python Code**:
```python
def prefix_sum(arr):
prefix = [0] * len(arr)
prefix[0] = arr[0]
for i in range(1, len(arr)):
prefix[i] = prefix[i - 1] + arr[i]
return prefix
def range_sum(prefix, l, r):
if l == 0:
return prefix[r]
return prefix[r] - prefix[l - 1]
# Example usage:
arr = [1, 2, 3, 4, 5]
prefix = prefix_sum(arr)
result = range_sum(prefix, 1, 3) # Sum of arr[1] to arr[3] -> 2+3+4 = 9
print(result)
```
### 2. **2D Prefix Sum (Matrix)**
- **Description**: This variation extends the prefix sum to two dimensions, commonly used for range queries in a 2D matrix.
- **Use Case**: Efficiently calculate the sum of elements in any rectangular submatrix.
- **Formula**:
$$[
\\text{prefix}[i][j] = \\text{arr}[i][j] + \\text{prefix}[i-1][j] + \\text{prefix}[i][j-1] - \\text{prefix}[i-1][j-1]
]$$
- **Query**: To get the sum of a submatrix from `(r1, c1)` to `(r2, c2)`, use:
\\[
\\text{sum}(r1, c1, r2, c2) = \\text{prefix}[r2][c2] - \\text{prefix}[r1-1][c2] - \\text{prefix}[r2][c1-1] + \\text{prefix}[r1-1][c1-1]
\\]
**Python Code**:
```python
def prefix_sum_2d(matrix):
rows, cols = len(matrix), len(matrix[0])
prefix = [[0] * cols for _ in range(rows)]
prefix[0][0] = matrix[0][0]
# First row
for j in range(1, cols):
prefix[0][j] = prefix[0][j - 1] + matrix[0][j]
# First column
for i in range(1, rows):
prefix[i][0] = prefix[i - 1][0] + matrix[i][0]
# Rest of the matrix
for i in range(1, rows):
for j in range(1, cols):
prefix[i][j] = matrix[i][j] + prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1]
return prefix
def range_sum_2d(prefix, r1, c1, r2, c2):
total = prefix[r2][c2]
if r1 > 0:
total -= prefix[r1 - 1][c2]
if c1 > 0:
total -= prefix[r2][c1 - 1]
if r1 > 0 and c1 > 0:
total += prefix[r1 - 1][c1 - 1]
return total
# Example usage:
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
prefix = prefix_sum_2d(matrix)
result = range_sum_2d(prefix, 1, 1, 2, 2) # Sum of submatrix from (1, 1) to (2, 2) -> 5+6+8+9 = 28
print(result)
```
### 3. **Prefix XOR (1D Array)**
- **Description**: Instead of summing elements, this variation computes the prefix XOR.
- **Use Case**: Useful for solving XOR-related problems efficiently, like range XOR queries.
- **Formula**:
\\[
\\text{prefix\\_xor}[i] = \\text{prefix\\_xor}[i-1] \\oplus \\text{arr}[i]
\\]
- **Query**: To get the XOR of elements between two indices `l` and `r`, the formula becomes:
\\[
\\text{xor}(l, r) = \\text{prefix\\_xor}[r] \\oplus \\text{prefix\\_xor}[l-1]
\\]
**Python Code**:
```python
def prefix_xor(arr):
prefix = [0] * len(arr)
prefix[0] = arr[0]
for i in range(1, len(arr)):
prefix[i] = prefix[i - 1] ^ arr[i]
return prefix
def range_xor(prefix, l, r):
if l == 0:
return prefix[r]
return prefix[r] ^ prefix[l - 1]
# Example usage:
arr = [1, 2, 3, 4, 5]
prefix = prefix_xor(arr)
result = range_xor(prefix, 1, 3) # XOR of arr[1] to arr[3] -> 2 ^ 3 ^ 4 = 5
print(result)
```
### 4. **Prefix Product (1D Array)**
- **Description**: Computes the running product of elements in an array.
- **Use Case**: Useful when handling multiplicative operations over ranges.
- **Formula**:
\\[
\\text{prefix}[i] = \\text{prefix}[i-1] \\times \\text{arr}[i]
\\]
- **Query**: To get the product of elements between two indices `l` and `r`, the formula becomes:
\\[
\\text{product}(l, r) = \\frac{\\text{prefix}[r]}{\\text{prefix}[l-1]}
\\]
**Python Code**:
```python
def prefix_product(arr):
prefix = [1] * len(arr)
prefix[0] = arr[0]
for i in range(1, len(arr)):
prefix[i] = prefix[i - 1] * arr[i]
return prefix
def range_product(prefix, l, r):
if l == 0:
return prefix[r]
return prefix[r] // prefix[l - 1]
# Example usage:
arr = [1, 2, 3, 4, 5]
prefix = prefix_product(arr)
result = range_product(prefix, 1, 3) # Product of arr[1] to arr[3] -> 2*3*4 = 24
print(result)
```
Each variation of the prefix sum adapts the basic idea to different mathematical operations like summing, XORing, or multiplying, providing efficient ways to handle range queries for different types of problems.';
$subst = "$$\1$$";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php