PROBLEM LINK:DIFFICULTY:Hard PREREQUISITES:Number theory, Segment Trees, Heavy Light Decomposition PROBLEM:You're given a tree of N vertices. Each vertex has a number written on it. Process two types of queries :
QUICK EXPLANATION:Use Heavy Light Decomposition of the tree to reduce problem to only linear paths. Use the fact that gcd(a, b, c, d..) = gcd(a, ba, cb, dc...) to change segment updates to point updates. DETAILED EXPLANATION:It's a difficult problem and as with many other difficult problems as well  the secret of solving this problem is solving a simplification of the problem. Simplification : Given tree is a linear path
It is still not clear how these help us. Let us first try to see how gcd is changed (or rather how it is not changed) when we increment all the numbers of a sequence. Say G = (A_{1},A_{2},...,A_{n}) and G' =
(A_{1} + k, A_{2} + k, ... A_{n} + k) So similarly G' = ( A_{1} + k, A_{2} + k  (A_{1} + k)
, ...., A_{n} + k  (A_{n1} + k) ) Now call { A_{2}A_{1}, A_{3}A_{2}, ...., A_{n}  A_{n1} } as the difference sequence. So if we compare G and G', we see that much of the information has been retained through the gcd of the difference sequence. In particular it tells us what information we need to store at each segment of segment tree: first number of the sequence and the gcd of the difference sequence. With this information, its easy to find gcd of the segment : its the gcd of first number and gcd of difference sequence. It's also easy to increment all numbers of the sequence by d, gcd of difference sequence remains unchanged and the first number increments by d. If we want to merge two consecutive smaller segments to make a larger segment, as we'd need to do with segment trees, we also need to store the last number of the segment. Using this, it is easy to write a segment tree which can do all operations in O(logN) time, making it an O( (N + Q) * logN) solution. Back to original problem: We can solve original problem with the help of a technique called Heavy Light Decomposition of a tree. Essential idea is as follows. We divide the vertex set of the tree in O(N) linear chains such that following property holds : On the unique path between any two vertices u and v, there are utmost 2 * log(N) different chains. If we can find such a decomposition, we could build individual segment trees on each of these chains. Now a single query would correspond to quries on each of these O(log N) chains making a single query O( (log N)^{2}) overall. I'm not going to explain Heavy Light Decomposition here  rather I'd leave you with an exceptionally well written description of this technique. I myself learnt this technique from here. SETTER'S SOLUTION:Can be found here. TESTER'S SOLUTION:Can be found here. RELATED PROBLEMS:QTREE on Spoj REFERENCES:
This question is marked "community wiki".
asked 11 Jul '12, 16:33

Tester's solution solves only a simplified version of the problem. answered 11 Jul '12, 18:48

I am getting TLE!! Can anyone HELP me ? my code : https://ideone.com/m7wSJ0 answered 17 Feb '16, 22:19

what is the worst case that cause TLE?, i use HLD + segment tree + lazy update answered 16 May '17, 16:47

Setters's solution gives WA. Check this: https://www.codechef.com/viewsolution/17287540 answered 05 Feb, 10:05
