반응형
https://leetcode.com/problems/xor-queries-of-a-subarray
class Solution {
public:
vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
vector<int> x(arr.size() + 1);
x[0] = 0;
for(int i=0; i<arr.size(); i++) {
x[i + 1] = x[i] ^ arr[i];
}
vector<int> res;
for(vector<int>& query : queries) {
int left = query[0];
int right = query[1];
res.push_back(x[right + 1] ^ x[left]);
}
return res;
}
};
prefix xor을 미리 전처리해두고, 각 쿼리를 빠르게 구할 수 있었습니다.
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 2364. Count Number of Bad Pairs (0) | 2025.02.09 |
---|---|
LeetCode 1550. Three Consecutive Odds (0) | 2024.07.06 |
LeetCode 350. Intersection of Two Arrays II (0) | 2024.07.06 |
LeetCode 2181. Merge Nodes in Between Zeros (0) | 2024.07.06 |
LeetCode 2582. Pass the Pillow (0) | 2024.07.06 |