Sina to Irfan an array of
n
n is the number of gifts given. Ilya asks Irfan
Q
Q performs operations on the array. Each operation is in one of the following 4 modes:
0
,
l
,
r
:
∑
a
i
(
l
≤
i
≤
r
)
0,l,r:∑a
i
(l≤i≤r)
1
,
l
,
r
,
v
:
a
i
:
a
i
⊕
v
(
l
≤
i
≤
r
)
1, l, r, v: a
i
:=a
i
⊕v(l≤i≤r)
2
,
l
,
r
,
v
:
a
i
:
a
i
and
v
(
l
≤
i
≤
r
)
2, l, r, v: a
i
:=a
i
and v(l≤i≤r)
3
,
l
,
r
,
v
:
a
i
:
a
i
or
v
(
l
≤
i
≤
r
)
3, l, r, v: a
i
:=a
i
or v(l≤i≤r)
Irfan must apply the mentioned operations on the presentation members and for each type of operation
0
0 Tell Ilya the sum of the numbers within the requested range. But since Irfan is tired, he asks you to answer Ilya's questions.
input
In the first line, in order of number
n
n which is the length of the array and in the next line
n
n is a number that
i
Iamin Shan
a
i
a
i
is coming In the next line
Q
Q comes and in
Q
Q Next line in each line comes an operation from the 4 mentioned operations.
1
≤
n
,
Q
≤
100
,
000
1≤n, Q≤100,000
1
≤
l
≤
r
≤
n
1≤l≤r≤n
1
≤
a
i
,
v
≤
1000
,
000
1≤a
i
,v≤1000,000
output
For each operation of the type
0
0 Output the sum of numbers within the requested range.
Example
Sample entry 1
4
1 2 4 8
5
1 2 3 8
3 1 1 8
0 1 3
2 1 4 8
0 2 4
Plain text
Sample output 1
31
24
Plain text
The result of each operation:
a
:
[
1
,
10
,
12
,
8
]
a:[1,10,12,8]
a
:
[
9
,
10
,
12
,
8
]
a:[9,10,12,8]
p
r
i
n
t
:
9
+
10
+
12
31
print: 9+10+12=31
a
:
[
8
,
8
,
8
,
8
]
a:[8,8,8,8]
p
r
i
n
t
:
8
+
8
+
8
24
print: 8+8+8=24 c++
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a C++ implementation for the problem you described. The solution utilizes segment trees with lazy propagation for each bit (up to 20 bits, as the maximum value of a_i and v is (10^6)). This approach efficiently handles range updates and queries.
Solution Overview
Bitwise Segmentation:
Since the operations involve bitwise manipulations (AND, OR, XOR), we handle each bit separately. Specifically, for each bit position from 0 to 19, we maintain a separate segment tree.
Segment Tree Structure for Each Bit:
Sum: Stores the count of set bits in a given range.
Lazy Propagation: Helps in deferring updates to child nodes to optimize performance.
Operations Handling:
Type 0 (Sum Query): For each bit, query the count of set bits in the range and multiply by (2^{\text{bit position}}) to get the contribution to the total sum.
Type 1 (XOR): For each bit set in v, flip the bits in the specified range.
Type 2 (AND): For each bit not set in v, set the bits to 0 in the specified range.
Type 3 (OR): For each bit set in v, set the bits to 1 in the specified range.
Efficiency:
By handling each bit separately and using lazy propagation, the solution ensures that each operation runs in (O(\log n)) time per bit, resulting in an overall efficient solution.
C++ Implementation
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 100005;
// Maximum number of bits
const int BITS = 20;
// Segment trees for each bit
ll tree_bits[BITS][4 * MAX];
int lazy_bits[BITS][4 * MAX];
// Original array
ll a_arr[MAX];
// Number of elements
int n;
// Function to build the segment tree for a specific bit
void build(int bit, int node, int l, int r){
lazy_bits[bit][node] = 0;
if(l == r){
tree_bits[bit][node] = ( (a_arr[l] >> bit) & 1LL ) ? 1 : 0;
return;
}
int mid = (l + r) / 2;
build(bit, node*2, l, mid);
build(bit, node*2+1, mid+1, r);
tree_bits[bit][node] = tree_bits[bit][node*2] + tree_bits[bit][node*2+1];
}
// Function to propagate the lazy updates to children
void push_down(int bit, int node, int l, int r){
if(lazy_bits[bit][node] == 0) return;
int mid = (l + r) / 2;
// Apply the pending operation to the left child
if(lazy_bits[bit][node] == 1){
// Set to 1
tree_bits[bit][node*2] = (mid - l +1);
tree_bits[bit][node*2+1] = (r - mid);
lazy_bits[bit][node*2] = 1;
lazy_bits[bit][node*2+1] = 1;
}
else if(lazy_bits[bit][node] ==2){
// Set to 0
tree_bits[bit][node*2] = 0;
tree_bits[bit][node*2+1] = 0;
lazy_bits[bit][node*2] =2;
lazy_bits[bit][node*2+1] =2;
}
else if(lazy_bits[bit][node] ==3){
// Flip
tree_bits[bit][node*2] = (mid - l +1) - tree_bits[bit][node*2];
tree_bits[bit][node*2+1] = (r - mid) - tree_bits[bit][node*2+1];
// Update lazy flags
// For left child
if(lazy_bits[bit][node*2] ==0){
lazy_bits[bit][node*2] =3;
}
else if(lazy_bits[bit][node*2]==1){
lazy_bits[bit][node*2] =2;
}
else if(lazy_bits[bit][node*2]==2){
lazy_bits[bit][node*2] =1;
}
else if(lazy_bits[bit][node*2]==3){
lazy_bits[bit][node*2] =0;
}
// For right child
if(lazy_bits[bit][node*2+1] ==0){
lazy_bits[bit][node*2+1] =3;
}
else if(lazy_bits[bit][node*2+1]==1){
lazy_bits[bit][node*2+1] =2;
}
else if(lazy_bits[bit][node*2+1]==2){
lazy_bits[bit][node*2+1] =1;
}
else if(lazy_bits[bit][node*2+1]==3){
lazy_bits[bit][node*2+1] =0;
}
}
// Clear the current node's lazy flag
lazy_bits[bit][node] =0;
}
// Function to update the segment tree for a specific bit
// type: 1 - set to1, 2 - set to0, 3 - flip
void update_tree(int bit, int node, int l, int r, int ul, int ur, int type){
if(r < ul || l > ur) return;
if(ul <= l && r <= ur){
if(type ==1){
tree_bits[bit][node] = (r - l +1);
lazy_bits[bit][node] =1;
}
else if(type ==2){
tree_bits[bit][node] =0;
lazy_bits[bit][node] =2;
}
else if(type ==3){
tree_bits[bit][node] = (r - l +1) - tree_bits[bit][node];
if(lazy_bits[bit][node] ==0){
lazy_bits[bit][node] =3;
}
else if(lazy_bits[bit][node]==1){
lazy_bits[bit][node] =2;
}
else if(lazy_bits[bit][node]==2){
lazy_bits[bit][node] =1;
}
else if(lazy_bits[bit][node]==3){
lazy_bits[bit][node] =0;
}
}
return;
}
push_down(bit, node, l, r);
int mid = (l + r) /2;
update_tree(bit, node*2, l, mid, ul, ur, type);
update_tree(bit, node*2+1, mid+1, r, ul, ur, type);
tree_bits[bit][node] = tree_bits[bit][node*2] + tree_bits[bit][node*2+1];
}
// Function to query the number of set bits in a range for a specific bit
ll query_tree(int bit, int node, int l, int r, int ql, int qr){
if(r < ql || l > qr) return 0;
if(ql <= l && r <= qr){
return tree_bits[bit][node];
}
push_down(bit, node, l, r);
int mid = (l + r) /2;
return query_tree(bit, node*2, l, mid, ql, qr) + query_tree(bit, node*2+1, mid+1, r, ql, qr);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// Input n
cin >> n;
// Input array
for(int i=1;i<=n;i++) cin >> a_arr[i];
// Build segment trees for each bit
for(int bit=0; bit<BITS; bit++) {
build(bit, 1,1,n);
}
// Input Q
int Q;
cin >> Q;
while(Q--){
int type;
cin >> type;
if(type ==0){
int l, r;
cin >> l >> r;
ll total =0;
for(int bit=0; bit<BITS; bit++){
ll cnt = query_tree(bit,1,1,n, l, r);
total += (cnt * (1LL << bit));
}
cout << total << "\n";
}
else{
int l, r;
ll v;
cin >> l >> r >> v;
if(type ==1){
// XOR: flip bits where v has bit set
for(int bit=0; bit<BITS; bit++){
if( (v >> bit) &1LL ){
update_tree(bit,1,1,n, l, r, 3);
}
}
}
else if(type ==2){
// AND: set bits to 0 where v has bit 0
for(int bit=0; bit<BITS; bit++){
if( ((v >> bit) &1LL) ==0 ){
update_tree(bit,1,1,n, l, r, 2);
}
}
}
else if(type ==3){
// OR: set bits to1 where v has bit set
for(int bit=0; bit<BITS; bit++){
if( (v >> bit) &1LL ){
update_tree(bit,1,1,n, l, r,1);
}
}
}
}
}
}
Explanation of the Code
Initialization:
The array a_arr stores the initial values.
For each bit (0 to 19), a separate segment tree is built using the build function. Each segment tree keeps track of the number of elements with that specific bit set in any given range.
Building the Segment Trees:
The build function initializes the segment trees for each bit based on the initial array values.
Lazy Propagation:
The push_down function ensures that any pending updates are propagated to the child nodes before performing further operations. This is crucial for maintaining the correctness of the segment trees during range updates.
Updating the Segment Trees:
The update_tree function handles the range updates. Depending on the operation type (set to 1, set to 0, or flip), it updates the relevant bits in the specified range.
Querying the Segment Trees:
The query_tree function retrieves the count of set bits in a given range for a specific bit. This count is then used to calculate the contribution of that bit to the total sum.
Handling Operations:
Type 0: Calculates the sum of elements in the specified range by aggregating the contributions from each bit.
Type 1: Performs XOR operations by flipping the relevant bits.
Type 2: Performs AND operations by setting bits to 0 where necessary.
Type 3: Performs OR operations by setting bits to 1 where necessary.
Sample Input & Output
Input:
code
4
1 2 4 8
5
1 2 3 8
3 1 1 8
0 1 3
2 1 4 8
0 2 4
Output:
code
31
24
Explanation:
After the first two operations, the array becomes [9, 10, 12, 8].
The first sum query (0 1 3) results in 9 + 10 + 12 = 31.
After the fourth operation (2 1 4 8), the array becomes [8, 8, 8, 8].
The second sum query (0 2 4) results in 8 + 8 + 8 = 24.
Conclusion
This solution effectively handles large input sizes by leveraging bitwise operations and segment trees with lazy propagation. It ensures that each operation is executed efficiently, making it suitable for the given constraints.