이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
void build(vector<int>& segTree, vector<int>& a, int x, int l, int r) {
if (l == r) {
segTree[x] = a[l];
return;
}
int mid = (l+r)/2;
build(segTree, a, 2*x, l, mid);
build(segTree, a, 2*x+1, mid+1, r);
segTree[x] = segTree[2*x] ^ segTree[2*x+1];
}
void update(vector<int>& segTree, vector<int>& a, int x, int l, int r, int i, int v) {
if (l == r) {
segTree[x] = a[l] = v;
return;
}
int mid = (l+r)/2;
if (i <= mid) {
update(segTree, a, 2*x, l, mid, i, v);
} else{
update(segTree, a, 2*x+1, mid+1, r, i, v);
}
segTree[x] = segTree[2*x] ^ segTree[2*x+1];
}
int getRange(vector<int>& segTree, int x, int l, int r, int lb, int rb) {
if (rb <= l or r <= lb) {
return 0;
} else if (lb <= l and r <= rb) {
return segTree[x];
} else {
int mid = (l+r)/2;
int xor1 = getRange(segTree, 2*x, l, mid, lb, rb);
int xor2 = getRange(segTree, 2*x+1, mid+1, r, lb, rb);
return xor1 ^ xor2;
}
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(NULL);
int n, n1, n2, q;
vector<int> a, a1, a2;
vector<int> segTree1, segTree2;
int type, pos, val, l, r;
cin >> n >> q;
a = vector<int>(n);
for (int& i: a) cin >> i;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
a1.push_back(a[i]);
} else {
a2.push_back(a[i]);
}
}
n1 = a1.size();
n2 = a2.size();
segTree1 = vector<int>(4*n1, 0);
segTree2 = vector<int>(4*n2, 0);
build(segTree1, a1, 1, 0, n1-1);
build(segTree2, a2, 1, 0, n2-1);
while (q--) {
cin >> type;
if (type == 1) { // rescan
cin >> pos >> val;
pos--;
a[pos] = val;
int idx = pos/2;
if (pos % 2 == 0) {
update(segTree1, a1, 1, 0, n1-1, idx, val);
} else {
update(segTree2, a2, 1, 0, n2-1, idx, val);
}
}
else { // query
cin >> l >> r;
l--; r--;
int range = r-l+1;
if (l == r) {
cout << a[l] << '\n';
} else if (range % 2 == 0) {
cout << "0\n";
} else {
if (l % 2 == 0) {
cout << getRange(segTree1, 1, 0, n1-1, l/2, r/2) << '\n';
} else {
cout << getRange(segTree2, 1, 0, n2-1, l/2, r/2) << '\n';
}
}
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |