Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations:
Operation 1: AND opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] AND opn (here "AND" is bitwise operation).
Operation 2: OR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] OR opn (here "OR" is bitwise operation).
Operation 3: XOR opn L R
Here opn, L and R are integers.
For L≤i≤R, we do A[i]=A[i] XOR opn (here "XOR" is bitwise operation).
Operation 4: SUM L R
We want to know the result of A[L]+A[L+1]+...+A[R].
Now can you solve this easy problem?
Input
The first line of the input contains an integer T, indicating the number of test cases. (T≤100)
Then T cases, for any case, the first line has two integers n and m (1≤n≤1,000,000, 1≤m≤100,000), indicating the number of elements in A and the number of operations.
Then one line follows n integers A[0], A[1], ..., A[n-1] (0≤A[i]<16,0≤i<n).
Then m lines, each line must be one of the 4 operations above. (0≤opn≤15)
Output
For each test case and for each "SUM" operation, please output the result with a single line.
Sample Input
1
4 4
1 2 4 7
SUM 0 2
XOR 5 0 0
OR 6 0 3
SUM 0 2
Sample Output
7
18
Hint
A = [1 2 4 7]
SUM 0 2, result=1+2+4=7;
XOR 5 0 0, A=[4 2 4 7];
OR 6 0 3, A=[6 6 6 7];
SUM 0 2, result=6+6+6=18.
题意:
给出区间与、或、异或x操作,还有询问区间和。
思路:
因为给定数的范围只有15,对应二进制可以用4位表示
我们给每一位建线段树,这样每次只要更新对应位的答案

线段树节点维护三个重要信息
sum:区间内一的个数
cov:区间是否完全被0、1覆盖 -1 代表为被覆盖
XOR:异或懒标记
对于区间 与 来说只有当数为0时会产生影响,相当于区间置0
对于区间 或、异或 来说只有当数为1时才会产生影响
对于区间 或 来说相当于区间置1
对于区间 异或 来说改变区间的异或标记即可
#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
template<class...Args>
void debug(Args... args) {//Parameter pack
auto tmp = { (cout << args << ' ', 0)... };
cout << "\n";
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pll;
typedef pair<int, int>pii;
const ll N = 5e5 + 5;
const ll INF = 0x7fffffff;
const ll MOD = 1e9 + 7;
int a[N];
struct node {
int l, r;
int sum; //区间内一的个数
int cov; //区间是否完全被0、1覆盖 -1 代表为被覆盖
int XOR; //异或懒标记
int len() { return r - l + 1; }
};
struct Segtree{
node tree[N << 2];
void push_up(int root) {
tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
}
void build(int root, int l, int r,int idx) {
if (l == r) tree[root] = { l,r,(a[l] >> idx) & 1 ,-1,0 };
else {
tree[root].l = l;
tree[root].r = r;
tree[root].cov = -1;
tree[root].XOR = 0;
int mid = l + r >> 1;
build(root << 1, l, mid, idx);
build(root << 1 | 1, mid + 1, r, idx);
push_up(root);
}
}
void push_down(int root) {
if (tree[root].cov != -1) {
tree[root << 1].sum = tree[root].cov * tree[root<<1].len();
tree[root << 1 | 1].sum= tree[root].cov * tree[root << 1 | 1].len();
tree[root << 1].cov= tree[root << 1 | 1].cov = tree[root].cov;
tree[root << 1].XOR = tree[root << 1 | 1].XOR = 0;
tree[root].cov = -1;
}
if (tree[root].XOR != 0) {
tree[root << 1].sum = tree[root << 1].len() - tree[root << 1].sum;
tree[root << 1 | 1].sum = tree[root << 1 | 1].len() - tree[root << 1 | 1].sum;
tree[root << 1].XOR ^= 1;
tree[root << 1 | 1].XOR ^= 1;
tree[root].XOR = 0;
}
}
void modify(int root, int l, int r, int op) {
if (l <= tree[root].l && tree[root].r <= r) {
if (op == 1) {
tree[root].cov = 0;
tree[root].XOR = 0;
tree[root].sum = 0;
}
if (op == 2) {
tree[root].cov = 1;
tree[root].XOR = 0;
tree[root].sum = tree[root].len();
}
if (op == 3){
tree[root].XOR ^= 1;
tree[root].sum = tree[root].len()-tree[root].sum;
}
}
else {
push_down(root);
int mid = tree[root].l + tree[root].r >> 1;
if (l <= mid)modify(root << 1, l, r, op);
if (r > mid)modify(root << 1 | 1, l, r, op);
push_up(root);
}
}
int query(int root, int l, int r) {
if (l <= tree[root].l && tree[root].r <= r)return tree[root].sum;
push_down(root);
int mid = tree[root].l + tree[root].r >> 1;
if (r <= mid)return query(root << 1, l, r);
else if (l > mid)return query(root << 1 | 1, l, r);
else {
int left = query(root << 1, l, r);
int right = query(root << 1 | 1, l, r);
int ans = left + right;
return ans;
}
}
}seg[4];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < 4; i++) seg[i].build(1, 1, n, i);//每个数的第i位为单独拿出来建立一棵树
for (int i = 0; i < m; i++) {
string s;
cin >> s;
if (s == "AND") {
int x, l, r;
cin >> x >> l >> r;
l++, r++;
for (int i = 0; i < 4; i++) {
//对于区间 与 来说只有当数为0时会产生影响
if (!((x >> i) & 1))seg[i].modify(1, l, r, 1);
}
}
else if(s=="OR"||s=="XOR") {
int x, l, r;
cin >> x >> l >> r;
l++, r++;
int temp = s == "OR" ? 2 : 3;
for (int i = 0; i < 4; i++) {
//对于区间 或、异或 来说只有当数为1时才会产生影响
if ((x >> i) & 1)seg[i].modify(1, l, r, temp);
}
}
else {
int l, r;
cin >> l >> r;
l++, r++;
ll ans = 0;
for (ll i = 0; i < 4; i++) {
ll t = seg[i].query(1, l, r);//查询第i个线段树
ans += (1 << i) * t;
}
cout << ans << "\n";
}
}
}
return 0;
}
博客探讨了FZU2105问题,涉及区间与、或、异或操作。由于数值范围限制,可以使用四位二进制表示。通过为每位建立线段树,更新对应位的答案。线段树节点存储区间一的个数(sum)、是否完全被0或1覆盖(cov)和异或懒标记(XOR)。区间与操作对应区间置0,区间或操作对应区间置1,区间异或操作改变异或标记。
705

被折叠的 条评论
为什么被折叠?



