Блог пользователя code_hard123

Автор code_hard123, история, 8 лет назад, По-английски

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 :)

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

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://codeforces.com/problemset/problem/242/E c++ solution: https://github.com/Jaskamalkainth/Codeforces/blob/master/xor_on_segment.cpp

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Thanks a lot! :)

»
8 лет назад, # |
  Проголосовать: нравится -8 Проголосовать: не нравится

IMPOSIBLE