310505: CF1843F2. Omsk Metro (hard version)
Description
This is the hard version of the problem. The only difference between the simple and hard versions is that in this version $u$ can take any possible value.
As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree.
To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight $x \in \{-1, 1\}$. If the station has a weight of $-1$, then when the station is visited by an Omsk resident, a fee of $1$ burle is charged. If the weight of the station is $1$, then the Omsk resident is rewarded with $1$ burle.
Omsk Metro currently has only one station with number $1$ and weight $x = 1$. Every day, one of the following events occurs:
- A new station with weight $x$ is added to the station with number $v_i$, and it is assigned a number that is one greater than the number of existing stations.
- Alex, who lives in Omsk, wonders: is there a subsegment$\dagger$ (possibly empty) of the path between vertices $u$ and $v$ such that, by traveling along it, exactly $k$ burles can be earned (if $k < 0$, this means that $k$ burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to $k$. Note that the subsegment can be empty, and then the sum is equal to $0$.
You are a friend of Alex, so your task is to answer Alex's questions.
$\dagger$Subsegment — continuous sequence of elements.
InputThe first line contains a single number $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the number $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of events.
Then there are $n$ lines describing the events. In the $i$-th line, one of the following options is possible:
- First comes the symbol "+" (without quotes), then two numbers $v_i$ and $x_i$ ($x_i \in \{-1, 1\}$, it is also guaranteed that the vertex with number $v_i$ exists). In this case, a new station with weight $x_i$ is added to the station with number $v_i$.
- First comes the symbol "?" (without quotes), and then three numbers $u_i$, $v_i$, and $k_i$ ($-n \le k_i \le n$). It is guaranteed that the vertices with numbers $u_i$ and $v_i$ exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations $u_i$ and $v_i$ with a sum of weights exactly equal to $k_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
OutputFor each of Alex's questions, output "Yes" (without quotes) if the subsegment described in the condition exists, otherwise output "No" (without quotes).
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
ExamplesInput1 8 + 1 -1 ? 1 1 2 ? 1 2 1 + 1 1 ? 1 3 -1 ? 1 1 1 ? 1 3 2 ? 1 1 0Output
NO YES NO YES YES YESInput
1 7 + 1 -1 + 2 -1 + 2 1 + 3 -1 ? 5 2 2 ? 3 1 -1 ? 5 4 -3Output
NO YES YESNote
Explanation of the first sample.
The answer to the second question is "Yes", because there is a path $1$.
In the fourth question, we can choose the $1$ path again.
In the fifth query, the answer is "Yes", since there is a path $1-3$.
In the sixth query, we can choose an empty path because the sum of the weights on it is $0$.
It is not difficult to show that there are no paths satisfying the first and third queries.
Input
题意翻译
$t$ 组数据,每组数据有 $n$ 次询问。 初始只有有节点 $1$,权值为 $1$,记当前节点数为 $cnt$,初值为 $1$。 每个 `+ v x` 询问会在节点 $v$ 下增加一个节点(编号为 $cnt+1$,只与 $v$ 有连边,同时 $cnt\leftarrow cnt+1$),权值为 $x\in\{-1,1\}$; 每个 `? x y k` 询问需要你将 $x,y$ 之间的路径节点权值序列取出,求该序列是否存在和为 $k$ 的子段(可以是空子段,此时和为 $0$),存在输出 `YES`,不存在输出 `NO`。 $t\le 10^4,\sum n\le 2\times10^5$。Output
这个问题是“Omsk地铁”难题版本。简单版本和困难版本之间的唯一区别在于,在这个版本中,u可以取任何可能的值。
正如所知,Omsk是Berland的首都。像任何首都一样,Omsk有一个发达的地铁系统。Omsk地铁由一定数量的车站通过隧道连接而成,并且任意两个车站之间恰好有一条路径,每个隧道都不超过一次。换句话说,地铁是一棵树。
为了发展地铁并吸引居民,Omsk使用了以下系统。每个站点都有自己的权重x∈{-1,1}。如果站点的权重为-1,则当Omsk居民访问该站点时,将收取1个布尔的费用。如果站点的权重为1,则Omsk居民将获得1个布尔的奖励。
Omsk地铁目前只有一个编号为1、权重为x = 1的车站。每天,会发生以下事件之一:
- 一个新的车站与权重x连接到编号为v_i的车站,并分配一个比现有车站数量大一的编号。
- Alex住在Omsk,想知道:在顶点u和v之间的路径上是否存在这样一个子段†(可能为空),以至于沿着它旅行正好可以获得k个布尔(如果k < 0,这意味着旅行将花费k个布尔)。换句话说,Alex对是否存在这样一个子段感兴趣,该子段的顶点权重之和等于k†。注意,子段可以是空的,然后总和等于0。
你是Alex的朋友,所以你的任务是回答Alex的问题。
输入输出数据格式:
输入:
第一行包含一个数字t(1≤t≤10^4)——测试用例的数量。
每个测试用例的第一行包含一个数字n(1≤n≤2 * 10^5)——事件的数量。
然后有n行描述事件。在第i行中,可能出现以下选项之一:
- 首先,出现符号“+”(不带引号),然后是两个数字v_i和x_i(x_i∈{-1,1},还保证存在编号为v_i的顶点)。在这种情况下,一个新的车站与权重x_i连接到编号为v_i的车站。
- 首先,出现符号“?”(不带引号),然后是三个数字u_i、v_i和k_i(-n≤k_i≤n)。保证存在编号为u_i和v_i的顶点。在这种情况下,需要确定是否在车站u_i和v_i之间的路径上存在一个子段(可能为空),其权重之和正好等于k_i。
保证所有测试用例的n之和不超过2 * 10^5。
输出:
对于Alex的每个问题,如果存在条件中描述的子段,则输出“Yes”(不带引号),否则输出“No”(不带引号)。
您可以在任何情况下输出答案(例如,字符串“yEs”、“yes”、“Yes”和“YES”将被识别为肯定答案)。
†子段——连续元素序列。题目大意: 这个问题是“Omsk地铁”难题版本。简单版本和困难版本之间的唯一区别在于,在这个版本中,u可以取任何可能的值。 正如所知,Omsk是Berland的首都。像任何首都一样,Omsk有一个发达的地铁系统。Omsk地铁由一定数量的车站通过隧道连接而成,并且任意两个车站之间恰好有一条路径,每个隧道都不超过一次。换句话说,地铁是一棵树。 为了发展地铁并吸引居民,Omsk使用了以下系统。每个站点都有自己的权重x∈{-1,1}。如果站点的权重为-1,则当Omsk居民访问该站点时,将收取1个布尔的费用。如果站点的权重为1,则Omsk居民将获得1个布尔的奖励。 Omsk地铁目前只有一个编号为1、权重为x = 1的车站。每天,会发生以下事件之一: - 一个新的车站与权重x连接到编号为v_i的车站,并分配一个比现有车站数量大一的编号。 - Alex住在Omsk,想知道:在顶点u和v之间的路径上是否存在这样一个子段†(可能为空),以至于沿着它旅行正好可以获得k个布尔(如果k < 0,这意味着旅行将花费k个布尔)。换句话说,Alex对是否存在这样一个子段感兴趣,该子段的顶点权重之和等于k†。注意,子段可以是空的,然后总和等于0。 你是Alex的朋友,所以你的任务是回答Alex的问题。 输入输出数据格式: 输入: 第一行包含一个数字t(1≤t≤10^4)——测试用例的数量。 每个测试用例的第一行包含一个数字n(1≤n≤2 * 10^5)——事件的数量。 然后有n行描述事件。在第i行中,可能出现以下选项之一: - 首先,出现符号“+”(不带引号),然后是两个数字v_i和x_i(x_i∈{-1,1},还保证存在编号为v_i的顶点)。在这种情况下,一个新的车站与权重x_i连接到编号为v_i的车站。 - 首先,出现符号“?”(不带引号),然后是三个数字u_i、v_i和k_i(-n≤k_i≤n)。保证存在编号为u_i和v_i的顶点。在这种情况下,需要确定是否在车站u_i和v_i之间的路径上存在一个子段(可能为空),其权重之和正好等于k_i。 保证所有测试用例的n之和不超过2 * 10^5。 输出: 对于Alex的每个问题,如果存在条件中描述的子段,则输出“Yes”(不带引号),否则输出“No”(不带引号)。 您可以在任何情况下输出答案(例如,字符串“yEs”、“yes”、“Yes”和“YES”将被识别为肯定答案)。 †子段——连续元素序列。