A16343 | A Growing Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a rooted tree with the root at vertex $1$ , initially consisting of a single vertex. Each vertex has a numerical value, initially set to $0$ . There are also $q$ queries of two types:
- The first type: add a child vertex with the number $sz + 1$ to vertex $v$ , where $sz$ is the current size of the tree. The numerical value of the new vertex will be $0$ .
- The second type: add $x$ to the numerical values of all vertices in the subtree of vertex $v$ .
After all queries, output the numerical value of all of the vertices in the final tree.
- The first type: add a child vertex with the number $sz + 1$ to vertex $v$ , where $sz$ is the current size of the tree. The numerical value of the new vertex will be $0$ .
- The second type: add $x$ to the numerical values of all vertices in the subtree of vertex $v$ .
After all queries, output the numerical value of all of the vertices in the final tree.
输入格式
The first line contains a single integer $T$ ( $1 \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 $q$ ( $1 \leq q \leq 5 \cdot 10^5$ ) — the number of queries.
The following $q$ lines can fall into two cases:
- The first type of query: The $i$ -th line contains two integers $t_i$ ( $t_i = 1$ ), $v_i$ . You need to add a child with the number $sz + 1$ to vertex $v_i$ , where $sz$ is the current size of the tree. It is guaranteed that $1 \leq v_i \leq sz$ .
- The second type of query: The $i$ -th line contains three integers $t_i$ ( $t_i = 2$ ), $v_i$ , $x_i$ ( $-10^9 \leq x_i \leq 10^9$ ). You need to add $x_i$ to all numerical values of vertices in the subtree of $v_i$ . It is guaranteed that $1 \leq v_i \leq sz$ , where $sz$ is the current size of the tree.
It is guaranteed that the sum of $q$ across all test cases does not exceed $5 \cdot 10^5$ .
The first line of each test case contains a single integer $q$ ( $1 \leq q \leq 5 \cdot 10^5$ ) — the number of queries.
The following $q$ lines can fall into two cases:
- The first type of query: The $i$ -th line contains two integers $t_i$ ( $t_i = 1$ ), $v_i$ . You need to add a child with the number $sz + 1$ to vertex $v_i$ , where $sz$ is the current size of the tree. It is guaranteed that $1 \leq v_i \leq sz$ .
- The second type of query: The $i$ -th line contains three integers $t_i$ ( $t_i = 2$ ), $v_i$ , $x_i$ ( $-10^9 \leq x_i \leq 10^9$ ). You need to add $x_i$ to all numerical values of vertices in the subtree of $v_i$ . It is guaranteed that $1 \leq v_i \leq sz$ , where $sz$ is the current size of the tree.
It is guaranteed that the sum of $q$ across all test cases does not exceed $5 \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
 The final tree with the assigned numerical values
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted