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.
To solve this problem efficiently, we'll use a Segment Tree with Lazy Propagation. Given the constraints where both n (the size of the array) and Q (the number of operations) can be up to 100,000, a simple brute-force approach would be too slow. The segment tree allows us to perform range queries and range updates in logarithmic time, making it suitable for this problem.
Overview of the Approach:
Segment Tree Structure:
Counts: For each segment tree node, maintain a count of set bits for each bit position (0 to 19) within that range.
Lazy Flags: Use three separate bitmask flags to handle pending operations:
flip: Indicates which bits need to be toggled (XOR).
set: Indicates which bits need to be set to 1 (OR).
reset: Indicates which bits need to be set to 0 (AND).
Handling Operations:
Type 0 (Sum Query): Calculate the sum of elements in the range [l, r] by aggregating the contributions of each bit.
Type 1 (XOR Operation): Toggle the specified bits within the range.
Type 2 (AND Operation): Clear the specified bits within the range.
Type 3 (OR Operation): Set the specified bits within the range.
Lazy Propagation:
To efficiently handle multiple updates on overlapping segments, use lazy propagation. When an update is applied to a segment, it's marked as "lazy" without immediately updating the children. The actual updates are deferred until necessary.
Bitwise Operations:
Each operation is handled bit by bit. For each bit position, determine how the operation affects the count of set bits and update the counts accordingly.
Implementation Details:
Below is the C++ implementation based on the above approach. The code includes detailed comments to help you understand each part of the process.
cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Maximum number of bits needed (since a_i and v <= 1e6, 20 bits)
const int MAX_BITS = 20;
// Segment Tree Node
struct Node {
int l, r;
int count[MAX_BITS]; // count[j]: number of set bits at bit j in [l, r]
unsigned int flip; // flip mask
unsigned int set_flag; // set to 1 mask
unsigned int reset_flag; // set to 0 mask
} tree_node;
// Maximum number of nodes in segment tree
const int MAXN = 100005;
Node tree[4 * MAXN];
// Original array
int a[100005];
int n, Q;
// Build the segment tree
void build(int idx, int l, int r){
tree[idx].l = l;
tree[idx].r = r;
tree[idx].flip = 0;
tree[idx].set_flag = 0;
tree[idx].reset_flag = 0;
if(l == r){
for(int j=0; j<MAX_BITS; j++){
if(a[l] & (1 << j)){
tree[idx].count[j] = 1;
}
else{
tree[idx].count[j] = 0;
}
}
return;
}
int mid = (l + r) / 2;
build(idx*2, l, mid);
build(idx*2+1, mid+1, r);
for(int j=0; j<MAX_BITS; j++){
tree[idx].count[j] = tree[idx*2].count[j] + tree[idx*2+1].count[j];
}
}
// Push down the lazy flags to children
void push_down(int idx){
if(idx >= 4 * MAXN){
return;
}
// If no pending operations, return
if(tree[idx].flip == 0 && tree[idx].set_flag == 0 && tree[idx].reset_flag == 0){
return;
}
// Push to left child
for(int child = idx*2; child <= idx*2+1; child++){
// First handle reset flags
if(tree[idx].reset_flag){
// Apply reset: set to 0
tree[child].reset_flag |= tree[idx].reset_flag;
tree[child].set_flag &= ~(tree[idx].reset_flag); // remove set if any
tree[child].flip &= ~(tree[idx].reset_flag); // remove flip if any
for(int j=0; j<MAX_BITS; j++) {
if(tree[idx].reset_flag & (1 << j)){
tree[child].count[j] = 0;
}
}
}
// Then handle set flags
if(tree[idx].set_flag){
// Apply set: set to 1
tree[child].set_flag |= tree[idx].set_flag;
tree[child].reset_flag &= ~(tree[idx].set_flag); // remove reset if any
tree[child].flip &= ~(tree[idx].set_flag); // remove flip if any
for(int j=0; j<MAX_BITS; j++) {
if(tree[idx].set_flag & (1 << j)){
tree[child].count[j] = tree[child].r - tree[child].l + 1;
}
}
}
// Then handle flip flags
if(tree[idx].flip){
// Apply flip
// For bits that are to be flipped, toggle the count
unsigned int bits = tree[idx].flip;
// Update counts
for(int j=0; j<MAX_BITS; j++) {
if(bits & (1 << j)){
tree[child].count[j] = (tree[child].r - tree[child].l + 1) - tree[child].count[j];
}
}
// Update flip flags
tree[child].flip ^= tree[idx].flip;
// Also, if there are set or reset flags, they might be affected by flip
// However, since set and reset overwrite, and we've handled set and reset before flip, no need to adjust them here
}
}
// After pushing down, clear the current node's flags
tree[idx].flip = 0;
tree[idx].set_flag = 0;
tree[idx].reset_flag = 0;
}
// Update the segment tree in range [l, r] with operation masks
void update_range(int idx, int l, int r, unsigned int set_mask, unsigned int reset_mask, unsigned int flip_mask){
if(tree[idx].r < l || tree[idx].l > r){
return;
}
if(l <= tree[idx].l && tree[idx].r <= r){
// Apply set_mask
if(set_mask){
tree[idx].set_flag |= set_mask;
tree[idx].reset_flag &= ~set_mask;
tree[idx].flip &= ~set_mask;
for(int j=0; j<MAX_BITS; j++){
if(set_mask & (1 << j)){
tree[idx].count[j] = tree[idx].r - tree[idx].l + 1;
}
}
}
// Apply reset_mask
if(reset_mask){
tree[idx].reset_flag |= reset_mask;
tree[idx].set_flag &= ~reset_mask;
tree[idx].flip &= ~reset_mask;
for(int j=0; j<MAX_BITS; j++){
if(reset_mask & (1 << j)){
tree[idx].count[j] = 0;
}
}
}
// Apply flip_mask
if(flip_mask){
unsigned int bits = flip_mask;
for(int j=0; j<MAX_BITS; j++) {
if(bits & (1 << j)){
tree[idx].count[j] = (tree[idx].r - tree[idx].l + 1) - tree[idx].count[j];
}
}
tree[idx].flip ^= flip_mask;
}
return;
}
push_down(idx);
update_range(idx*2, l, r, set_mask, reset_mask, flip_mask);
update_range(idx*2+1, l, r, set_mask, reset_mask, flip_mask);
for(int j=0; j<MAX_BITS; j++){
tree[idx].count[j] = tree[idx*2].count[j] + tree[idx*2+1].count[j];
}
}
// Query the sum in range [l, r]
ll query_sum(int idx, int l, int r){
if(tree[idx].r < l || tree[idx].l > r){
return 0;
}
if(l <= tree[idx].l && tree[idx].r <= r){
ll res = 0;
for(int j=0; j<MAX_BITS; j++){
res += (ll)(tree[idx].count[j]) * ((1LL) << j);
}
return res;
}
push_down(idx);
return query_sum(idx*2, l, r) + query_sum(idx*2+1, l, r);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// Read n
cin >> n;
// Read array a[1..n]
for(int i=1; i<=n; i++) cin >> a[i];
// Build the segment tree
build(1, 1, n);
// Read Q
cin >> Q;
while(Q--){
int type;
cin >> type;
if(type == 0){
int l, r;
cin >> l >> r;
ll res = query_sum(1, l, r);
cout << res << "\n";
}
else{
int l, r;
ll v;
cin >> l >> r >> v;
unsigned int set_mask = 0, reset_mask = 0, flip_mask = 0;
if(type == 1){
// XOR v: flip bits where v has 1
flip_mask = v;
}
else if(type == 2){
// AND v: set bits to 0 where v has 0
// reset_mask = ~v, but only up to MAX_BITS
for(int j=0; j<MAX_BITS; j++){
if( ((int)v & (1 << j)) == 0 ){
reset_mask |= (1 << j);
}
}
}
else if(type == 3){
// OR v: set bits to 1 where v has 1
set_mask = v;
}
update_range(1, l, r, set_mask, reset_mask, flip_mask);
}
}
}
Explanation of the Code:
Initialization:
The segment tree is built using the build function, which recursively populates the count array for each bit position in every segment.
Handling Updates:
The update_range function applies the required bitwise operations (XOR, AND, OR) to the specified range [l, r].
Depending on the operation type, appropriate masks (flip_mask, set_mask, reset_mask) are created and applied.
Lazy propagation ensures that updates are deferred and efficiently applied only when necessary.
Handling Queries:
The query_sum function calculates the sum of elements in the range [l, r] by summing the contributions of each bit based on the count of set bits.
Lazy Propagation:
The push_down function ensures that any pending updates (flip, set, reset) are correctly propagated to the child nodes before any further operations are performed on them.
Sample Input and 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 performing the operations as described in the problem statement, the sum queries correctly return 31 and 24, matching the expected output.
This implementation efficiently handles large inputs within the provided constraints using advanced data structures and bitwise operations.