CSES - Aalto Competitive Programming 2024 - wk4 - Mon - Results
Submission details
Task:Xor sum
Sender:bielaltes
Submission time:2024-09-23 17:09:27 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.61 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:62:13: warning: unused variable 'type' [-Wunused-variable]
   62 |         int type, aux1, aux2;
      |             ^~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;

const int MOD = 1e9 + 7;

class SegmentTree {
    private:
        vector<int> tree;
        int n;

        void build(const vector<int>& values, int node, int start, int end) {
            if (start == end) {
                tree[node] = values[start];
            } 
            else {
                int mid = (start + end) / 2;
                build(values, 2 * node + 1, start, mid);
                build(values, 2 * node + 2, mid + 1, end);
                tree[node] = tree[2 * node + 1] ^ tree[2 * node + 2];
            }
        }

        int query(int node, int start, int end, int l, int r) {
            if (r < start || end < l) {
                return 0; 
            }
            if (l <= start && end <= r) {
                return tree[node];
            }
            int mid = (start + end) / 2;
            int left_query = query(2 * node + 1, start, mid, l, r);
            int right_query = query(2 * node + 2, mid + 1, end, l, r);
            return left_query ^ right_query;
        }

    public:
        SegmentTree(const vector<int>& values) {
            n = values.size();
            tree.resize(4 * n);
            build(values, 0, 0, n - 1);
        }

        int query(int l, int r) {
            return query(0, 0, n - 1, l, r);
        }
};

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

    vector<int> values(n);
    for (int i = 0; i < n; ++i)
        cin >> values[i];

    SegmentTree st(values);

    for (int i = 0; i < q; ++i) {
        int type, aux1, aux2;
        cin >> aux1 >> aux2;
        cout << st.query(aux1 - 1, aux2 - 1) << 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
...
Truncated