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

Автор CQXYM, 5 лет назад, По-английски

Hello!

You may have met these kinds of problems that require you to calculate the prefix sum with modifying, and you can use a fenwick tree SUM in order to solve it. When you want to lower_bound on it, you can reference this way as follows: You wonder the minimum index in the array SUM[L~R] satisfied SUM[index]≥K.

Set l=0, r=R, mid=R three pointers as the left, right, middle.
If log2(r-l) is a positive interger, mid = (r - l)/2, or mid = pow(2, ceil(r-l) ) + l.
If SUM[mid]≥K, r = mid, or l = mid.
Continue to do it if l < mid < r.
In the end, index = mid.

The Picture

For this algorithm, it's not difficult to prove that the time complexity is about log2(R). You can understand it better with the help of this picture. And also you can solve such problems with a segment tree If there is something wrong with my algorithm, please tell me. Thanks.

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

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

I can't see the picture. Is the link ok?

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

In addition, if the final mid≤L, index=L, or index=mid

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

I can't understand what you are explaining. Maybe just because I'm too vegtable.