[Codeforces 242.E] XOR on Segment(线段树)

E. XOR on Segment

time limit per test:4 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

You’ve got an array aa, consisting of nn integers a1,a2,…,an. You are allowed to perform two operations on this array:

  1. Calculate the sum of current array elements on the segment [l,r], that is, count value al+al+1+…+ar.
  2. Apply the xor operation with a given number xx to each array element on the segment [l,r][l,r], that is, execute al=al⊕x,al+1=al+1⊕x,…,ar=ar⊕x. This operation changes exactly r−l+1 array elements.

Expression x⊕y means applying bitwise xor operation to numbers x and y. The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as “^”, in Pascal — as “xor”.

You’ve got a list of m operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get.

Input

The first line contains integer nn (1≤n≤105) — the size of the array. The second line contains space-separated integers a1,a2,…,an (0≤ai≤106)— the original array.

The third line contains integer mm (1≤m≤5⋅104) — the number of operations with the array. The ii-th of the following mm lines first contains an integer titi (1≤ti≤2) — the type of the i-th query. If ti=1, then this is the query of the sum, if ti=2, then this is the query to change array elements. If the ii-th operation is of type 1, then next follow two integers li,ri (1≤li≤ri≤n). If the i-th operation is of type 2, then next follow three integers li,ri,xi (1≤li≤ri≤n,1≤xi≤106). The numbers on the lines are separated by single spaces.

Output

For each query of type 1 print in a single line the sum of numbers on the given segment. Print the answers to the queries in the order in which the queries go in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.

Examples

inputoutput
5
4 10 3 13 7
8
1 2 4
2 1 3 3
1 2 4
1 3 3
2 2 5 5
1 1 5
2 1 2 10
1 2 3
26
22
0
34
11
6
4 7 4 0 7 3
5
2 2 3 8
1 1 5
2 3 5 1
2 4 5 6
1 2 3
38
28

题意

给出一段序列,每次有两个操作

1 l r 查询[l,r]的和

2 l r x将[l,r]异或x

解题思路

由于 x⩽106,即 x 不超过20位,可以每一位都开一个线段树,总共开二十个线段树。异或操作即是区间取反(0 变成 1,1变成 0),询问操作转化为求区间1的个数。
如图,以样例1为例,竖着一个虚线框即开成一个线段树。

 

#include <bits/stdc++.h>
#define FOR(i,s,t) for(int i=(s);i<=(t);i++)
#define ROF(i,s,t) for(int i=(s);i>=(t);i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 1e5 + 6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int readInt(){
    int x=0;
    bool sign=false;
    char c=getchar();
    while(!isdigit(c)){
        sign=c=='-';
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return sign?-x:x;
}
ll readLong(){
    ll x=0;
    bool sign=false;
    char c=getchar();
    while(!isdigit(c)){
        sign=c=='-';
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return sign?-x:x;
}
string readString(){
    string s;
    char c=getchar();
    while(isspace(c)){
        c=getchar();
    }
    while(!isspace(c)){
        s+=c;
        c=getchar();
    }
    return s;
}

ll pow2[maxn];
int n;
int tr[maxn <<2][21];
int tag[maxn << 2][21];
int a[maxn];
void gather(int pos, int p){
    tr[p][pos] = tr[p << 1][pos] + tr[p << 1 | 1][pos];
}

void pushdown(int pos, int p, int l, int r){
    if (l == r) return;
    if (tag[p][pos]){
        tag[p<<1][pos] ^= 1;
        tag[p<<1|1][pos] ^= 1;
        int mid = (l + r) / 2;
        tr[p<<1][pos] = (mid - l + 1) - tr[p<<1][pos];
        tr[p<<1|1][pos] =(r - mid) - tr[p<<1|1][pos];
        tag[p][pos] = 0;
    }
}

void build(int pos, int p, int l, int r ){
    if (l > r) return ;
    if (l == r){
        tr[p][pos] = ((a[l] >> pos) & 1);
        return ;
    }
    int mid = (l + r) / 2;
    build(pos, p << 1, l , mid);
    build(pos, p << 1 | 1, mid + 1, r);
    gather(pos, p);
}

int query(int pos ,int p, int l , int r, int ql, int qr){
    if (l > r) return 0;
    if (l > qr || r < ql) return 0;
    pushdown(pos, p, l, r);
    if (l >= ql && r <= qr){
        return tr[p][pos];
    }
    int mid = (l + r) / 2;
    return query(pos, p << 1, l, mid, ql, qr) + query(pos, p << 1 | 1, mid + 1, r, ql, qr);
    gather(pos, p);
}

void update(int pos, int p, int l, int r, int ql, int qr){
    if (l > r) return ;
    if(l >qr || r <ql) return ;
    pushdown(pos, p, l, r);
    if (l >= ql && r <= qr){
        tag[p][pos] ^= 1;
        tr[p][pos] = (r - l + 1) - tr[p][pos];
        //cout << l << " " << r << " " << tr[p][pos] << endl;
        return;
    }
    int mid = (l + r) / 2;
    update(pos, p << 1, l , mid, ql, qr);
    update(pos, p << 1 | 1, mid + 1, r, ql,qr);
    gather(pos , p);
}

int main(){
    pow2[0] = 1;
    FOR(i,1,20) pow2[i] = pow2[i-1] * 2ll;
    n = readInt();
    FOR(i,1,n) a[i] = readInt();
    FOR(i,0,20){
        build(i,1,1,n);
    }
    int m = readInt();
    while (m--){
        int ope = readInt();
        int ql = readInt();
        int qr = readInt();
        if (ope == 1){
            ll ans = 0;
            FOR(i,0,20){
                ans += 1ll * query(i, 1, 1, n, ql, qr) * pow2[i];
            }
            printf("%lld\n", ans);
        }
        else {
            int qx = readInt();
            FOR(i, 0, 20){
                if(((qx >> i) & 1)) update(i, 1, 1, n, ql, qr);
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值