| Task: | Xor sum |
| Sender: | aalto26dm_010 |
| Submission time: | 2026-09-21 16:30:20 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.60 s | details |
Code
#include <iostream>
#include <vector>
#include <ranges>
#include <math.h>
using namespace std;
int parent(int node) {
return node / 2;
}
int left(int node) {
return 2 * node;
}
int right(int node) {
return 2 * node + 1;
}
int getXorSum(int a, int b, vector<int>& arr) {
int n = arr.size() / 2;
a += n; b += n;
int sumVal = 0;
while (a <= b) {
if (a%2 == 1) {
sumVal = sumVal ^ arr[a];
a++;
}
if (b%2 == 0) {
sumVal = sumVal ^ arr[b];
b--;
}
a = parent(a);
b = parent(b);
}
return sumVal;
}
int main() {
int n, q;
cin >> n >> q;
int elems = 1 << (int)ceil(log2(n));
vector<int> tree(2*elems);
for (int i=0; i<n; i++) {
cin >> tree[i + elems];
}
for (int i=n; i<elems; i++) {
tree[i + elems] = 0;
}
for (int i=elems-1; i>0; i--) {
tree[i] = tree[left(i)] ^ tree[right(i)];
}
int a, b;
for (int i=0; i<q; i++) {
cin >> a >> b;
cout << getXorSum(a-1, b-1, tree) << endl;
}
return 0;
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 8 36 7 6 4 6 2 9 4 8 1 1 1 2 1 3 ... |
| correct output |
|---|
| 7 1 5 3 1 ... |
| user output |
|---|
| 7 1 5 3 1 ... |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 200000 200000 921726510 307633388 992247073 ... |
| correct output |
|---|
| 834756431 130379787 403037296 308618218 784778243 ... |
| user output |
|---|
| 834756431 130379787 403037296 308618218 784778243 ... |
