Hello cf! I am solving this problem http://www.spoj.com/problems/HORRIBLE/ . And I don't know how to implement BIT with range modification. Please help me. Thanks.
| # | User | Rating |
|---|---|---|
| 1 | Kevin114514 | 3722 |
| 2 | Benq | 3676 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3196 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | maroonrk | 153 |
| 2 | Qingyu | 152 |
| 3 | nik_exists | 151 |
| 4 | AmShZ | 147 |
| 4 | maspy | 147 |
| 6 | Um_nik | 144 |
| 7 | Errichto | 139 |
| 8 | adamant | 135 |
| 9 | BledDest | 134 |
| 10 | AksLolCoding | 132 |
Hello cf! I am solving this problem http://www.spoj.com/problems/HORRIBLE/ . And I don't know how to implement BIT with range modification. Please help me. Thanks.
| Name |
|---|



You should read about Segment Tree. This blog relly helped me to understand it — segment tree!
there are great tutorial on russian: tutorial, you can translate it with any translator.
There is a variant of BIT Trees that does range_update & point_query we will use this one
To update a range [l, r] with value V (incrementing every element in range by V), we add V to bit[l] and subtract it from bit[r + 1], since bit tree returns for
we always get the right results for point queries
Now in order to make range_queries and range_updates, lets have a look at our array a:
[0, 0, 0, 0, 0, 0]
Calling update(l = 3, r = 5, v = 5) should make it like this:
[0, 0, 5, 5, 5, 0]
And prefix_sum array: [0, 0, 5, 10, 15, 15]
Let's divide the array into 3 parts:
a[1, ..., l - 1] query(x) should return 0
a[l, ..., r] query(x) should return V × (x - (l - 1)) = Vx - V(l - 1)
a[r + 1, ..., N] query(x) should return V × (r - (l - 1)) = Vr - V(l - 1)
So basically, query(x) is now like a linear function of index x: f(x) = Ax + B
The terms that depend on x can be stored in a bit tree and other terms in another bit tree
Pseudo_code:
Wow, now it's clear for me, thank you ^^
But in function update_range(..) in second row u should write
update_point(bitAX, r+1, -v)
You are forgot a minus before "v" :)
UPD: fixed
Thanks, sorry for forgetting such important thing :D
Thank you for your perfect explanation! Is not query (r) -query (l-1) on the last line? I think it should be minus, not plus.
Yes! That should have been minus instead of plus.
Read this Blog : Various Usage Of BIT