- 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
- 58 lines of C++ from the credited upstream file xor-after-range-multiplication-queries-ii.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 5 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 45class Solution {6public:7 int xorAfterQueries(vector<int>& nums, vector<vector<int>>& queries) {8 static const int MOD = 1e9 + 7;9 const auto& powmod = [](uint32_t a, uint32_t b, uint32_t mod) {10 a %= mod;11 uint64_t result = 1;12 while (b) {13 if (b & 1) {14 result = result * a % mod;15 }16 a = uint64_t(a) * a % mod;17 b >>= 1;18 }19 return result;20 };21 22 const auto& inv = [&](int x, int p) {23 return powmod(x, p - 2, p);24 };25 26 const int block_size = sqrt(size(nums)) + 1;27 unordered_map<int, vector<int64_t>> diffs;28 for (const auto& q : queries) {29 int64_t l = q[0], r = q[1], k = q[2], v = q[3];30 if (k <= block_size) {31 if (!diffs.count(k)) {32 diffs[k].assign(size(nums), 1);33 }34 diffs[k][l] = (diffs[k][l] * v) % MOD;35 r += k - (r - l) % k;36 if (r < size(nums)) {37 diffs[k][r] = (diffs[k][r] * inv(v, MOD)) % MOD;38 }39 } else {40 for (int i = l; i <= r; i += k) {41 nums[i] = (nums[i] * v) % MOD;42 }43 }44 }45 for (auto& [k, diff] : diffs) {46 for (int i = 0; i < size(diff); ++i) {47 if (i - k >= 0) {48 diff[i] = (diff[i] * diff[i - k]) % MOD;49 }50 nums[i] = (nums[i] * diff[i]) % MOD;51 }52 }53 return accumulate(cbegin(nums), cend(nums), 0, [](const auto& accu, const auto& x) {54 return accu ^ x;55 });56 }57};58