[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 $a$, consisting of $n$ integers $a_1,a_2,…,a_n$. 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 $a_l+a_{l+1}+…+a_r$.
  2. Apply the xor operation with a given number $x$ to each array element on the segment $[l,r]$, that is, execute $a_l=a_l \oplus x,a_{l+1} = a_{l+1} \oplus x,…,a_r=a_r\oplus x$. This operation changes exactly $r-l+1$ array elements.

Expression $x\oplus 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 $n$ $(1≤n≤10^5)$ — the size of the array. The second line contains space-separated integers $a_1,a_2,…,a_n$ $(0≤a_i≤10^6)$ — the original array.

The third line contains integer $m$ $(1≤m≤5·10^4)$ — the number of operations with the array. The $i$-th of the following $m$ lines first contains an integer $t_i$ $(1≤t_i≤2)$ — the type of the i-th query. If $t_i=1$, then this is the query of the sum, if $t_i=2$, then this is the query to change array elements. If the $i$-th operation is of type $1$, then next follow two integers $l_i,r_i$ $(1≤l_i≤r_i≤n)$. If the $i$-th operation is of type $2$, then next follow three integers $l_i,r_i,x_i$ $(1≤l_i≤r_i≤n,1≤x_i≤10^6)$. 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

input output
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

解题思路

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

时间复杂度 $O(20\cdot n\log n)$


Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<bits/stdc++.h>

using namespace std;

//此处省了略 define 的一些东西和读入输出优化

const int N = 100005;

int n, m, opt, ql, qr;
LL a, qx, pow2[N];

struct SegmentTree{
#define lid id<<1
#define rid id<<1|1
#define mid ((tr[num][id].l + tr[num][id].r) >> 1)
#define len(id) (tr[num][id].r - tr[num][id].l + 1)
struct segTree{
int l, r;
LL cnt;
bool rev;
}tr[25][N<<2];
inline markRev(int num, int id){
tr[num][id].rev ^= 1;
tr[num][id].cnt = len(id) - tr[num][id].cnt;
}
inline void pushup(int num, int id){
tr[num][id].cnt = tr[num][lid].cnt + tr[num][rid].cnt;
}
inline void pushdown(int num, int id){
if(tr[num][id].l == tr[num][id].r) return;
if(tr[num][id].rev){
markRev(num, lid);
markRev(num, rid);
tr[num][id].rev = 0;
}
}
void build(int num, int id, int l, int r){
tr[num][id].l = l, tr[num][id].r = r;
if(l == r) return;
build(num, lid, l, mid);
build(num, rid, mid+1, r);
pushup(num, id);
}
LL query(int num, int id, int l, int r){
pushdown(num, id);
if(tr[num][id].l == l && tr[num][id].r == r)
return tr[num][id].cnt;
if(r <= mid) return query(num, lid, l, r);
else if(l > mid) return query(num, rid, l, r);
else return query(num, lid, l, mid) + query(num, rid, mid+1, r);
}
void change(int num, int id, int l, int r){
pushdown(num, id);
if(tr[num][id].l == l && tr[num][id].r == r){
markRev(num, id);
return;
}
if(r <= mid) change(num, lid, l, r);
else if(l > mid) change(num, rid, l, r);
else change(num, lid, l, mid), change(num, rid, mid+1, r);
pushup(num, id);
}
}seg;

int main(){
pow2[0] = 1; fo1(j, 1, 20) pow2[j] = pow2[j-1] * 2;
in(n);
fo1(j, 0, 20) seg.build(j, 1, 1, n);
fo1(i, 1, n){
in(a);
fo1(j, 0, 20)
if((a >> j) & 1 == 1)
seg.change(j, 1, i, i);
}
in(m);
while(m--){
in(opt); in(ql); in(qr);
if(opt == 1){
LL ans = 0;
fo1(j, 0, 20)
ans += seg.query(j, 1, ql, qr) * pow2[j];
outln(ans);
}
else{
in(qx);
fo1(j, 0, 20)
if((qx >> j) & 1 == 1)
seg.change(j, 1, ql, qr);
}
}
return 0;
}