MCPcopy Create free account
hub / github.com/acm-clan/algorithm-stone / SegmentTreeQuery

Class SegmentTreeQuery

animations/segmenttree.py:214–261  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

212 self.wait(10)
213
214class SegmentTreeQuery(SegmentTreeBase):
215 def __init__(self, **kwargs):
216 super().__init__(**kwargs)
217
218 def query(self, node, l, r):
219 if node.l == l and node.r == r:
220 self.play(self.tree.get_node(node.id).set_color, BLUE)
221 self.show_message("找到区间[%d, %d]"%(l, r))
222 self.wait()
223 return node.v
224 m = math.floor((node.l+node.r)/2)
225 #
226 if r <= m:
227 arrow = self.tree.get_edge(node.id, node.left.id)
228 self.play(arrow.set_color, BLUE)
229 return self.query(node.left, l, r)
230 elif l >m:
231 arrow = self.tree.get_edge(node.id, node.right.id)
232 self.play(arrow.set_color, BLUE)
233 return self.query(node.right, l, r)
234
235 arrow = self.tree.get_edge(node.id, node.left.id)
236 self.play(arrow.set_color, BLUE)
237 arrow = self.tree.get_edge(node.id, node.right.id)
238 self.play(arrow.set_color, BLUE)
239 return self.query(node.left, l, m)+self.query(node.right, m+1, r)
240
241
242 def construct(self):
243 self.start_logo(animate=False)
244 self.init_message("查询线段树")
245
246 self.tree = AlgoSegTree(self, self.datas).scale(0.9).shift(UP*0.5)
247 self.play(ShowCreation(self.tree))
248
249 for i in range(0, 1):
250 for j in range(i, 6):
251 for k in self.tree.arrows:
252 v = self.tree.arrows[k]
253 v.set_color("#6e6e6c")
254 for k in self.tree.node_objs:
255 v = self.tree.node_objs[k]
256 v.set_color("#6e6e6c")
257 self.show_message("查询区间[%d,%d]的和"%(i, j))
258 value = self.query(self.tree.root, i, j)
259 self.play(Uncreate(self.tree))
260 self.finish_scene("完成线段树,谢谢观看!")
261 self.wait(10)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected