Submission details
Task:Xor sum
Sender:tjaa
Submission time:2025-09-22 16:24:40 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.56 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:43:20: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   43 |     for(int i=0; i < nc; i++) {
      |                  ~~^~~~
input/code.cpp:44:14: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   44 |         if(i < n) cin >> ar[i];
      |            ~~^~~
input/code.cpp:50:19: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
   50 |     for(int i=0; i<q; i++) {
      |                  ~^~

Code

#include <bits/stdc++.h>
using namespace std;

const unsigned int N = 2e5;
const unsigned int N2 = bit_ceil(N);
int ar[N2+1];
int tree[2*N2+1];

void bfs(int k, int qi, int qe) {
    if(qe-qi <= 1) tree[k] = ar[qi];
    else {
        int a=2*k;
        int b=a+1;
        int mid = (qe+qi)/2;
        bfs(a, qi, mid);
        bfs(b, mid, qe);
        tree[k]=tree[a]^tree[b];
    }
}

int xorsum(int a, int b) {
    int res = 0;

    while (a<b) {
        if(a % 2) {
            res = res^tree[a];
            a++;
        };
        if(b % 2) {
            b--;
            res = res^tree[b];
        };
        a /= 2; b /= 2;
    }
    return res;
}

int main () {
    unsigned int n, q; cin >> n >> q;

    unsigned int nc = bit_ceil(n);

    for(int i=0; i < nc; i++) {
        if(i < n) cin >> ar[i];
        else ar[i] = INT_MAX;
    }

    bfs(1, 0, nc);

    for(int i=0; i<q; i++) {
        int a, b;
        cin >> a >> b;
        cout << xorsum(nc+a-1, nc+b) << '\n';
    }


    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
...
Truncated