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 <= 105
1 <= nums[i] <= 109
1 <= q == queries.length <= 105
queries[i] = [li, ri, ki, vi]
0 <= li <= ri < n
1 <= ki <= n
1 <= vi <= 105
Solutions
Solution 1
Thinking
With \(n,q\le 10^5\), simulating every query as in I becomes quadratic on small strides. Queries with \(k>\sqrt{n}\) stay few and may still multiply in place; small \(k\) must be batched.
A query with \(k\le B\) lies on the arithmetic progression of residue \(l\bmod k\). Multiply by \(v\) at \(t=(i-\textit{res})/k\) and by the modular inverse just after the right end, i.e. a difference on that progression.
For each \((k,\textit{res})\), merge factors at the same \(t\), scan the progression, and apply the prefix product to \(\textit{nums}\). XOR the array at the end.
classSolution:defxorAfterQueries(self,nums:List[int],queries:List[List[int]])->int:MOD=1_000_000_007n=len(nums)B=int(math.isqrt(n))+1# events[k][res] = list of (t, v)events=[[[]for_inrange(k)]forkinrange(B+1)]forl,r,k,vinqueries:ifk>B:foridxinrange(l,r+1,k):nums[idx]=nums[idx]*v%MODelse:res=l%kt1=(l-res)//kt2=(r-res)//kevents[k][res].append((t1,v))ift2+1<=(n-1-res)//k:invv=pow(v,MOD-2,MOD)events[k][res].append((t2+1,invv))forkinrange(1,B+1):forresinrange(k):ev=events[k][res]ifnotev:continueev.sort()comp=[]fort,valinev:ifcompandcomp[-1][0]==t:comp[-1]=(t,comp[-1][1]*val%MOD)else:comp.append([t,val])cur=1ptr=0t=0idx=reswhileidx<n:whileptr<len(comp)andcomp[ptr][0]==t:cur=cur*comp[ptr][1]%MODptr+=1nums[idx]=nums[idx]*cur%MODidx+=kt+=1xr=0forxinnums:xr^=xreturnxr