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

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

Given a tree with n nodes, rooted at 1. You have q offline queries with the form of ui, vi and xi. You have to add xi to every node in the simple path from ui to vi. It is guaranteed than ui is an ancestor of vi. Then you have to print out the values of all the nodes. The limit is... well, too large for O(n^2). Could anyone give me an approach to this problem?

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

»
7 лет назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

Since you asked for an approach, I won't go in details, but just give the approach.

Edit: Seems like I've overkilled it for both online and offline. Refer to Bedge's solution for the simple approach.


For O(log) amortized time for a query you can use centroid decomposition. Each time you find the centroid you can process all queries that pass through it in constant amortized time per query.

Additionally, in case you want to solve the problem online, there's a O(log2) per query solution using heavy-light decomposition with lazy-propagation segment trees for the heavy paths.

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

Another approach is to use partial sum on tree.

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

https://mirror.codeforces.com/contest/1076/problem/E
Similar problem if you want to try another one.