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

Автор Noble_Mushtak, история, 2 года назад, По-английски

I recently found an implementation of Li Chao tree in this ICPC notebook, and it is quite different than the implementations I have seen before. Usually, I see the insert_line method implemented in a way similar to that described in this cp-algorithms.com article, where you recur either on the left child or the right child depending on if the new line is above or below the current line at the midpoint of the interval. However in this implementation, they recur on both the left and the right child, but they stop recurring once the new line is completely below or completely above the current line in that node.

Does this implementation still handle inserting lines in $$$O(\log n)$$$ time in the worst case? I tried using this implementation on the Frog 3 problem on AtCoder and it got AC, but I am wondering if it is due to weak test cases, or if this implementation is actually valid and really handles inserting lines in $$$O(\log n)$$$ time. I wasn't able to find a counterexample where inserting a line would cause the implementation to traverse the whole tree, but recurring on both the left and the right children seems strange to me.

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

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

Auto comment: topic has been updated by Noble_Mushtak (previous revision, new revision, compare).

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

I think you probably want a combination of both implementations, that is, only going to one subtree and pruning some recursion calls by checking if the current line is always above/below.

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

    Also, it probably is hard to construct bad cases for the ICPC notebook code. It only recursively goes down if it intersects the current line at (l, r).

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

[redacted]

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

I think you should be able to conceive a counter-example if you insert a bunch of lines such that the line on node with range $$$l..r$$$ passes through $$$0$$$ at some $$$x \in (l,r)$$$. Then inserting line $$$y=0$$$ multiple times should break the complexity.

You should theoretically be able to fill the tree with such lines in pre-order (it may be hard with integer slopes). It’s definitely much easier if the DS must support insertion of segments (line on range $$$x_1..x_2$$$). So, at least for insertion of segments, it can degenerate to linear complexity for insertion. Maybe someone can give an example that breaks with (unbounded) lines.