F1. Omsk Metro (simple version)

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{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 vi, 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 (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.

Subsegment — continuous sequence of elements.

Input

The first line contains a single number t (1t104) — the number of test cases.

The first line of each test case contains the number n (1n2105) — 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 vi and xi (xi{1,1}, it is also guaranteed that the vertex with number vi exists). In this case, a new station with weight xi is added to the station with number vi.
  • First comes the symbol "?" (without quotes), and then three numbers ui, vi, and ki (nkin). It is guaranteed that the vertices with numbers ui and vi exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations ui and vi with a sum of weights exactly equal to ki. In this version of the task, it is guaranteed that ui=1.

It is guaranteed that the sum of n over all test cases does not exceed 2105.

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

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如果有lxr,则存在某个子区间的和为x

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

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

  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

posted @   onlyblues  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-09-04 Maximum Number of Robots Within Budget
2022-09-04 串联数字
2022-09-04 列表排序
Web Analytics
点击右上角即可分享
微信分享提示