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

Автор P_Nyagolov, история, 9 лет назад, По-английски

Hello everybody,

Last year I read that dynamic trees exist, that is trees in which we create only the nodes we need since there can be a lot (say we have indices 1,...,10^9). I thought it's cool and wanted to know more about it. I googled a few times "Dynamic BIT", "Dynamic trees" or something like that but I found nothing...

Recently I participated in BOI 2015 and there was a problem that required a segment tree for 60 points and dynamic segment tree or AVL Tree which I can't code for full score so I wasn't able to get more than 60 on it. Now, I tried googling "Dynamic Segment Tree" but found nothing...

Can somebody please explain how dynamic segment trees work and give me an implementation, I would appreciate your help!

Thanks in advance! :)

UPD: Thanks everybody for the help, I understood it! So basically, we initially have the root only which stores the whole interval ([1;10^9] for example). Then the update/query goes exactly like in the normal segment tree but if we don't have a child of some node but we need it, we just create it and go on. I implemented Horrible Queries from SPOJ (I know that a normal segment tree is enough, but I wanted to test it). Here is my accepted code, it might be helpful for somebody who has problems with this structure — http://ideone.com/hdI5aX.

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

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

You can use coordinates compression technique. For example if we have 105 numbers each less than 109 you can sort them and give every unique number an index, so you will have up to 105 element and you can use segment tree easily. I've seen an implementation to dynamic segment trees using pointers and stuff but I prefer this technique to not complicate things..

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

    Unfortunately, you could not use this technique as the task in question was interactive, so online solution was required.

»
9 лет назад, # |
Rev. 10   Проголосовать: нравится +19 Проголосовать: не нравится

Main idea: in dynamic segment tree we create a node only when we need in it.

Example: "Monkey and apples" from IZhO 2012 — Given an array of size 109 and 2 types of queries — assign 1 to segment [L, R] and get sum on segment [L, R].

Tree: We can create struct with four parameters (sum, assign_value, left_node, right_node).

Update: Apply the main idea. Assume that you stand in node 1 (segment [1, 109]) and you need to assign 1 to segment that means you need to go to your left child in tree. If your current node haven't got left (or right, doesn't matter) child we can just create it, can't we? You can do it easily — just keep counter.

Get sum: Again assume that you need to find sum on segment and you are currently in node 1. If your current node haven't got left child that means that you never updated this segment so it have zero sum else you can just return it's sum.

Links: Problem (Day 2, F), tests, my code, jury's code, participants' codes.

P.S Sorry for my poor English.

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

can someone tell me why am i wrong?

http://ideone.com/BxPXmD

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

There is another idea for that. It is more easy to code but every query will have O(lg2).

Use map for saving segment tree (or BIT(fenwick tree) ). like this : 18066766.

You can use unordered_map too.