A10098 | Power Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Genos and Saitama went shopping for Christmas trees. However, a different type of tree caught their attention, the exalted Power Tree.
A Power Tree starts out as a single root vertex indexed $1$ . A Power Tree grows through a magical phenomenon known as an update. In an update, a single vertex is added to the tree as a child of some other vertex.
Every vertex in the tree (the root and all the added vertices) has some value $v_{i}$ associated with it. The power of a vertex is defined as the strength of the multiset composed of the value associated with this vertex ( $v_{i}$ ) and the powers of its direct children. The strength of a multiset is defined as the sum of all elements in the multiset multiplied by the number of elements in it. Or in other words for some multiset $S$ :
Saitama knows the updates that will be performed on the tree, so he decided to test Genos by asking him queries about the tree during its growth cycle.
An update is of the form $1 p v$ , and adds a new vertex with value $v$ as a child of vertex $p$ .
A query is of the form $2 u$ , and asks for the power of vertex $u$ .
Please help Genos respond to these queries modulo $10^{9}+7$ .
A Power Tree starts out as a single root vertex indexed $1$ . A Power Tree grows through a magical phenomenon known as an update. In an update, a single vertex is added to the tree as a child of some other vertex.
Every vertex in the tree (the root and all the added vertices) has some value $v_{i}$ associated with it. The power of a vertex is defined as the strength of the multiset composed of the value associated with this vertex ( $v_{i}$ ) and the powers of its direct children. The strength of a multiset is defined as the sum of all elements in the multiset multiplied by the number of elements in it. Or in other words for some multiset $S$ :
Saitama knows the updates that will be performed on the tree, so he decided to test Genos by asking him queries about the tree during its growth cycle.
An update is of the form $1 p v$ , and adds a new vertex with value $v$ as a child of vertex $p$ .
A query is of the form $2 u$ , and asks for the power of vertex $u$ .
Please help Genos respond to these queries modulo $10^{9}+7$ .
输入格式
The first line of the input contains two space separated integers $v_{1}$ and $q$ ( $1<=v_{1}<10^{9}$ , $1<=q<=200000$ ) — the value of vertex $1$ and the total number of updates and queries respectively.
The next $q$ lines contain the updates and queries. Each of them has one of the following forms:
- $1 p_{i} v_{i}$ , if these line describes an update. The index of the added vertex is equal to the smallest positive integer not yet used as an index in the tree. It is guaranteed that $p_{i}$ is some already existing vertex and $1<=v_{i}<10^{9}$ .
- $2 u_{i}$ , if these line describes a query. It is guaranteed $u_{i}$ will exist in the tree.
It is guaranteed that the input will contain at least one query.
The next $q$ lines contain the updates and queries. Each of them has one of the following forms:
- $1 p_{i} v_{i}$ , if these line describes an update. The index of the added vertex is equal to the smallest positive integer not yet used as an index in the tree. It is guaranteed that $p_{i}$ is some already existing vertex and $1<=v_{i}<10^{9}$ .
- $2 u_{i}$ , if these line describes a query. It is guaranteed $u_{i}$ will exist in the tree.
It is guaranteed that the input will contain at least one query.
输出格式
For each query, print out the power of the given vertex modulo $10^{9}+7$ .
输入输出样例
输入 #1
2 5 1 1 3 1 2 5 1 3 7 1 4 11 2 1
输出 #1
344
输入 #2
5 5 1 1 4 1 2 3 2 2 1 2 7 2 1
输出 #2
14 94
For the first sample case, after all the updates the graph will have vertices labelled in the following manner: 1 — 2 — 3 — 4 — 5
These vertices will have corresponding values: 2 — 3 — 5 — 7 — 11
And corresponding powers: 344 — 170 — 82 — 36 — 11
These vertices will have corresponding values: 2 — 3 — 5 — 7 — 11
And corresponding powers: 344 — 170 — 82 — 36 — 11
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted