Trilogy xor
unknown
c_cpp
a year ago
799 B
6
Indexable
int applyMask(int val, int y) {
return (val & (1 << (y - 1)));
}
int findEnd(const vector<int>& a, int x, int n, int mask) {
int end = x;
while (end < n && applyMask(a[end], mask)) {
end++;
}
return end;
}
int solve(vector<int> a, vector<vector<int>> q) {
int totalUpdates = 0;
int n = a.size();
for (const auto& query : q) {
int x = query[0] - 1;
int y = query[1];
int z = query[2];
int mask = 1 << (y - 1);
int end = findEnd(a, x, n, mask);
int sizeToUpdate = end - x;
if (sizeToUpdate > 0) {
for (int i = x; i < end; ++i) {
a[i] ^= z;
}
totalUpdates += sizeToUpdate;
}
}
return totalUpdates;
}Editor is loading...
Leave a Comment