Codeforces Round #149 (Div. 2) E. XOR on Segment --二进制按位建树

Codeforces Round #149 (Div. 2) E. XOR on Segment

#include 
#include  
using namespace std;
template<class...Args>
void debug(Args... args) {//Parameter pack 
    auto tmp = { (cout << args << ' ', 0)... };
    cout << "\n";
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pll;
typedef pair<int, int>pii;
const ll N = 1e5 + 5;
const ll INF = 0x7fffffff;
const ll MOD = 1e9 + 7;

int a[N];
struct node {
    int l, r;
    int sum;  //区间内一的个数 
    int XOR;  //异或懒标记 
    int len() { return r - l + 1; }
};
struct Segtree {
    node tree[N << 2];
    void push_up(int root) {
        tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
    }
    void build(int root, int l, int r, int idx) {
        if (l == r) tree[root] = { l,r,(a[l] >> idx) & 1 ,0 };
        else {
            tree[root].l = l;
            tree[root].r = r;
            tree[root].XOR = 0;
            int mid = l + r >> 1;
            build(root << 1, l, mid, idx);
            build(root << 1 | 1, mid + 1, r, idx);
            push_up(root);
        }
    }
    void push_down(int root) {
        if (tree[root].XOR != 0) {
            tree[root << 1].sum = tree[root << 1].len() - tree[root << 1].sum;
            tree[root << 1 | 1].sum = tree[root << 1 | 1].len() - tree[root << 1 | 1].sum;
            tree[root << 1].XOR ^= 1;
            tree[root << 1 | 1].XOR ^= 1;
            tree[root].XOR = 0;
        }
    }
    void modify(int root, int l, int r) {
        if (l <= tree[root].l && tree[root].r <= r) {
            tree[root].XOR ^= 1;
            tree[root].sum = tree[root].len() - tree[root].sum;
        }
        else {
            push_down(root);
            int mid = tree[root].l + tree[root].r >> 1;
            if (l <= mid)modify(root << 1, l, r);
            if (r > mid)modify(root << 1 | 1, l, r);
            push_up(root);
        }
    }
    int query(int root, int l, int r) {
        if (l <= tree[root].l && tree[root].r <= r)return tree[root].sum;
        push_down(root);
        int mid = tree[root].l + tree[root].r >> 1;
        if (r <= mid)return query(root << 1, l, r);
        else if (l > mid)return query(root << 1 | 1, l, r);
        else {
            int left = query(root << 1, l, r);
            int right = query(root << 1 | 1, l, r);
            int ans = left + right;
            return ans;
        }
    }
}tr[20];

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 0; i < 20; i++) tr[i].build(1, 1, n, i);
    int q;
    cin >> q;
    while (q--) {
        int op;
        cin >> op;
        if (op == 1) {
            int l, r;
            cin >> l >> r;
            ll ans = 0;
            for (int i = 0; i < 20; i++) {
                ll t = tr[i].query(1, l, r);
                ans += (1 << i) * t;
            }
            cout << ans << "\n";
        }
        else {
            int l, r, num;
            cin >> l >> r >> num;
            for (int i = 0; i < 20; i++) {
                if ((num >> i) & 1)tr[i].modify(1, l, r);
            }
        }
    }

    return 0;
}

你可能感兴趣的:(数据结构,线段树,数据结构,算法)