An array is given. I want to scan from the front. I want to delete.
requirement
Prepare a zero-filled array next, prev of the same size as the given array
When to delete
python
prev[pos + 1 + next[pos]] += prev[pos] + 1
next[pos - 1 - prev[pos]] += next[pos] + 1
In short bi-directional linked list.
If constructed simply, the initial value would be next[pos] = pos + 1, but by having only the difference from that value, it can be initialized with a zero fill.
If you only do deletions, this is fine because you don't need to have absolute addresses. If you also do insertions, a regular two-way list is better.
The first element is the guard. It should not be deleted. We have not yet considered what would happen if it were deleted.
Random access in index at creation is O(1)
This page is auto-translated from /nishio/ゼロフィルのリンクトリスト using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.