Can anyone solve this? Problem G

# | User | Rating |
---|---|---|

1 | tourist | 3805 |

2 | maroonrk | 3637 |

3 | Benq | 3513 |

4 | ecnerwala | 3506 |

5 | MiracleFaFa | 3466 |

6 | slime | 3428 |

7 | jiangly | 3415 |

8 | Radewoosh | 3399 |

9 | greenheadstrange | 3393 |

10 | djq_cpp | 3391 |

# | User | Contrib. |
---|---|---|

1 | awoo | 192 |

2 | -is-this-fft- | 191 |

3 | Monogon | 185 |

4 | Um_nik | 182 |

4 | YouKn0wWho | 182 |

6 | maroonrk | 170 |

7 | antontrygubO_o | 167 |

8 | errorgorn | 166 |

8 | kostka | 166 |

10 | SecondThread | 165 |

Can anyone solve this? Problem G

↑

↓

Codeforces (c) Copyright 2010-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/17/2022 19:45:20 (f1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

If my understanding is correct the problem statement reduces to (assuming we can replace each node with an edge of the same weight)- Given a tree, find its diameter and there are updates that increase the weight of some edges? In that case, I don't understand why mod 1e9+7

I think you misunderstood the statement

I understand the statement as follows: You are given a tree with weights on edges ($$$b$$$) and vertices ($$$a$$$), calculate the sum over all pairs of vertices $$$\sum_i \sum_j \texttt{dist}(i, j) \cdot a_i \cdot a_j$$$. Queries may modify the weight of edges and vertices, and we have to report the value of the sum after each query.

If it is okay, then it can be solved with centroid decomposition in $$$\mathcal{O}((n + q) \log^2 n)$$$. When we modify the weight of a vertex $$$i$$$ we want to answer the sum $$$a_i \cdot \sum_i \texttt{dist}(i, j) \cdot a_j$$$, when we modify the weight of an edge $$$b_i$$$, we change the answer by a sum over $$$a_i$$$ in its subtree multiplied by the rest of weights. The latter can be done with one segment tree.

So we have to quickly answer, for a vertex, the sum of distances (multiplied by some coefficient) to all the other vertices. We keep the centroid decomposition, and for each subtree, in the decomposition, we keep a segment tree with distances from the root to the vertex, multiplied by the weight of the vertex. This requires some formulas, but we can update the weight of an edge (update on interval), the weight of a vertex (just one path to change), and query for the sum paths, which we need to answer when we change the weight of a vertex.

You can solve this problem here