-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
244 lines (210 loc) · 6.1 KB
/
main.cpp
File metadata and controls
244 lines (210 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Source: https://leetcode.com/problems/xor-after-range-multiplication-queries-ii
// Title: XOR After Range Multiplication Queries II
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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`.
//
// 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^5`
// - `1 <= nums[i] <= 10^9`
// - `1 <= q == queries.length <= 10^5`
// - `queries[i] = [l_i, r_i, k_i, v_i]`
// - `0 <= l_i <= r_i < n`
// - `1 <= k_i <= n`
// - `1 <= v_i <= 10^5`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cstdint>
#include <vector>
using namespace std;
// Simulation, TLE
// Worst Case O(qn)
class Solution {
constexpr static int64_t modulo = 1e9 + 7;
public:
int xorAfterQueries(vector<int>& nums, const vector<vector<int>>& queries) {
// Queries
for (const auto& query : queries) {
const int l = query[0], r = query[1], k = query[2];
const int64_t v = query[3];
for (int i = l; i <= r; i += k) {
nums[i] = (nums[i] * v) % modulo;
}
}
// XOR
int ans = 0;
for (const int num : nums) {
ans ^= num;
}
return ans;
}
};
// Difference Array
// O(sqrt(n)q + sqrt(n)n) Time, O(sqrt(n)n) Space
//
// Let m = sqrt(n).
//
// For k >= m, there are at most n/m elements to update, just update it directly.
// For k < m, there are too many elements to update, we use lazy update trick.
// We create a difference array for each k <= m.
// For each query, diff_k[l] *= v and diff_k[r+1] *= inv(v)
class Solution2 {
constexpr static int64_t modulo = 1e9 + 7;
int64_t mod(int64_t x) { //
return x % modulo;
}
int64_t pow(int64_t x, int p) { //
int64_t res = 1;
while (p > 0) {
if (p % 2) res = mod(res * x);
x = mod(x * x);
p >>= 1;
}
return res;
}
int64_t inv(int64_t x) { //
return pow(x, modulo - 2);
}
public:
int xorAfterQueries(vector<int>& nums, const vector<vector<int>>& queries) {
const int n = nums.size();
const int m = sqrt(n);
// Prepare difference array
auto diffs = vector<vector<int64_t>>(m, vector<int64_t>(n + m, 1));
// Queries
for (const auto& query : queries) {
const int l = query[0], r = query[1], k = query[2];
const int64_t v = query[3];
if (k >= m) {
for (int i = l; i <= r; i += k) {
nums[i] = mod(nums[i] * v);
}
} else {
int r2 = ((r - l) / k + 1) * k + l; // real ending
diffs[k][l] = mod(diffs[k][l] * v);
diffs[k][r2] = mod(diffs[k][r2] * inv(v));
}
}
// Fill difference array
for (int k = 1; k < m; ++k) {
for (int i = k; i < n; ++i) {
diffs[k][i] = mod(diffs[k][i] * diffs[k][i - k]);
}
}
// XOR
int ans = 0;
for (int i = 0; i < n; ++i) {
int64_t num = nums[i];
for (int k = 1; k < m; ++k) {
num = mod(num * diffs[k][i]);
}
ans ^= static_cast<int>(num);
}
return ans;
}
};
// Difference Array
// O(sqrt(n)q + sqrt(n)n) Time, O(n) Space
//
// Reuse difference array
class Solution3 {
constexpr static int64_t modulo = 1e9 + 7;
int64_t mod(int64_t x) { //
return x % modulo;
}
int64_t pow(int64_t x, int p) { //
int64_t res = 1;
while (p > 0) {
if (p % 2) res = mod(res * x);
x = mod(x * x);
p >>= 1;
}
return res;
}
int64_t inv(int64_t x) { //
return pow(x, modulo - 2);
}
struct DiffQuery {
int l, r;
int64_t v;
};
public:
int xorAfterQueries(vector<int>& nums, const vector<vector<int>>& queries) {
const int n = nums.size();
const int m = sqrt(n);
// Query groups
auto queryGroups = vector<vector<DiffQuery>>(m);
// Queries
for (const auto& query : queries) {
const int l = query[0], r = query[1], k = query[2];
const int64_t v = query[3];
if (k >= m) {
for (int i = l; i <= r; i += k) {
nums[i] = (nums[i] * v) % modulo;
}
} else {
int r2 = ((r - l) / k + 1) * k + l; // real ending
queryGroups[k].push_back(DiffQuery{l, r2, v});
}
}
// Process difference array
auto diffs = vector<int64_t>(n + m);
for (int k = 1; k < m; ++k) {
if (queryGroups[k].empty()) continue;
// Reset diff array
fill(diffs.begin(), diffs.end(), 1);
// Fill diff array
for (const auto [l, r, v] : queryGroups[k]) {
diffs[l] = mod(diffs[l] * v);
diffs[r] = mod(diffs[r] * inv(v));
}
// Aggregate diff array
for (int i = k; i < n; ++i) {
diffs[i] = mod(diffs[i] * diffs[i - k]);
}
// Apply diff array
for (int i = 0; i < n; ++i) {
nums[i] = mod(nums[i] * diffs[i]);
}
}
// XOR
int ans = 0;
for (const int num : nums) {
ans ^= num;
}
return ans;
}
};