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