xor
This commit is contained in:
@@ -10,7 +10,7 @@ vector<ll> construct(vector<ll> &input) {
|
|||||||
output[input.size() + i] = input[i];
|
output[input.size() + i] = input[i];
|
||||||
}
|
}
|
||||||
for (int i = input.size() - 1; i > 0; i--) {
|
for (int i = input.size() - 1; i > 0; i--) {
|
||||||
output[i] = output[2*i] + output[2*i + 1];
|
output[i] = (output[2*i] ^ output[2*i + 1]);
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ void update(int k, ll x, int n, vector<ll> &tree) {
|
|||||||
tree[k] = x;
|
tree[k] = x;
|
||||||
k /= 2;
|
k /= 2;
|
||||||
for (; k >= 1; k /= 2) {
|
for (; k >= 1; k /= 2) {
|
||||||
tree[k] = tree[2*k + 1] + tree[2*k];
|
tree[k] = (tree[2*k + 1] ^ tree[2*k]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,11 +30,11 @@ ll query(int a, int b, int n, vector<ll> &tree) {
|
|||||||
ll s = 0;
|
ll s = 0;
|
||||||
while (a <= b) {
|
while (a <= b) {
|
||||||
if (a % 2 == 1) {
|
if (a % 2 == 1) {
|
||||||
s += tree[a];
|
s ^= tree[a];
|
||||||
a++;
|
a++;
|
||||||
}
|
}
|
||||||
if (b % 2 == 0) {
|
if (b % 2 == 0) {
|
||||||
s += tree[b];
|
s ^= tree[b];
|
||||||
b--;
|
b--;
|
||||||
}
|
}
|
||||||
a /= 2; b /= 2;
|
a /= 2; b /= 2;
|
||||||
|
|||||||
60
CSES - CSES Problem Set/Range_Queries/Range_XOR_Query.cpp
Normal file
60
CSES - CSES Problem Set/Range_Queries/Range_XOR_Query.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
//
|
||||||
|
// Created by darian on 17.06.24.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
void update(int x, ll u, vector<ll> &tree, int n) {
|
||||||
|
x--;
|
||||||
|
x += n;
|
||||||
|
tree[x] = u;
|
||||||
|
x /= 2;
|
||||||
|
for (; x >= 1; x /= 2) {
|
||||||
|
tree[x] = tree[2*x] ^ tree[2*x + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<ll> construct(vector<ll> &input) {
|
||||||
|
int n = input.size();
|
||||||
|
vector<ll> output(2 * n);
|
||||||
|
for (int i = n; i < 2 * n; i++) {
|
||||||
|
output[i] = input[i - n];
|
||||||
|
}
|
||||||
|
for (int i = n - 1; i >= 1; i --) {
|
||||||
|
output[i] = output[2 * i] ^ output[2 * i + 1];
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll query(int a, int b, vector<ll> &tree, int n) {
|
||||||
|
a--; b--;
|
||||||
|
a += n; b += n;
|
||||||
|
ll s = 0;
|
||||||
|
while (a <= b) {
|
||||||
|
if (a % 2 == 1) {
|
||||||
|
s ^= tree[a];
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
if (b % 2 == 0) {
|
||||||
|
s ^= tree[b];
|
||||||
|
b--;
|
||||||
|
}
|
||||||
|
a /= 2; b /= 2;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n; cin >> n;
|
||||||
|
int q; cin >> q;
|
||||||
|
vector<ll> input(n);
|
||||||
|
for (int i{}; i < n; i++) cin >> input[i];
|
||||||
|
auto seg = construct(input);
|
||||||
|
for (int i{}; i < q; i++) {
|
||||||
|
int a, b; cin >> a >> b;
|
||||||
|
cout << query(a, b, seg, n) << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user