题目链接
对数值的每一位去建立一颗线段树,一个值"≤1e6"的线段树差不多建立21棵线段树就可以了。
题意:有N个数,以及M次操作,
分别是查询区间和;区间每个数"^x"、区间每个数"|x"、区间每个数"&x"。
那么,看成每一个二进制位上面,对应的操作会变成区间翻转、如果该位为1区间强制赋值为1、如果该位为0区间强制赋值为0。
这时候可以处理两个lazy,分别是lazy_turn表示是否需要翻转,还有就是lazy_up表示是否需要区间置数,然后,如果有lazy_up那么lazy_turn在这里就是无效的了。所以我们在pushdown的时候,需要判断一下是否存在lazy_up,以及在赋值的时候,需要判断一下lazy_up的存在性。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M;
int tree[22][maxN<<2] = {0}, lazy_turn[22][maxN<<2], lazy_up[22][maxN<<2] = {0};
inline void pushup(int bit, int rt) { tree[bit][rt] = tree[bit][lsn] + tree[bit][rsn]; }
void Insert(int bit, int rt, int l, int r, int qx, int val)
{
if(l == r)
{
tree[bit][rt] = val;
return;
}
int mid = HalF;
if(qx <= mid) Insert(bit, Lson, qx, val);
else Insert(bit, Rson, qx, val);
pushup(bit, rt);
}
inline void pushdown(int bit, int rt, int l, int r)
{
int mid = HalF;
if(lazy_turn[bit][rt])
{
tree[bit][lsn] = (mid - l + 1) - tree[bit][lsn];
tree[bit][rsn] = (r - mid) - tree[bit][rsn];
lazy_turn[bit][lsn] ^= 1;
lazy_turn[bit][rsn] ^= 1;
lazy_up[bit][lsn] *= -1;
lazy_up[bit][rsn] *= -1;
if(lazy_up[bit][lsn]) lazy_turn[bit][lsn] = 0;
if(lazy_up[bit][rsn]) lazy_turn[bit][rsn] = 0;
lazy_turn[bit][rt] = 0;
}
if(lazy_up[bit][rt])
{
if(lazy_up[bit][rt] == -1)
{
tree[bit][lsn] = 0;
tree[bit][rsn] = 0;
lazy_turn[bit][lsn] = lazy_turn[bit][rsn] = 0;
lazy_up[bit][lsn] = lazy_up[bit][rsn] = -1;
}
else
{
tree[bit][lsn] = mid - l + 1;
tree[bit][rsn] = r - mid;
lazy_turn[bit][lsn] = lazy_turn[bit][rsn] = 0;
lazy_up[bit][lsn] = lazy_up[bit][rsn] = 1;
}
lazy_up[bit][rt] = 0;
}
}
void update(int bit, int rt, int l, int r, int ql, int qr, int val, int op)
{
if(ql <= l && qr >= r)
{
if(op == 2) //翻转
{
tree[bit][rt] = r - l + 1 - tree[bit][rt];
lazy_turn[bit][rt] ^= 1;
lazy_up[bit][rt] *= -1;
if(lazy_up[bit][rt]) lazy_turn[bit][rt] = 0;
}
else if(op == 3) //强制制1
{
tree[bit][rt] = r - l + 1;
lazy_turn[bit][rt] = 0;
lazy_up[bit][rt] = 1;
}
else //强制制0
{
tree[bit][rt] = 0;
lazy_turn[bit][rt] = 0;
lazy_up[bit][rt] = -1;
}
return;
}
int mid = HalF;
//pushdown
pushdown(bit, myself);
if(qr <= mid) update(bit, QL, val, op);
else if(ql > mid) update(bit, QR, val, op);
else { update(bit, QL, val, op); update(bit, QR, val, op); }
pushup(bit, rt);
}
int query(int bit, int rt, int l, int r, int ql, int qr)
{
if(ql <= l && qr >= r) return tree[bit][rt];
//pushdown
pushdown(bit, myself);
int mid = HalF;
if(qr <= mid) return query(bit, QL);
else if(ql > mid) return query(bit, QR);
else return query(bit, QL) + query(bit, QR);
}
int main()
{
scanf("%d", &N);
for(int i=1, val; i<=N; i++)
{
scanf("%d", &val);
for(int j=0; j<=21; j++)
{
if((val >> j) & 1)
{
Insert(j, 1, 1, N, i, 1);
}
}
}
scanf("%d", &M);
int op, l, r, x;
ll ans;
while(M--)
{
scanf("%d%d%d", &op, &l, &r);
if(op == 1)
{
ans = 0;
for(int i=0; i<=21; i++)
{
ans += (ll)query(i, 1, 1, N, l, r) << (ll)i;
}
printf("%lld\n", ans);
}
else
{
scanf("%d", &x);
if(op == 2)
{
for(int i=0; i<=21; i++)
{
if((x >> i) & 1)
{
update(i, 1, 1, N, l, r, 1, 2);
}
}
}
else if(op == 3)
{
for(int i=0; i<=21; i++)
{
if((x >> i) & 1)
{
update(i, 1, 1, N, l, r, 1, 3);
}
}
}
else
{
for(int i=0; i<=21; i++)
{
if(((x >> i) & 1) == 0)
{
update(i, 1, 1, N, l, r, 1, 4);
}
}
}
}
}
return 0;
}