Submission details
Task:Xor sum
Sender:Dereden
Submission time:2025-09-22 16:22:57 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.39 sdetails

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <climits>

using namespace std;

template<class T, class Op>
struct SegTree {
    int n;
    T ID;
    Op op;
    vector<T> st;

    SegTree(int n = 0, T ID = T(), Op op = Op()) { init(n, ID, op); }

    void init(int n_, T ID_, Op op_ = Op()) {
        n = n_;
        ID = ID_;
        op = op_;
        st.assign(2 * n, ID);
    }

    void build(const vector<T> &a) {
        n = (int) a.size();
        st.assign(2 * n, ID);
        for (int i = 0; i < n; i++) st[n + i] = a[i];
        for (int i = n - 1; i > 0; i--) st[i] = op(st[i << 1], st[i << 1 | 1]);
    }

    void set(int i, T v) {
        for (st[i += n] = v; i > 1; i >>= 1)
            st[i >> 1] = op(st[i], st[i ^ 1]);
    }

    T query(int l, int r) const {
        T resL = ID, resR = ID;
        for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) resL = op(resL, st[l++]);
            if (r & 1) resR = op(st[--r], resR);
        }
        return op(resL, resR);
    }
};

struct XorOp {
    long long operator()(long long a, long long b) const {
        return a ^ b;
    }
};

const long long ID = 1l;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // freopen("input.txt", "r", stdin); // TODO: REMOVE THIS YOU STUPID ****

    int n, q;
    cin >> n >> q;
    vector<long long> origValues(n);
    for (int i = 0; i < n; i++) {
        cin >> origValues[i];
    }
    auto tree = SegTree(n, ID, XorOp());
    tree.build(origValues);
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        cout << tree.query(a-1, b-1) << endl;
    }
}

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