A simple introduction to "Segment tree beats"

Revision en5, by jiry_2, 2018-01-24 18:02:56

Hi, I’d like to introduce a simple trick about segment tree in this blog as I promised in this comment. Sorry for the long delay, as a sophomore in Peking University, I've just finished a tired semester and a painful final exam. And now I finally have enough time to do a simple introduction to this interesting algorithm.

It may be a huge project for me since my English is not good. I think I will finish this blog in several steps and I will try to finish it as soon as possible :)

In China, all of the 15 candidates for the Chinese National Team are asked to write a simple research report about algorithms in informatics Olympiad, and the score will be counted in the final selection. There are many interesting ideas and algorithms in these reports. And I find that some of them are quite new for competitors in CF although they are well known in China from the final standings of some recent contests. For example, In the last contest CF Round #458, the problem G can be easily solved using set power series (I don’t know how to describe it in English so I just use the phrase given by Baidu translate) in O(2nn2) which was imported in China by vfleaking in 2015.

This blog is about my report which is written about two years ago. I am satisfied with this work although there is a flaw about time complexity. xyz111 gave me a lot of inspiration, and the name “Segment tree beats” is given by C_SUNSHINE which is from a famous Japanese anime “Angel Beats”.

Here is the link about the Chinese version of this report.

Part1. What it can do.

This work have two part:

  1. Transform interval max/min (for ) operations into interval add/minus operations in O(1) or
  2. Transform history max/min/sum queries into interval max/min operations in O(1).

Here are some sample problems I used in my report. At first you have a array A of length n and two auxiliary arrays B, C. Initially, B is equal to A and C is all zero.

Task 1. There are two kinds of operations:

  1. For all , change Ai to max(Ai, x)
  2. Query for the sum of Ai in [l, r]

It can be solved in

Task 2. We can add more operations to Task 1:

  1. For all , change Ai to max(Ai, x)
  2. For all , change Ai to min(Ai, x)
  3. For all , change Ai to Ai + x, x can be a negative number
  4. Query for the sum of Ai in [l, r]

It can be solved in and I could not proved the exact time complexity yet, maybe It is still ;)

Task 3. And we can query for some other things:

  1. For all , change Ai to max(Ai, x)
  2. For all , change Ai to min(Ai, x)
  3. For all , change Ai to Ai + x, x can be a negative number
  4. Query for the sum of Bi in [l, r]

After each operation, for each i, if Ai changed in this operation, add 1 to Bi.

It’s time complexity is the same as Task 2.

Task 4. We can also deal with several arrays synchronously. Assume there are two arrays A1 and A2 of length n.

  1. For all , change Aa, i to min(Aa, i, x)
  2. For all , change Aa, i to Aa, i + x, x can be a negative number
  3. Query for the max A1, i + A2, i in [l, r]

It can be solved in and if there are k arrays, the time complexity will be raised to .

Task 5. And there are some tasks about history informations.

  1. For all , change Aa, i to Aa, i + x, x can be a negative number.
  2. Query for the sum of Bi in [l, r].
  3. Query for the sum of Ci in [l, r]

After each operation, for each i, change Bi to max(Bi, Ai) and add Ai to Ci.

The query for Bi can be solved in and the query for Ci can be solved in .

Task 6. We can even merged the to parts together.

  1. For all , change Aa, i to max(Aa, i, x)
  2. For all , change Aa, i to Aa, i + x, x can be a negative number.
  3. Query for the sum of Bi in [l, r].

After each operation, for each i, change Bi to min(Bi, Ai).

It can be solved in .

There are 11 sample tasks in my report and here are 6 of them. All of them are interesting and are hard to solve using the traditional techniques such as lazy tags.

In the next update, I suppose to introduce the main idea of this trick without the prove of the time complexity. Since I’m traveling in Harbin and the temperature of thirty degrees below zero makes me really tired, I want to go to rest early. Sorry for the interruption and I’ll try to finish the rest part as soon as possible :)

Tags #data structure

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en21 English jiry_2 2018-01-28 04:43:36 12 Tiny change: 'ven by [usr:xyz111] ' -> 'ven by [user:xyz111] '
en20 English jiry_2 2018-01-27 14:17:29 117
en19 English jiry_2 2018-01-27 14:08:14 1735 Tiny change: '90605.png)\n\nThus, ' -> '90605.png){:height="100px" width="400px"}\n\nThus, '
en18 English jiry_2 2018-01-27 13:40:43 7 Tiny change: ', we will also use the t' -> ', we will still use the t'
en17 English jiry_2 2018-01-27 13:39:20 1607 Tiny change: 'the total number of $\Phi(' -> 'the total increase of $\Phi('
en16 English jiry_2 2018-01-27 12:56:03 365
en15 English jiry_2 2018-01-27 12:43:04 4659 Add the proof for task 1
en14 English jiry_2 2018-01-27 12:35:19 33
en13 English jiry_2 2018-01-27 11:56:33 5 Tiny change: '& r <= rr and max_value' -> '& r <= rr && max_value'
en12 English jiry_2 2018-01-27 10:29:51 49
en11 English jiry_2 2018-01-27 10:12:20 4 Tiny change: 'Historic maximal value' -> 'Historic minimal value'
en10 English jiry_2 2018-01-26 17:01:38 2806
en9 English jiry_2 2018-01-25 17:21:34 136
en8 English jiry_2 2018-01-25 17:17:52 5446 Tiny change: 'affected. \n2. When ' -> 'affected. So we can return immediately.\n2. When '
en7 English jiry_2 2018-01-25 13:07:08 12
en6 English jiry_2 2018-01-24 18:05:58 1 Tiny change: 'rged the to parts to' -> 'rged the two parts to'
en5 English jiry_2 2018-01-24 18:02:56 3 Tiny change: 'in China for the final' -> 'in China from the final'
en4 English jiry_2 2018-01-24 17:40:31 5 Tiny change: '2)$ which is import in China ' -> '2)$ which was imported in China '
en3 English jiry_2 2018-01-24 17:26:29 2 Tiny change: ') in $O(2^n n^2)$ whi' -> ') in $O(2^{n} n^2)$ whi'
en2 English jiry_2 2018-01-24 17:25:28 1 Tiny change: ' in $O(2^nn^2)$ whic' -> ' in $O(2^n n^2)$ whic'
en1 English jiry_2 2018-01-24 17:24:51 5058 Initial revision (published)