ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 2119. Tree Hull

Any hints?
Posted by Zergatul 28 Feb 2021 04:43
My thoughts for now:
- Generate list of parents for every node, as well as weight sum (2nd parent, 4th parent, 8th parent and so on). This will allow to calculate weight sum between two nodes in O(log N)
- Implement LCA (better with O(1) time?)
- Store root of subtree in variable
- When new node comes, find LCA(new node, subtree root). 2 variants here:
   1) LCA = new node (need to add sum of weights from new subtree root to previous subtree root)
   2) LCA = existing subtree root. What to do here?
- When we remove node?
Re: Any hints?
Posted by Gilles Deleuze 28 Feb 2021 14:50
Sort v_i by depth-first order.
d[v_i] is the sum of weights from the root to v_i

The answer, when there are n nodes, is:
sum_{i=1}^{n} d[v_i] - d[LCA(v_i, v_{i+1})]; where v_{n+1}:=v_1

Implement these operations using std::set with a custom comparator that orders vertices using depth-first order.

The proof of the formula is left to the reader as an exercise.
Re: Any hints?
Posted by Zergatul 4 Mar 2021 05:52
WOW, that's really smart. Thank you