CF463E.Caisa and Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Caisa is now at home and his son has a simple task for him.

Given a rooted tree with nn vertices, numbered from 11 to nn (vertex 11 is the root). Each vertex of the tree has a value. You should answer qq queries. Each query is one of the following:

  • Format of the query is "1 vv ". Let's write out the sequence of vertices along the path from the root to vertex vv : u1,u2,...,uku_{1},u_{2},...,u_{k} (u1=1; uk=v)(u_{1}=1; u_{k}=v) . You need to output such a vertex uiu_{i} that gcd(value of u_{i},value of v)>1 and i<k . If there are several possible vertices uiu_{i} pick the one with maximum value of ii . If there is no such vertex output -1.
  • Format of the query is "2 vv ww ". You must change the value of vertex vv to ww .

You are given all the queries, help Caisa to solve the problem.

输入格式

The first line contains two space-separated integers nn , qq (1<=n,q<=105)(1<=n,q<=10^{5}) .

The second line contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} (1<=ai<=2106)(1<=a_{i}<=2·10^{6}) , where aia_{i} represent the value of node ii .

Each of the next n1n-1 lines contains two integers xix_{i} and yiy_{i} (1<=xi,yi<=n; xiyi)(1<=x_{i},y_{i}<=n; x_{i}≠y_{i}) , denoting the edge of the tree between vertices xix_{i} and yiy_{i} .

Each of the next qq lines contains a query in the format that is given above. For each query the following inequalities hold: 1<=v<=n1<=v<=n and 1<=w<=21061<=w<=2·10^{6} . Note that: there are no more than 5050 queries that changes the value of a vertex.

输出格式

For each query of the first type output the result of the query.

输入输出样例

  • 输入#1

    4 6
    10 8 4 3
    1 2
    2 3
    3 4
    1 1
    1 2
    1 3
    1 4
    2 1 9
    1 4
    

    输出#1

    -1
    1
    2
    -1
    1
    

说明/提示

gcd(x,y)gcd(x,y) is greatest common divisor of two integers xx and yy .

首页