CF1479D.Odd Mineral Resource

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

In Homer's country, there are nn cities numbered 11 to nn and they form a tree. That is, there are (n1)(n-1) undirected roads between these nn cities and every two cities can reach each other through these roads.

Homer's country is an industrial country, and each of the nn cities in it contains some mineral resource. The mineral resource of city ii is labeled aia_i .

Homer is given the plans of the country in the following qq years. The plan of the ii -th year is described by four parameters ui,vi,liu_i, v_i, l_i and rir_i , and he is asked to find any mineral resource cic_i such that the following two conditions hold:

  • mineral resource cic_i appears an odd number of times between city uiu_i and city viv_i ; and
  • liciril_i \leq c_i \leq r_i .

As the best friend of Homer, he asks you for help. For every plan, find any such mineral resource cic_i , or tell him that there doesn't exist one.

输入格式

The first line contains two integers nn ( 1n31051 \leq n \leq 3 \cdot 10^5 ) and qq ( 1q31051 \leq q \leq 3 \cdot 10^5 ), indicating the number of cities and the number of plans.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ain1 \leq a_i \leq n ).

Then the ii -th line of the following (n1)(n-1) lines contains two integers xix_i and yiy_i ( 1xi,yin1 \leq x_i, y_i \leq n ) with xiyix_i \neq y_i , indicating that there is a bidirectional road between city xix_i and city yiy_i . It is guaranteed that the given roads form a tree.

Then the ii -th line of the following qq lines contains four integers uiu_i , viv_i , lil_i , rir_i ( 1uin1 \leq u_i \leq n , 1vin1 \leq v_i \leq n , 1lirin1 \leq l_i \leq r_i \leq n ), indicating the plan of the ii -th year.

输出格式

Print qq lines, the ii -th of which contains an integer cic_i such that

  • ci=1c_i = {-1} if there is no such mineral resource that meets the required condition; or
  • cic_i is the label of the chosen mineral resource of the ii -th year. The chosen mineral resource cic_i should meet those conditions in the ii -th year described above in the problem statement. If there are multiple choices of cic_i , you can print any of them.

输入输出样例

  • 输入#1

    6 8
    3 2 1 3 1 3
    1 2
    1 3
    2 4
    2 5
    4 6
    3 5 1 1
    3 5 1 3
    3 5 1 3
    1 1 2 2
    1 1 3 3
    1 4 1 5
    1 6 1 3
    1 6 1 3

    输出#1

    -1
    2
    3
    -1
    3
    2
    2
    3

说明/提示

In the first three queries, there are four cities between city 33 and city 55 , which are city 11 , city 22 , city 33 and city 55 . The mineral resources appear in them are mineral resources 11 (appears in city 33 and city 55 ), 22 (appears in city 22 ) and 33 (appears in city 11 ). It is noted that

  • The first query is only to check whether mineral source 11 appears an odd number of times between city 33 and city 55 . The answer is no, because mineral source 11 appears twice (an even number of times) between city 33 and city 55 .
  • The second and the third queries are the same but they can choose different mineral resources. Both mineral resources 22 and 33 are available.
首页