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 | tourist | 3856 |
2 | jiangly | 3747 |
3 | orzdevinwang | 3706 |
4 | jqdai0815 | 3682 |
5 | ksun48 | 3591 |
6 | gamegame | 3477 |
7 | Benq | 3468 |
8 | Radewoosh | 3462 |
9 | ecnerwala | 3451 |
10 | heuristica | 3431 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | -is-this-fft- | 162 |
3 | Dominater069 | 160 |
4 | Um_nik | 158 |
5 | atcoder_official | 157 |
6 | Qingyu | 156 |
7 | adamant | 151 |
7 | djm03178 | 151 |
7 | luogu_official | 151 |
10 | awoo | 146 |
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
Unable to parse markup [type=CF_TEX]
and subtract it fromUnable to parse markup [type=CF_TEX]
, since bit tree returns forUnable to parse markup [type=CF_TEX]
we always get the right results for point queriesNow in order to make range_queries and range_updates, lets have a look at our array a:
Unable to parse markup [type=CF_TEX]
Calling
Unable to parse markup [type=CF_TEX]
should make it like this:Unable to parse markup [type=CF_TEX]
And prefix_sum array:
Unable to parse markup [type=CF_TEX]
Let's divide the array into 3 parts:
Unable to parse markup [type=CF_TEX]
Unable to parse markup [type=CF_TEX]
should return 0Unable to parse markup [type=CF_TEX]
Unable to parse markup [type=CF_TEX]
should returnUnable to parse markup [type=CF_TEX]
Unable to parse markup [type=CF_TEX]
Unable to parse markup [type=CF_TEX]
should returnUnable to parse markup [type=CF_TEX]
So basically,
Unable to parse markup [type=CF_TEX]
is now like a linear function of index x:Unable to parse markup [type=CF_TEX]
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