CF2236G.Criterion in Burlandia
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Regions in Burlandia form a graph with n vertices and n−1 edges, and there is exactly one path between any two vertices. Formally, the regions form a tree.
Each region has a friendliness value ai.
There are q queries. In each query, a pair of friends located in different regions is given. They want to know how many subsegments of the path between these regions are hospitable.
It is known that in Burlandia there are two criteria for evaluating relationships — XOR and sum. A subsegment of the path, containing some vertices lying on the path from region x to region y, is called hospitable if it is non-empty and the sum of friendliness values on this subsegment does not exceed their XOR.
More formally, for each query you are given two vertices x and y (x=y). Consider the shortest path from vertex x to vertex y in the tree. Let the vertices v1,v2,…,vk form this path, where v1=x, vk=y. You need to find the number of subsegments of this path for which the following condition holds: $$ a_{v_{l}} \oplus a_{v_{l+1}} \oplus \ldots \oplus a_{v_{r}} \geq (a_{v_{l}} + a_{v_{l+1}} + \ldots + a_{v_{r}}), $$ where 1≤l≤r≤k — the boundaries of the subsegment of vertices on the path from x to y.
Burlandia 的各个区域构成一个包含 n 个顶点和 n−1 条边的图,且任意两个顶点之间恰好存在一条路径。形式上,这些区域构成一棵树。
每个区域具有一个友好度值 ai。
共有 q 个查询。在每次查询中,给出一对位于不同区域的朋友。他们希望知道:连接这两个区域的路径上,有多少个子段是“宜人”的。
已知 Burlandia 中存在两种关系评估标准——异或(XOR) 和求和。若某路径子段(即路径上若干连续顶点)非空,且该子段上所有顶点的友好度之和不超过其异或值,则称该子段为“宜人”。
更形式化地,对每个查询,给定两个顶点 x 和 y(x=y)。考虑树中从顶点 x 到顶点 y 的最短路径。设该路径由顶点 v1,v2,…,vk 构成,其中 v1=x,vk=y。你需要计算满足如下条件的子段数量:
avl⊕avl+1⊕…⊕avr≥(avl+avl+1+…+avr),
其中 1≤l≤r≤k 表示路径上从 x 到 y 的顶点子段的端点。
输入格式
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Then the descriptions of the test cases follow.
The first line of each test case contains integers n (2≤n≤105) — the number of vertices in the tree, and q (1≤q≤105) — the number of queries.
The second line contains an array of n non-negative integers — the friendliness values of the regions (0≤ai<220).
The next n−1 lines describe the edges of the tree: each line contains integers u,v (1≤u,v≤n) — an edge.
Then q lines follow describing the queries. Each query is given by integers x,y (1≤x,y≤n, x=y) — the vertices that define the path for which you need to count the number of hospitable subsegments.
It is guaranteed that the sum of n and the sum of q over all test cases do not exceed 105, and that the edges indeed form a tree.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n(2≤n≤105,表示树中顶点的数量)和 q(1≤q≤105,表示查询的数量)。
第二行包含一个由 n 个非负整数组成的数组——各区域的友好度值(0≤ai<220)。
接下来的 n−1 行描述树的边:每行包含两个整数 u,v(1≤u,v≤n),表示一条边。
随后是 q 行,每行描述一个查询。每个查询由两个整数 x,y(1≤x,y≤n,且 x=y)给出,表示需统计其路径上“宜人子段”数量的两个顶点。
保证所有测试用例中 n 的总和与 q 的总和均不超过 105,且所给边确实构成一棵树。
输出格式
For each query output an answer on a separate line.
对于每个查询,在单独的一行上输出答案。
输入输出样例
输入#1
3 4 3 0 0 4 1 1 2 1 3 1 4 1 4 2 3 2 4 4 3 0 4 1 2 1 3 1 4 2 4 1 2 2 3 2 4 4 3 3 2 4 4 1 2 2 4 3 4 1 2 1 3 2 3
输出#1
3 6 6 6 10 3 2 5 4
说明/提示
For clarity, consider the third query from the third sample.
The path from vertex 2 to vertex 3 consists of vertices 2,4,3.
The subsegment [2;3] does not satisfy the condition, since the XOR on it is a4⊕a3=4⊕4=0, while the sum is a4+a3=4+4=8.
The subsegment [1;3] does not satisfy the condition, since the XOR on it is a2⊕a4⊕a3=2⊕4⊕4=2, while the sum is a2+a4+a3=2+4+4=10.
It can be shown that all other 4 subsegments satisfy the condition.
For each query, output the answer in a separate line.
为清晰起见,考虑第三个样例中的第三次查询。
从顶点 2 到顶点 3 的路径包含顶点 2,4,3。
子段 [2;3] 不满足条件,因为其异或值为 a4⊕a3=4⊕4=0,而其和为 a4+a3=4+4=8。
子段 [1;3] 不满足条件,因为其异或值为 a2⊕a4⊕a3=2⊕4⊕4=2,而其和为 a2+a4+a3=2+4+4=10。
可以证明,其余全部 4 个子段均满足条件。
对每次查询,将答案单独输出在一行中。
输入解题思路,AI测评打分。不知道怎么写?