F1. Omsk Metro (simple version)

发布时间 2023-09-04 21:48:06作者: onlyblues

F1. Omsk Metro (simple version)

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

Examples

input

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.

 

解题思路

  可以把点对$(u,v)$所构成的路径中的每一个点看作是一个区间中的元素,假设在所有连续子区间中,区间和的最小值是$l$,最大值是$r$,那么对于询问的$x$如果有$l \leq x \leq r$,则存在某个子区间的和为$x$。

  对我来说这个结论不太直观,总觉得$l \sim r$中可能会有某些整数不存在,下面给出证明。首先一定会存在一个和最小的子区间,对于其他的子区间,我们总是可以通过这个和最小的子区间往两端不断添加或删除元素来得到。由因为每一个元素的值不是$1$就是$-1$,因此每添加或删除一个元素得到的新的子区间的和与之前相比只会相差$1$,即子区间的和是连续变化的。所以我们总是可以通过连续变化的方式从区间和最小的子区间来得到区间和最大的子区间,因此$l \sim r$中的任意一个整数值都有相应的子区间与之对应。

  + 操作相当于往某个区间的末尾插入一个元素,因此问题就变成了如何动态维护某个区间的最大子区间和以及最小子区间和。对于最大子区间和可以类别最大连续子序列和问题。由于在这个问题中始终有$u = 1$,因此定义状态$f_1(v)$表示从$1$到$v$这条路径所对应的区间中,所有子区间和的最大值;$f_0(v)$表示从$1$到$v$这条路径所对应的区间中,以$v$为区间末端的最大区间和。因此有状态转移方程$f_0(v) = \max \{ 0, \, f_0(p_v) + x \}$,$f_1(v) = \max \{ f_1(p_v), \, f_0(v) \}$,其中$p_v$是$v$的父亲。对于最小子区间和同理可得。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 2e5 + 10;
 7 
 8 int f[N][2], g[N][2];
 9 
10 void solve() {
11     int n;
12     scanf("%d", &n);
13     int m = 1;
14     f[1][0] = f[1][1] = 1;
15     g[1][0] = g[1][1] = 0;
16     while (n--) {
17         char op[5];
18         int x, y, z;
19         scanf("%s %d %d", op, &x, &y);
20         if (op[0] == '+') {
21             m++;
22             f[m][0] = max(0, f[x][0] + y);
23             f[m][1] = max(f[x][1], f[m][0]);
24             g[m][0] = min(0, g[x][0] + y);
25             g[m][1] = min(g[x][1], g[m][0]);
26         }
27         else {
28             scanf("%d", &z);
29             printf("%s\n", z >= g[y][1] && z <= f[y][1] ? "YES" : "NO");
30         }
31     }
32 }
33 
34 int main() {
35     int t;
36     scanf("%d", &t);
37     while (t--) {
38         solve();
39     }
40     
41     return 0;
42 }

 

参考资料

  Codeforces Round #881 (Div. 3) Editorial:https://codeforces.com/blog/entry/117468