algorithm - segment tree with lazy propagation in large range -
can please tell me how lazy propagation works in segment tree if want update values in range? also, how problem using segment tree , lazy propagation?
suppose there 15 boys in row, standing facing east, , after 3 moves range [3,6] facing north, , after 2 moves facing west. how update range if our row size around 106?
clockwise direction [east-->south-->west-->north-->east]
for example: suppose there n students standing facing east, , have move students 3 6 2 moves in clockwise direction. thus, after move, students "e e w w w w e e e e". then, want find max number of students in range standing facing same direction. in example, if find answer in range [1,6], there 2 students facing east , 4 facing west, answer 4.
http://wcipeg.com/wiki/segment_tree#lazy_propagation
to problem:
suppose have n people. create segment tree addition n leafs value 0 initially. if want turn people indices j clockwise, update [i..j] range +1, if counterclockwise -- -1.
let's encode north = 0, east = 1, south = 2 , west = 3. example, if people stand "n e e s" in notation becomes "0, 1, 1, 2" after encoding.
let's sum values (considering range updates!) leaves of tree accordings in encoded array (n log n). pretend turned people [1..2] counterclockwise, , 2 times [4] clockwise. after addition array "-1, 0, 1, 4".
now apply modulo operation every number of array: "3, 0, 1, 0". after decoding final arrangement: "w n e n". finding people facing same direction trivial now.
Comments
Post a Comment