CF1479D.Odd Mineral Resource

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:1024MB

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 (n−1)(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
  • li≤ci≤ril_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.

在荷马的国家中,有 nn 座城市,编号为 11 到 nn,它们构成一棵树。也就是说,这 nn 座城市之间有 (n−1)(n-1) 条无向道路,且任意两座城市均可通过这些道路相互到达。

荷马的国家是一个工业国,其中每座城市都蕴藏某种矿产资源。城市 ii 的矿产资源标记为 aia_i。

荷马获得了该国未来 qq 年的发展规划。第 ii 年的规划由四个参数 ui,vi,liu_i, v_i, l_i 和 rir_i 描述,他需要找出任意一种矿产资源 cic_i,使得以下两个条件同时成立:

  • 矿产资源 cic_i 在城市 uiu_i 与城市 viv_i 之间的路径上出现奇数次;
  • li≤ci≤ril_i \leq c_i \leq r_i。

作为荷马最好的朋友,他向你求助。对每一项规划,请找出满足条件的任意一个矿产资源 cic_i;若不存在这样的资源,则告知他不存在。

输入格式

The first line contains two integers nn (1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5) and qq (1≤q≤3⋅1051 \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 (1≤ai≤n1 \leq a_i \leq n).

Then the ii-th line of the following (n−1)(n-1) lines contains two integers xix_i and yiy_i (1≤xi,yi≤n1 \leq x_i, y_i \leq n) with xi≠yix_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 (1≤ui≤n1 \leq u_i \leq n, 1≤vi≤n1 \leq v_i \leq n, 1≤li≤ri≤n1 \leq l_i \leq r_i \leq n), indicating the plan of the ii-th year.

第一行包含两个整数 nn(1≤n≤3⋅1051 \leq n \leq 3 \cdot 10^5)和 qq(1≤q≤3⋅1051 \leq q \leq 3 \cdot 10^5),分别表示城市的数量和计划的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \leq a_i \leq n)。

接下来的 (n−1)(n-1) 行中,第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi,yi≤n1 \leq x_i, y_i \leq n),满足 xi≠yix_i \neq y_i,表示城市 xix_i 与城市 yiy_i 之间存在一条双向道路。保证所给道路构成一棵树。

再接下来的 qq 行中,第 ii 行包含四个整数 uiu_i、viv_i、lil_i、rir_i(1≤ui≤n1 \leq u_i \leq n,1≤vi≤n1 \leq v_i \leq n,1≤li≤ri≤n1 \leq l_i \leq r_i \leq n),表示第 ii 年的计划。

输出格式

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.

输出 qq 行,其中第 ii 行包含一个整数 cic_i,满足以下条件:

  • 若不存在满足要求的矿产资源,则 ci=−1c_i = {-1};
  • 否则,cic_i 为第 ii 年所选矿产资源的编号。所选矿产资源 cic_i 必须满足题目描述中第 ii 年所规定的上述条件。若存在多个满足条件的 cic_i,可任选其一输出。

输入输出样例

  • 输入#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.

在前三次查询中,城市 33 与城市 55 之间共有四座城市,即城市 11、城市 22、城市 33 和城市 55。这些城市中出现的矿产资源包括:矿产资源 11(出现在城市 33 和城市 55 中)、矿产资源 22(出现在城市 22 中)以及矿产资源 33(出现在城市 11 中)。需注意:

  • 第一次查询仅需判断矿产资源 11 在城市 33 与城市 55 之间是否出现了奇数次。答案是否定的,因为矿产资源 11 在城市 33 与城市 55 之间共出现了两次(偶数次)。
  • 第二次与第三次查询内容相同,但可选择不同的矿产资源。矿产资源 22 与矿产资源 33 均可选用。

输入解题思路,AI测评打分。不知道怎么写?

首页