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:
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.
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.
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.
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 |
给出一段序列,每次有两个操作
1 l r 查询[l,r]的和
2 l r x将[l,r]异或x
由于 x⩽106,即 x 不超过20位,可以每一位都开一个线段树,总共开二十个线段树。异或操作即是区间取反(0 变成 1,1变成 0),询问操作转化为求区间1的个数。
如图,以样例1为例,竖着一个虚线框即开成一个线段树。
#include
#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 && 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;
}