Skrtel !
Practice
0 (0 votes)
Depth first search
Fenwick tree
Graphs
Medium
Number theory
Prime factorization
Problem
24% Success 511 Attempts 50 Points 2.5s Time Limit 256MB Memory 1024 KB Max Code

Stevie : "The more you play with Christmas Trees, the more addicted you are to them. Let's go on". Legends serve a club selflessly for ages without any noise off the pitch. Do you remember the headed goal vs the Gunners with a highly bandaged head by this guy : ?

                                                                        enter image description here

You are given a Tree T consisting of N nodes where node i of the tree has number \(A[i]\) written on it. Now, you need to process 2 kinds of queries on this tree.

\( 1 \space X \space Y \) : Update the number written on node with index X to Y

\( 2 \space U \space V \space K \) : Find if the product of number written on each nodes lying on the path from node U to V is divisible by the number K.

The answer to the second kind of query is either a YES or NO .

Can you answer all the given queries efficiently?

Input Format :

The first line contains 2 space separated integer N and Q denoting the number of nodes in tree T and the number of queries respectively.

The next line contains N space separated integers, where the \(i^{th}\) integer denotes the number initially written on node i i.e. \(A[i]\)

Each of the next \( N-1 \) lines contains 2 space separated integers U and V denoting an undirected edge between nodes with index U and V in tree T.

It is guaranteed that the input edges are such that it always forms a valid tree.

Each of the next Q lines contains a query of either of the two types on a single line.

\(1^{st}\) type : \( 1 \space X \space Y \)

\( 2^{nd}\) type : \( 2 \space U \space V \space K \)

Output Format:

Print the answer to each query of type 2 in the order they appear in the input on a new line. The answer to the queries shall be either a YES or a NO.

Constraints :

\( 1 \le N,Q \le 100,000 \)

\( 2 \le A[i],Y ,K \le 200,000 \)

\( 1 \le U,V,X \le N \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
7 votes
Tags:
AlgorithmsCombinatoricsDepth First SearchGraphsMathMedium
Points:50
9 votes
Tags:
AlgorithmsC++Depth First SearchDynamic ProgrammingGraphsImplementation
Points:50
12 votes
Tags:
AlgorithmsDepth First SearchGraphs