#3653
Medium Algorithms Xor after range multiplication queries i
Array Divide and Conquer Simulation
73.9% acceptance
Feb 25, 2026
31
13
You are given an integer array nums of length n and a 2D integer array queries of size q, where queries[i] = [li, ri, ki, vi].
For each query, you must apply the following operations in order:
Set idx = li.
While idx <= ri:
Update: nums[idx] = (nums[idx] * vi) % (109 + 7)
Set idx += ki.
Return the bitwise XOR of all elements in nums after processing all queries.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn xor_after_queries(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {
const MOD: i64 = 1_000_000_007;
let mut nums: Vec<i64> = nums.iter().map(|&x| x as i64).collect();
for q in &queries {
let (l, r, k, v) = (q[0] as usize, q[1] as usize, q[2] as usize, q[3] as i64);
let mut idx = l;
while idx <= r {
nums[idx] = (nums[idx] * v) % MOD;
idx += k;
}
}
nums.iter().fold(0i32, |acc, &x| acc ^ x as i32)
}
}