【題解】AtCoder ABC 185F – Range Xor Query

【題目敘述】https://atcoder.jp/contests/abc185/tasks/abc185_f
【解題想法】BIT

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

int bit[300005];

void update(int x, int d){
    while (x <= 300000){
        bit[x] ^= d;
        x += x & (-x);
    }
}
int query(int x){
    int ret = 0;
    while (x){
        ret ^= bit[x];
        x -= x & (-x);
    }
    return ret;
}
int main(){
    int n, q;
    cin >> n >> q;
    for (int i = 1, a; i <= n; i++){
        cin >> a;
        update(i, a);
    }
    for (int i = 1, t, x, y; i <= q; i++){
        cin >> t >> x >> y;
        if (t == 1) update(x, y);
        else cout << (query(y)^query(x-1)) << "\n";
    }
}

分享本文 Share with friends