### 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 :

- Add
**d**(input) to numbers of all vertices along the unique path between vertices**u**and**v**(both input) - Find out GCD of all numbers on the vertices along unique path between vertices
**u**and**v**(both input).

### 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, b-a, c-b, d-c…)** 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**

So what can we do if given tree is a linear path? Now our change operations

is reduced to to addition of a constant to a contiguous sub-array and our find opertion

reduces to finding out gcd of all numbers on a contiguous sub-array. Experienced

programmers would’ve seen immediately that a datastructure like segment tree with

technique of lazy propogation could be useful for this problem, however what information

is to be stored at each segment, that is not obvious. There are two very simple

identities at the core of this problem. Assume **(a,b)** represents **gcd(a,b)**.

**(a,b) = (a,b-a)****(a,b,c) = (a,(b,c)) = ((a,b),c)**

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

(A

_{1}+ k, A_{2}+ k, … A_{n}+ k)Now using identities 1 and 2 we can say that :

**G**is same as **(A

_{1}, A

_{2}- A

_{1}, A

_{3}

- A
_{2}…, A_{n}-A_{n-1})**

=>**G = (A**_{1}, (A_{2}-A_{1},

A_{3}-A_{2}…, A_{n}- A_{n-1}))

So similarly **G’ = ( A _{1} + k, A_{2} + k - (A_{1} + k)**

, …, A_{n} + k - (A_{n-1} + k) )

=>

**G’ = ( A**

A

_{1}+ k, A_{2}- A_{1},A

_{3}- A_{2}, … A_{n}- A_{n-1})=>

**G’ = ( A**

A

_{1}+ k, (A_{2}-A_{1},A

_{3}-A_{2}, …, A_{n}- A_{n-1}))Now call **{ A _{2}-A_{1}, A_{3}-A_{2}, …,** as the difference sequence. So if we compare

A_{n} - A_{n-1} }

**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

QTREE3 on Spoj

IPSC 2009 problem L