XOR After Range Multiplication Queries II Problem
Description
LeetCode Problem 3655.
You are given an integer array nums of length n and a 2D integer array queries of size q, where queries[i] = [l_i, r_i, k_i, v_i].
For each query, you must apply the following operations in order:
- Set
idx = l_i. - While
idx <= r_i:- Update:
nums[idx] = (nums[idx] * v_i) % (10^9 + 7). - Set
idx += k_i.
- Update:
Return the bitwise XOR of all elements in nums after processing all queries.
Example 1:
Input: nums = [1,1,1], queries = [[0,2,1,4]]
Output: 4
Explanation:
- A single query [0, 2, 1, 4] multiplies every element from index 0 through index 2 by 4.
- The array changes from [1, 1, 1] to [4, 4, 4].
- The XOR of all elements is 4 ^ 4 ^ 4 = 4.
Example 2:
Input: nums = [2,3,1,5,4], queries = [[1,4,2,3],[0,2,1,2]]
Output: 31
Explanation:
- The first query [1, 4, 2, 3] multiplies the elements at indices 1 and 3 by 3, transforming the array to [2, 9, 1, 15, 4].
- The second query [0, 2, 1, 2] multiplies the elements at indices 0, 1, and 2 by 2, resulting in [4, 18, 2, 15, 4].
- Finally, the XOR of all elements is 4 ^ 18 ^ 2 ^ 15 ^ 4 = 31.
Constraints:
1 <= n == nums.length <= 10^51 <= nums[i] <= 10^91 <= q == queries.length <= 10^5queries[i] = [l_i, r_i, k_i, v_i]0 <= l_i <= r_i < n1 <= k_i <= n1 <= v_i <= 10^5
Solution
Approach: Square-Root Decomposition with Difference Products
Let B be about sqrt(n).
Large steps (k > B). Such a query touches at most n / B elements, so apply it directly, multiplying each element by v modulo 10^9 + 7.
Small steps (k <= B). All indices of a query with step k have the same remainder res = l % k, so they sit on one progression res, res + k, res + 2k, .... Number the positions of that progression t = 0, 1, 2, .... The query multiplies positions t1..t2 by v. As in a difference array, record "multiply by v from t1 on" and "multiply by the modular inverse of v from t2 + 1 on" (skipped if t2 + 1 is past the end). The inverse is v^(p-2) modulo the prime p.
After all queries are read, for each (k, res) that has events, sort them by t, merge events at the same t, and walk along the progression keeping the running product cur. Multiply each element on it by cur.
Finally, XOR all elements.
Complexity: O((n + q) * sqrt(n)) time (plus a logarithmic factor for the inverses) and O(n + q) extra space.
import math
class Solution:
def xorAfterQueries(self, nums: List[int], queries: List[List[int]]) -> int:
MOD = 1_000_000_007
n = len(nums)
B = int(math.isqrt(n)) + 1
# events[k][res] = list of (t, v)
events = [[[] for _ in range(k)] for k in range(B + 1)]
for l, r, k, v in queries:
if k > B:
for idx in range(l, r + 1, k):
nums[idx] = nums[idx] * v % MOD
else:
res = l % k
t1 = (l - res) // k
t2 = (r - res) // k
events[k][res].append((t1, v))
if t2 + 1 <= (n - 1 - res) // k:
invv = pow(v, MOD - 2, MOD)
events[k][res].append((t2 + 1, invv))
for k in range(1, B + 1):
for res in range(k):
ev = events[k][res]
if not ev:
continue
ev.sort()
comp = []
for t, val in ev:
if comp and comp[-1][0] == t:
comp[-1] = (t, comp[-1][1] * val % MOD)
else:
comp.append([t, val])
cur = 1
ptr = 0
t = 0
idx = res
while idx < n:
while ptr < len(comp) and comp[ptr][0] == t:
cur = cur * comp[ptr][1] % MOD
ptr += 1
nums[idx] = nums[idx] * cur % MOD
idx += k
t += 1
xr = 0
for x in nums:
xr ^= x
return xr