310504: CF1843F1. Omsk Metro (simple version)

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

F1. Omsk Metro (simple version)time limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

This is the simple version of the problem. The only difference between the simple and hard versions is that in this version $u = 1$.

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.

Input

The 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$. In this version of the task, it is guaranteed that $u_i = 1$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For 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).

ExamplesInput
1
8
+ 1 -1
? 1 1 2
? 1 2 1
+ 1 1
? 1 3 -1
? 1 1 1
? 1 3 2
? 1 1 0
Output
NO
YES
NO
YES
YES
YES
Input
1
10
+ 1 -1
+ 1 -1
+ 3 1
+ 3 -1
+ 3 1
? 1 6 -1
? 1 6 2
? 1 2 0
? 1 5 -2
? 1 4 3
Output
YES
NO
YES
YES
NO
Note

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

题意翻译

有一棵节点带权的树,权重只可能为 $1$ 或者 $-1$ ,初始时树中只有一个权重为 $1$ 的根节点 $1$ ,然后进行 $n$ 次操作,操作 `+` 表示添加一个权重为 $x_i$ 的新节点和 $v_i$ 相连,新节点的编号按添加顺序分配;操作 `?` 表示查询 $v_i$ 到 $u_i$ 的路径上,是否存在一个可以为空的子路径的权重和为 $k_i$ 。 其中 F1 保证 $u_i = 1$ 。

Output

**题目大意:**

这个问题是关于奥姆斯克地铁系统的简化版本。在这个版本中,地铁系统可以看作是一棵树,每个站点都有一个权重,为-1或1。当居民访问权重为-1的站点时,需要支付1布列;访问权重为1的站点时,会获得1布列的奖励。地铁最初只有一个站点,编号为1,权重为1。每天会发生以下两种事件之一:

1. 在某个已有站点下新增一个站点,新站点的编号比现有站点的数量大1。
2. 居民Alex想知道,在两个站点u和v之间的路径上,是否存在一个子段(可能为空),使得沿着这个子段行进可以正好获得k布列(如果k<0,则意味着需要花费k布列)。换句话说,Alex想了解是否存在这样一个子段,其站点权重的总和等于k。

任务是回答Alex的问题。

**输入数据格式:**

输入包含多个测试用例。每个测试用例的第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。每个测试用例的第一行包含一个整数n(1≤n≤2×10^5),表示事件的数量。接下来n行描述事件,每个事件可能是以下两种格式之一:

1. 一个"+"符号,后跟两个整数v_i和x_i(x_i为-1或1,保证编号为v_i的站点存在)。这表示在编号为v_i的站点下新增一个权重为x_i的站点。
2. 一个"?"符号,后跟三个整数u_i、v_i和k_i(-n≤k_i≤n,保证编号为u_i和v_i的站点存在)。这表示询问是否存在一个从u_i到v_i的子段,其站点权重的总和正好等于k_i。在这个任务版本中,保证u_i=1。

**输出数据格式:**

对于每个Alex的问题,如果存在这样的子段,输出"Yes"(不包含引号),否则输出"No"(不包含引号)。输出大小写不敏感。

**示例:**

**输入:**
```
1
8
+ 1 -1
? 1 1 2
? 1 2 1
+ 1 1
? 1 3 -1
? 1 1 1
? 1 3 2
? 1 1 0
```

**输出:**
```
NO
YES
NO
YES
YES
YES
```

**注意:**

- 地铁系统是一个无环连通图,可以看作是一棵树。
- 每个站点的权重为-1或1。
- 子段是指路径上的连续序列,可以为空。
-Alex的问题是在询问是否存在一个子段,其站点权重的总和等于k_i。**题目大意:** 这个问题是关于奥姆斯克地铁系统的简化版本。在这个版本中,地铁系统可以看作是一棵树,每个站点都有一个权重,为-1或1。当居民访问权重为-1的站点时,需要支付1布列;访问权重为1的站点时,会获得1布列的奖励。地铁最初只有一个站点,编号为1,权重为1。每天会发生以下两种事件之一: 1. 在某个已有站点下新增一个站点,新站点的编号比现有站点的数量大1。 2. 居民Alex想知道,在两个站点u和v之间的路径上,是否存在一个子段(可能为空),使得沿着这个子段行进可以正好获得k布列(如果k<0,则意味着需要花费k布列)。换句话说,Alex想了解是否存在这样一个子段,其站点权重的总和等于k。 任务是回答Alex的问题。 **输入数据格式:** 输入包含多个测试用例。每个测试用例的第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。每个测试用例的第一行包含一个整数n(1≤n≤2×10^5),表示事件的数量。接下来n行描述事件,每个事件可能是以下两种格式之一: 1. 一个"+"符号,后跟两个整数v_i和x_i(x_i为-1或1,保证编号为v_i的站点存在)。这表示在编号为v_i的站点下新增一个权重为x_i的站点。 2. 一个"?"符号,后跟三个整数u_i、v_i和k_i(-n≤k_i≤n,保证编号为u_i和v_i的站点存在)。这表示询问是否存在一个从u_i到v_i的子段,其站点权重的总和正好等于k_i。在这个任务版本中,保证u_i=1。 **输出数据格式:** 对于每个Alex的问题,如果存在这样的子段,输出"Yes"(不包含引号),否则输出"No"(不包含引号)。输出大小写不敏感。 **示例:** **输入:** ``` 1 8 + 1 -1 ? 1 1 2 ? 1 2 1 + 1 1 ? 1 3 -1 ? 1 1 1 ? 1 3 2 ? 1 1 0 ``` **输出:** ``` NO YES NO YES YES YES ``` **注意:** - 地铁系统是一个无环连通图,可以看作是一棵树。 - 每个站点的权重为-1或1。 - 子段是指路径上的连续序列,可以为空。 -Alex的问题是在询问是否存在一个子段,其站点权重的总和等于k_i。

加入题单

上一题 下一题 算法标签: