DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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

Star Tree

Back to problem

Editorial

Subtask 1: direct updates

For each query, find the path from xxx to yyy by BFS and add 1,2,…1,2,\ldots1,2,… along it starting at xxx. This takes O(N+NQ)O(N+NQ)O(N+NQ) time and O(N)O(N)O(N) space, sufficient for N,Q≤2000N,Q\leq2000N,Q≤2000.

Subtask 2: the path in label order

When the edges are {i,i+1}\{i,i+1\}{i,i+1}, a path is an integer interval. If x<yx<yx<y, add v−x+1v-x+1v−x+ at vertex in . Otherwise, add in . Use two range-difference arrays for the coefficient of and the constant term, then take prefix sums. The complexity is time and space.

Full solution: linear functions of depth

Root the tree at vertex 111. Let d(v)d(v)d(v) be its depth and p(v)p(v)p(v) its parent. The root has depth zero and a virtual parent 000. For query (x,y)(x,y)(x,, let be their LCA. Split the path into the segment from through and the segment after through .

On the first segment, the amount at vertex vvv is

d(x)−d(v)+1=−d(v)+(d(x)+1).d(x)-d(v)+1=-d(v)+(d(x)+1).d(x)−d(v)+1=−d(v)+(d(x)+

On the second segment, it is

d(x)−d(z)+d(v)−d(z)+1=d(v)+(d(x)−2d(z)+1).d(x)-d(z)+d(v)-d(z)+1=d(v)+(d(x)-2d(z)+1).d(x)−d(z)+d(v)−d(z)+

Thus each segment receives a constant coefficient of depth and a constant intercept. Include zzz only in the first segment.

Tree differences for ancestor segments

If www is an ancestor of uuu, adding a constant ccc to the inclusive segment from uuu to www can be recorded as

D[u]+=c,D[p(w)]−=c.D[u]\mathrel{+}=c,\qquad D[p(w)]\mathrel{-}=c.D[u]+=c,D[p(w)]−=c.

After all records, accumulate child values into their parents in bottom-up order. The resulting value at a vertex is the sum of differences in its subtree. The contribution starting at uuu reaches www and is canceled above it by the opposite record at p(w)p(w)p(w).

Maintain arrays AAA for depth coefficients and BBB for intercepts. For one query, set c1=d(x)+1c_1=d(x)+1c1​=d(x)+1 and , then record

A[x]−=1,B[x]+=c1,A[x]\mathrel{-}=1,\quad B[x]\mathrel{+}=c_1,A[x]−=1,B[x]+=c1​, A[p(z)]+=1,B[p(z)]−=c1,A[p(z)]\mathrel{+}=1,\quad B[p(z)]\mathrel{-}=c_1,A[p(z)]+=1,B[p(z)]−=c1 A[y]+=1,B[y]+=c2,A[y]\mathrel{+}=1,\quad B[y]\mathrel{+}=c_2,A[y]+=1,B[y]+=c2​, A[z]−=1,B[z]−=c2.A[z]\mathrel{-}=1,\quad B[z]\mathrel{-}=c_2.A[z]−=1,B[z]−=c2​.

The second segment excludes zzz, so it is canceled at zzz, not at p(z)p(z)p(z). If y=zy=zy=z, its two records cancel immediately, correctly representing an empty segment.

After processing all queries, traverse vertices in reverse of a parent-before-child order and add each vertex's AAA and BBB to its parent. Do not output the virtual vertex 000 or propagate its value back into the tree. The answer at vvv is

A[v]d(v)+B[v](mod998 244 353).A[v]d(v)+B[v]\pmod{998\,244\,353}.A[v]d(v)+B[v](mod998244353).

Correctness and complexity

The two segments partition the original path, and each linear expression equals the distance from xxx plus one. Tree differences add exactly the specified coefficients on each segment. By linearity, combining all records before accumulation produces the same result as performing every query explicitly.

Binary lifting gives O(Nlog⁡N)O(N\log N)O(NlogN) preprocessing, O(log⁡N)O(\log N)O(logN) per LCA query, and O(N)O(N)O(N) final accumulation: O((N+Q) time and space overall. If , there can be no queries and the answer is zero. Trees of depth are valid, so construct parents and depths and perform accumulation iteratively.

Independent verification solution

A separate HLD implementation splits each path into oriented contiguous index intervals. Append the intervals found on the xxx side in their original order and those on the yyy side in reverse order. Maintain the next value to add. Each interval then receives an index coefficient of +1+1+1 or −1-1−1 and an intercept, handled by ordinary array range differences. This uses the actual order and lengths of intervals rather than the depth formula above. Its complexity is O(N+Qlog⁡N)O(N+Q\log N)O time and space.

Integer bounds

The final integer value of any vertex is at most QN≤2.5⋅1011QN\leq2.5\cdot10^{11}QN≤2.5⋅1011. All differences and negative intermediate values also fit signed 64-bit arithmetic comfortably. Do not store differences in 32-bit integers or multiply two 32-bit values before converting the result. Accumulate in 64-bit integers and take the remainder only when printing if desired.

Solution written by GPT6

1
vvv
[x,y][x,y][x,y]
x−v+1x-v+1x−v+1
[y,x][y,x][y,x]
vvv
O(N+Q)O(N+Q)O(N+Q)
O(N)O(N)O(N)
y
)
zzz
xxx
zzz
zzz
yyy
1
)
.
1=
d(v)+
(d(x)−
2d(z)+
1).
c2=d(x)−2d(z)+1c_2=d(x)-2d(z)+1
c2​=d(x)−2d(z)+1
​
,
log⁡N)O((N+Q)\log N)
O((N+Q)logN)
O(Nlog⁡N)O(N\log N)O(NlogN)
N=1N=1N=1
499 999499\,999499999
(
N
+
QlogN)
O(N)O(N)O(N)