题意:先给出n个数字,然后有q次询问,四种操作AND opn l r表示区间[l,r]里的数字和opn按位做&操作,OR opn l r表示区间[l,r]里的数字和opn按位做|操作,XOR opn l r表示区间[l,r]里的数字和opn按位做^操作,SUM l r表示输出区间[l,r]里所有数字的和。
题解:opn和A[i]的范围是15可以想到二进制只有4位,那么可以想到数位分离,把每一位单独拿出来考虑,那就可以把问题转化为区间内所有数字在某一位全部变为0(AND操作)或变为1(OR操作)或01数目互换(XOR操作),所以建4个线段树维护区间内某个位的0和1的个数,最后按二进制转十进制方法计算总和,这里有两个标记传递,一个是赋值另一个是置换,如果先置换再赋值会被覆盖结果,所以要先传递覆盖再传递置换,同时在传递覆盖时把左右子区间的置换标记清除。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000005;
int n, q, a[N], bit, tree0[N << 2], tree1[N << 2], res[N][4];
int flag1[N << 2], flag2[N << 2], l[N], r[N], v[N];
char op[N][5];
void pushup(int k) {
tree0[k] = tree0[k * 2] + tree0[k * 2 + 1];
tree1[k] = tree1[k * 2] + tree1[k * 2 + 1];
}
void pushdown(int k, int left, int right) {
if (flag1[k] != -1) {
int mid = (left + right) / 2;
flag1[k * 2] = flag1[k * 2 + 1] = flag1[k];
if (flag1[k] == 0) {
tree0[k * 2] = mid - left + 1;
tree0[k * 2 + 1] = right - mid;
tree1[k * 2] = tree1[k * 2 + 1] = 0;
}
else if (flag1[k] == 1) {
tree1[k * 2] = mid - left + 1;
tree1[k * 2 + 1] = right - mid;
tree0[k * 2] = tree0[k * 2 + 1] = 0;
}
flag1[k] = -1;
flag2[k * 2] = flag2[k * 2 + 1] = 0;
}
if (flag2[k]) {
flag2[k * 2] ^= flag2[k];
flag2[k * 2 + 1] ^= flag2[k];
swap(tree0[k * 2], tree1[k * 2]);
swap(tree0[k * 2 + 1], tree1[k * 2 + 1]);
flag2[k] = 0;
}
}
void build(int k, int left, int right, int bit) {
tree0[k] = tree1[k] = flag2[k] = 0;
flag1[k] = -1;
if (left == right) {
if ((a[left] & (1 << bit)))
tree1[k] = 1;
else tree0[k] = 1;
return;
}
int mid = (left + right) / 2;
build(k * 2, left, mid, bit);
build(k * 2 + 1, mid + 1, right, bit);
pushup(k);
}
void modify(int k, int left, int right, int l1, int r1, int flag_1, int flag_2) {
if (l1 <= left && right <= r1) {
if (flag_1 == 0) {
flag1[k] = flag_1;
tree0[k] = right - left + 1;
tree1[k] = flag2[k] = 0;
}
else if (flag_1 == 1) {
flag1[k] = flag_1;
tree1[k] = right - left + 1;
tree0[k] = flag2[k] = 0;
}
else {
flag2[k] ^= flag_2;
swap(tree0[k], tree1[k]);
}
return;
}
pushdown(k, left, right);
int mid = (left + right) / 2;
if (l1 <= mid)
modify(k * 2, left, mid, l1, r1, flag_1, flag_2);
if (r1 > mid)
modify(k * 2 + 1, mid + 1, right, l1, r1, flag_1, flag_2);
pushup(k);
}
int query(int k, int left, int right, int l1, int r1) {
if (l1 <= left && right <= r1)
return tree1[k];
pushdown(k, left, right);
int mid = (left + right) / 2;
if (r1 <= mid)
return query(k * 2, left, mid, l1, r1);
if (l1 > mid)
return query(k * 2 + 1, mid + 1, right, l1, r1);
return query(k * 2, left, mid, l1, mid) + query(k * 2 + 1, mid + 1, right, mid + 1, r1);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= q; i++) {
scanf("%s", op[i]);
if (op[i][0] == 'S')
scanf("%d%d", &l[i], &r[i]);
else
scanf("%d%d%d", &v[i], &l[i], &r[i]);
}
for (int i = 0; i < 4; i++) {
build(1, 1, n, i);
for (int j = 1; j <= q; j++) {
if (op[j][0] == 'S')
res[j][i] = query(1, 1, n, l[j] + 1, r[j] + 1);
else if (op[j][0] == 'A' && !(v[j] & (1 << i)))
modify(1, 1, n, l[j] + 1, r[j] + 1, 0, 0);
else if (op[j][0] == 'O' && (v[j] & (1 << i)))
modify(1, 1, n, l[j] + 1, r[j] + 1, 1, 0);
else if (op[j][0] == 'X' && (v[j] & (1 << i)))
modify(1, 1, n, l[j] + 1, r[j] + 1, -1, 1);
}
}
for (int i = 1; i <= q; i++)
if (op[i][0] == 'S')
printf("%d\n", res[i][0] + res[i][1] * 2 + res[i][2] * 4 + res[i][3] * 8);
}
return 0;
}