CF1891F.A Growing Tree

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a rooted tree with the root at vertex 11, initially consisting of a single vertex. Each vertex has a numerical value, initially set to 00. There are also qq queries of two types:

  • The first type: add a child vertex with the number sz+1sz + 1 to vertex vv, where szsz is the current size of the tree. The numerical value of the new vertex will be 00.
  • The second type: add xx to the numerical values of all vertices in the subtree of vertex vv.

After all queries, output the numerical value of all of the vertices in the final tree.

你将得到一棵以顶点 11 为根的有根树,初始时仅包含一个顶点。每个顶点都有一个数值,初始值均为 00。此外还有 qq 个查询,分为两种类型:

  • 第一种类型:向顶点 vv 添加一个子顶点,其编号为 sz+1sz + 1,其中 szsz 是当前树的大小(即顶点总数)。新顶点的数值为 00。
  • 第二种类型:将 xx 加到顶点 vv 的子树中所有顶点的数值上。

执行完所有查询后,输出最终树中所有顶点的数值。

输入格式

The first line contains a single integer TT (1≤T≤1041 \leq T \leq 10^4) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer qq (1≤q≤5⋅1051 \leq q \leq 5 \cdot 10^5) — the number of queries.

The following qq lines can fall into two cases:

  • The first type of query: The ii-th line contains two integers tit_i (ti=1t_i = 1), viv_i. You need to add a child with the number sz+1sz + 1 to vertex viv_i, where szsz is the current size of the tree. It is guaranteed that 1≤vi≤sz1 \leq v_i \leq sz.
  • The second type of query: The ii-th line contains three integers tit_i (ti=2t_i = 2), viv_i, xix_i (−109≤xi≤109-10^9 \leq x_i \leq 10^9). You need to add xix_i to all numerical values of vertices in the subtree of viv_i. It is guaranteed that 1≤vi≤sz1 \leq v_i \leq sz, where szsz is the current size of the tree.

It is guaranteed that the sum of qq across all test cases does not exceed 5⋅1055 \cdot 10^5.

第一行包含一个整数 TT(1≤T≤1041 \leq T \leq 10^4)——测试用例的数量。接下来是各测试用例的描述。

每个测试用例的第一行包含一个整数 qq(1≤q≤5⋅1051 \leq q \leq 5 \cdot 10^5)——查询次数。

接下来的 qq 行分为以下两类:

  • 第一类查询:第 ii 行包含两个整数 tit_i(ti=1t_i = 1)和 viv_i。你需要向顶点 viv_i 添加一个编号为 sz+1sz + 1 的子节点,其中 szsz 是当前树的大小。保证 1≤vi≤sz1 \leq v_i \leq sz。
  • 第二类查询:第 ii 行包含三个整数 tit_i(ti=2t_i = 2)、viv_i 和 xix_i(−109≤xi≤109-10^9 \leq x_i \leq 10^9)。你需要将 xix_i 加到顶点 viv_i 的子树中所有顶点的数值上。保证 1≤vi≤sz1 \leq v_i \leq sz,其中 szsz 是当前树的大小。

保证所有测试用例中 qq 的总和不超过 5⋅1055 \cdot 10^5。

输出格式

For each test case, output the numerical value of each vertex of the final tree after all queries have been performed.

对于每个测试用例,输出执行完所有查询后最终树中每个顶点的数值。

输入输出样例

  • 输入#1

    3
    9
    2 1 3
    1 1
    2 2 1
    1 1
    2 3 2
    1 3
    2 1 4
    1 3
    2 3 2
    5
    2 1 1
    1 1
    2 1 -1
    1 1
    2 1 1
    5
    1 1
    1 1
    2 1 1
    2 1 3
    2 2 10

    输出#1

    7 5 8 6 2 
    1 0 1 
    4 14 4

说明/提示

In the first case, the final tree with the assigned numerical values will look like this:

The final tree with the assigned numerical values

在第一种情况下,分配数值后的最终树形结构如下所示:

分配数值后的最终树形结构

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

首页