DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Dynamic Tree Vertex Add Subtree Sum

Back to problem

Editorial

A query for an edge (v,p)(v,p)(v,p) asks for the sum of the connected component containing vvv after temporarily removing that edge.

Maintain a Link-Cut Tree. For every node, store its own value, the aggregate of its auxiliary splay children, and the aggregate of its virtual children outside the current preferred paths. During access, when the right child changes between a preferred child and a virtual child, update the virtual aggregate accordingly. Then after makeroot(x) and access(x), the aggregate stored at xxx is the sum of the whole represented-tree component containing xxx.

Thus a query of type 0 is handled by cut and link, a query of type 1 by a point addition, and a query of type 2 by cut(v,p), reading the component sum of vvv, and link(v,p). Each operation takes O(log⁡N)O(\log N)O(logN) amortized time.

Solution written by GPT5.5