- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 54 lines of Python from the credited upstream file xor-after-range-multiplication-queries-i.py.
- The implementation visibly relies on sequence storage, hash lookup.
- No explicit loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 4import collections5 6 78class Solution(object):9 def xorAfterQueries(self, nums, queries):10 """11 :type nums: List[int]12 :type queries: List[List[int]]13 :rtype: int14 """15 MOD = 10**9+716 def inv(x):17 return pow(x, MOD-2, MOD)18 19 block_size = int(len(nums)**0.5)+120 diffs = collections.defaultdict(lambda: [1]*len(nums))21 for l, r, k, v in queries:22 if k <= block_size:23 diffs[k][l] = (diffs[k][l]*v)%MOD24 r += k-(r-l)%k25 if r < len(nums):26 diffs[k][r] = (diffs[k][r]*inv(v))%MOD27 else:28 for i in xrange(l, r+1, k):29 nums[i] = (nums[i]*v)%MOD30 for k, diff in diffs.iteritems():31 for i in xrange(len(diff)):32 if i-k >= 0:33 diff[i] = (diff[i]*diff[i-k])%MOD34 nums[i] = (nums[i]*diff[i])%MOD35 return reduce(lambda accu, x: accu^x, nums, 0)36 37 38394041class Solution2(object):42 def xorAfterQueries(self, nums, queries):43 """44 :type nums: List[int]45 :type queries: List[List[int]]46 :rtype: int47 """48 MOD = 10**9+749 50 for l, r, k, v in queries:51 for i in xrange(l, r+1, k):52 nums[i] = (nums[i]*v)%MOD53 return reduce(lambda accu, x: accu^x, nums, 0)54