the hint video is completely useless please help me with this question
Please explain this question
Hey @be10046.19, the question is one of the classic problems of segment tree and range queries. Let’s see the sample test case, Initially we are given integer zero i.e. 000
1 0 1, setting bits from 0 to 1 as one, 11 0
2 0 2, the integer formed from bits 110, is 6 .
0 1 1, set bits from 1 to 1 as zero, 1 0 0
2 1 1, the integer formed from bits 1 to 1, 0
1 2 2, set bits from 2 to 2 as zero, 10 1
2 0 2, the integer formed from bits 0 to 2, 5 .
The approach can be either iterating from L to R each time, but this is naive and will give TLE.
So think in terms of range queries.
yes i know range queries is the method but implementation is difficult for me to understand from the code provided in the discussion forum
The code uses lazy propagation, which means delaying the update until the query asked for an updated answer.
Update function - if we know the answer of the left half and right half then answer of current answer would be
ans= left_answer * 2^(end-mid)+right_answer or
t[node] = (t[node * 2 + 1] * pow(2, en - mid, mod) % mod + t[node * 2 + 2]) % mod;
Similar for the query function, recursively ask for left and right sub answers and use for current answer.
To know if the current node is updated or not, maintain an array lz[], if the node is not updated we update the node as well as all it’s children.
t[index]=(pow(2,se-ss+1)-1)*lazy[index]; and t[index]=(pow(2,(se-ss+1))-1)*x; please explain these two lines why are we doing this?
2^(no of 1’s in the range)-1 gives the decimal value of the range multiplying decimal val with 2^(r-e) shifts the value to left by the required amount. 111->11100 (7->28)