CF1843F2.Omsk Metro (hard version)

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The only difference between the simple and hard versions is that in this version uu 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∈−1,1x \in {-1, 1}. If the station has a weight of −1-1, then when the station is visited by an Omsk resident, a fee of 11 burle is charged. If the weight of the station is 11, then the Omsk resident is rewarded with 11 burle.

Omsk Metro currently has only one station with number 11 and weight x=1x = 1. Every day, one of the following events occurs:

  • A new station with weight xx is added to the station with number viv_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 uu and vv such that, by traveling along it, exactly kk burles can be earned (if k<0k \lt 0, this means that kk 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 kk. Note that the subsegment can be empty, and then the sum is equal to 00.

You are a friend of Alex, so your task is to answer Alex's questions.

†\daggerSubsegment — continuous sequence of elements.

这是该问题的困难版本。简单版本与困难版本的唯一区别在于:在本版本中,uu 可取任意可能的值。

众所周知,鄂木斯克(Omsk)是贝尔兰(Berland)的首都。如同任何首都一样,鄂木斯克拥有发达的地铁系统。鄂木斯克地铁由若干车站通过隧道连接而成,且任意两个车站之间恰好存在一条路径,该路径至多经过每条隧道一次。换言之,该地铁系统构成一棵树。

为发展地铁并吸引市民,鄂木斯克采用如下机制:每个车站具有一个权值 x∈{−1,1}x \in \{-1, 1\}。若某车站权值为 −1-1,则当鄂木斯克市民访问该站时,需缴纳 11 卢布(burle)费用;若权值为 11,则该市民将获得 11 卢布奖励。

目前,鄂木斯克地铁仅有一个编号为 11 的车站,其权值 x=1x = 1。每天发生以下事件之一:

  • 向编号为 viv_i 的车站添加一个权值为 xx 的新车站,该新车站的编号为当前已有车站总数加 11;
  • 居住在鄂木斯克的 Alex 提出疑问:在顶点 uu 与 vv 之间的路径上,是否存在一个子段†\dagger(可以为空),使得沿该子段行走恰好可赚取 kk 卢布(若 k<0k < 0,则表示需花费 ∣k∣|k| 卢布)。换言之,Alex 想知道:该路径上是否存在某个子段,使其所包含顶点的权值之和恰好等于 kk。注意:子段可以为空,此时其权值之和为 00。

作为 Alex 的朋友,你的任务就是回答 Alex 的这些询问。

†\dagger子段——元素的连续序列。

输入格式

The first line contains a single number tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains the number nn (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the number of events.

Then there are nn lines describing the events. In the ii-th line, one of the following options is possible:

  • First comes the symbol "+" (without quotes), then two numbers viv_i and xix_i (xi∈−1,1x_i \in {-1, 1}, it is also guaranteed that the vertex with number viv_i exists). In this case, a new station with weight xix_i is added to the station with number viv_i.
  • First comes the symbol "?" (without quotes), and then three numbers uiu_i, viv_i, and kik_i (−n≤ki≤n-n \le k_i \le n). It is guaranteed that the vertices with numbers uiu_i and viv_i exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations uiu_i and viv_i with a sum of weights exactly equal to kik_i.

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

第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4)——测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)——事件的数量。

接下来有 nn 行,每行描述一个事件。在第 ii 行中,可能出现以下两种情况之一:

  • 首先是一个符号 "+"(不带引号),随后是两个整数 viv_i 和 xix_i(其中 xi∈{−1,1}x_i \in \{-1, 1\},且保证编号为 viv_i 的站点存在)。此时,向编号为 viv_i 的站点添加一个权值为 xix_i 的新站点。
  • 首先是一个符号 "?"(不带引号),随后是三个整数 uiu_i、viv_i 和 kik_i(其中 −n≤ki≤n-n \le k_i \le n)。保证编号为 uiu_i 和 viv_i 的站点存在。此时,需要判断:在站点 uiu_i 与 viv_i 之间的路径上,是否存在一个子段(可以为空),其权值之和恰好等于 kik_i。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

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

对于 Alex 的每个问题,如果满足条件的子段存在,则输出 “Yes”(不带引号),否则输出 “No”(不带引号)。

您可以以任意大小写形式输出答案(例如,字符串 “yEs”、“yes”、“Yes” 和 “YES” 均会被识别为肯定回答)。

输入输出样例

  • 输入#1

    1
    8
    + 1 -1
    ? 1 1 2
    ? 1 2 1
    + 1 1
    ? 1 3 -1
    ? 1 1 1
    ? 1 3 2
    ? 1 1 0

    输出#1

    NO
    YES
    NO
    YES
    YES
    YES
  • 输入#2

    1
    7
    + 1 -1
    + 2 -1
    + 2 1
    + 3 -1
    ? 5 2 2
    ? 3 1 -1
    ? 5 4 -3

    输出#2

    NO
    YES
    YES

说明/提示

Explanation of the first sample.

The answer to the second question is "Yes", because there is a path 11.

In the fourth question, we can choose the 11 path again.

In the fifth query, the answer is "Yes", since there is a path 1−31-3.

In the sixth query, we can choose an empty path because the sum of the weights on it is 00.

It is not difficult to show that there are no paths satisfying the first and third queries.

第一个样例的解释。

第二个问题的答案是“是”,因为存在路径 11。

在第四个问题中,我们可以再次选择路径 11。

在第五个查询中,答案是“是”,因为存在路径 1−31-3。

在第六个查询中,我们可以选择空路径,因为其上边权之和为 00。

不难证明,不存在满足第一个和第三个查询的路径。

输入解题思路,AI测评打分。不知道怎么写?

首页