code_hard123's blog

By code_hard123, history, 8 years ago, In English

Given an array, and there are Q queries of two types.

Type-1 : given l , r , val

Update a[i] = a[i] xor val where i = [l , r]

Type-2 : given l , r

Return sum of a[i] where i = [l , r]

Can it be solved by segment tree with lazy propagation?

ThankYou :)

  • Vote: I like it
  • +4
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it +7 Vote: I do not like it

Yea, it can be solved using segment tree + lazy propagation, the idea is to keep the count of bits for all the values in a range.

Xoring a range basically means swapping the count of 1 bits with the count 0 bits for each subrange in the segment tree if val has 1 bit at that position.

The query is solved by adding 2^(bit_position)*cnt[bit_position] for each bit position and for every subrange.

ex problem from a past CF contest: http://mirror.codeforces.com/problemset/problem/242/E c++ solution: https://github.com/Jaskamalkainth/Codeforces/blob/master/xor_on_segment.cpp

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks a lot! :)

»
8 years ago, # |
  Vote: I like it -8 Vote: I do not like it

IMPOSIBLE