CF1479D.Odd Mineral Resource
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In Homer's country, there are n cities numbered 1 to n and they form a tree. That is, there are (n−1) undirected roads between these n cities and every two cities can reach each other through these roads.
Homer's country is an industrial country, and each of the n cities in it contains some mineral resource. The mineral resource of city i is labeled ai .
Homer is given the plans of the country in the following q years. The plan of the i -th year is described by four parameters ui,vi,li and ri , and he is asked to find any mineral resource ci such that the following two conditions hold:
- mineral resource ci appears an odd number of times between city ui and city vi ; and
- li≤ci≤ri .
As the best friend of Homer, he asks you for help. For every plan, find any such mineral resource ci , or tell him that there doesn't exist one.
输入格式
The first line contains two integers n ( 1≤n≤3⋅105 ) and q ( 1≤q≤3⋅105 ), indicating the number of cities and the number of plans.
The second line contains n integers a1,a2,…,an ( 1≤ai≤n ).
Then the i -th line of the following (n−1) lines contains two integers xi and yi ( 1≤xi,yi≤n ) with xi=yi , indicating that there is a bidirectional road between city xi and city yi . It is guaranteed that the given roads form a tree.
Then the i -th line of the following q lines contains four integers ui , vi , li , ri ( 1≤ui≤n , 1≤vi≤n , 1≤li≤ri≤n ), indicating the plan of the i -th year.
输出格式
Print q lines, the i -th of which contains an integer ci such that
- ci=−1 if there is no such mineral resource that meets the required condition; or
- ci is the label of the chosen mineral resource of the i -th year. The chosen mineral resource ci should meet those conditions in the i -th year described above in the problem statement. If there are multiple choices of ci , 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 3 and city 5 , which are city 1 , city 2 , city 3 and city 5 . The mineral resources appear in them are mineral resources 1 (appears in city 3 and city 5 ), 2 (appears in city 2 ) and 3 (appears in city 1 ). It is noted that
- The first query is only to check whether mineral source 1 appears an odd number of times between city 3 and city 5 . The answer is no, because mineral source 1 appears twice (an even number of times) between city 3 and city 5 .
- The second and the third queries are the same but they can choose different mineral resources. Both mineral resources 2 and 3 are available.