Codeforces Round #336 (Div. 1) D. Power Tree
来源:互联网 发布:张纪中版神雕侠侣 知乎 编辑:程序博客网 时间:2024/06/07 14:35
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 vi associated with it. The power of a vertex is defined as the strength of the multiset composed of the value associated with this vertex (vi) 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 somemultiset 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 109 + 7.
The first line of the input contains two space separated integers v1 and q (1 ≤ v1 < 109, 1 ≤ q ≤ 200 000) — 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 pi vi, 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 pi is some already existing vertex and 1 ≤ vi < 109.
- 2 ui, if these line describes a query. It is guaranteed ui 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 109 + 7.
2 51 1 31 2 51 3 71 4 112 1
344
5 51 1 41 2 32 21 2 72 1
1494
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
#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 2E5 + 20;typedef long long LL;const LL mo = 1000000007;struct Q{int opt;int qx,qy;}q[maxn];int n,m,i,j,In[maxn],Out[maxn],d_t,cur = 1,si[maxn],fa[maxn];LL c[maxn*20],va[maxn],Mark[maxn*20 + 10];vector <int> v[maxn];void dfs(int k){In[k] = ++d_t;for (int l = 0; l < v[k].size(); l++) {int to = v[k][l];dfs(to);}Out[k] = d_t;}void push_down(int o){if (Mark[o] == 1) return;Mark[2*o] = Mark[2*o]*Mark[o]%mo;Mark[2*o+1] = Mark[o]*Mark[2*o+1]%mo;c[2*o] = c[2*o]*Mark[o]%mo;c[2*o+1] = c[2*o+1]*Mark[o]%mo;Mark[o] = 1;}LL f_c(int o,int l,int r,int ql,int qr){if (ql <= l && r <= qr) return c[o];push_down(o);int mid = (l+r) >> 1;LL ret = 0;if (ql <= mid) ret = (ret+f_c(2*o,l,mid,ql,qr)) % mo;if (qr > mid) ret = (ret+f_c(2*o+1,mid+1,r,ql,qr)) % mo;return ret;}void Modify(int o,int l,int r,int ql,int qr,LL M){if (ql <= l && r <= qr) {c[o] = c[o]*M%mo;Mark[o] = Mark[o]*M%mo;return;}push_down(o);int mid = (l+r) >> 1;if (ql <= mid) Modify(2*o,l,mid,ql,qr,M);if (qr > mid) Modify(2*o+1,mid+1,r,ql,qr,M);c[o] = (c[2*o] + c[2*o+1]) % mo;}void Add(int o,int l,int r,int pos,LL A){c[o] = (c[o]+A)%mo;if (l == r) return;push_down(o);int mid = (l+r) >> 1;if (pos <= mid) Add(2*o,l,mid,pos,A);else Add(2*o+1,mid+1,r,pos,A);}LL ksm(LL x,LL y){LL ret = 1;for (; y; y >>= 1) {if (y&1) ret = ret*x%mo;x = x*x%mo;}return ret%mo;}int main(){#ifdef YZYfreopen("yzy.txt","r",stdin);#endifcin >> va[1] >> m;for (i = 1; i <= maxn*20; i++) Mark[i] = 1;for (i = 1; i <= m; i++) {scanf("%d",&q[i].opt);if (q[i].opt == 1) {scanf("%d %d",&q[i].qx,&va[++cur]);q[i].qy = cur;v[q[i].qx].push_back(q[i].qy);fa[q[i].qy] = q[i].qx;}else scanf("%d",&q[i].qx);}n = cur;dfs(1); si[1] = 1;Add(1,1,cur,In[1],va[1]);for (i = 1; i <= m; i++) {if (q[i].opt == 1) {si[q[i].qy] = 1;LL Si = f_c(1,1,n,In[q[i].qx],In[q[i].qx]);Si = Si*ksm(va[q[i].qx],mo-2)%mo;Add(1,1,n,In[q[i].qy],va[q[i].qy]*Si%mo);LL Modi = ksm(si[q[i].qx],mo-2)*(++si[q[i].qx])%mo;Modify(1,1,n,In[q[i].qx],Out[q[i].qx],Modi);}else {LL a = f_c(1,1,n,In[q[i].qx],Out[q[i].qx]);LL b;if (q[i].qx == 1) b = 1;else {int FA = fa[q[i].qx];b = f_c(1,1,n,In[FA],In[FA]);b = ksm(va[FA],mo-2)%mo*b%mo;b = ksm(b,mo-2);}printf("%I64d\n",a*b%mo);}}return 0;}
- Codeforces Round #336 (Div. 1) D. Power Tree
- 【Codeforces Round #200 (Div. 1)】Codeforces 343D Water Tree
- Codeforces Round #200 (Div. 1)D Water Tree
- Codeforces Round #200 (Div. 1) D. Water Tree
- Codeforces Round #200 (Div. 1) D.Water Tree
- [树的直径] Codeforces 804D Round #411 (Div. 1) D. Expected diameter of a tree
- Codeforces Round #316 (Div. 2) D. Tree Requests
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs_clock,二分
- Codeforces Round #316 (Div. 2) D Tree Requests
- Codeforces Round #316 (Div. 2) D. Tree Requests
- Codeforces Round #316 (Div. 2) D. Tree Requests (DFS序)
- Codeforces Round #329 (Div. 2) D. Happy Tree Party
- Codeforces Round #316 (Div. 2)D. Tree Requests
- Codeforces Round #353 (Div. 2) D. Tree Construction
- Codeforces Round #353 (Div. 2)D. Tree Construction
- Codeforces Round #353 (Div. 2) D. Tree Construction (BST)
- Codeforces Round #316 (Div. 2) D. Tree Requests
- Codeforces Round #353 (Div. 2) D. Tree Construction
- php学习日记第十棒(switch 语句)
- Android NDK开发(一)HelloWorld
- 【C++】深度探索C++对象模型之执行期语意学
- xUtils中的HttpUtils请求网络数据和BitmapUtils加载网络图片
- 《TCP/IP详解 卷1:协议》 读书笔记 第23章 TCP的保活定时器
- Codeforces Round #336 (Div. 1) D. Power Tree
- 常用正则表达式大全
- 【GLSL教程】(四)shder的简单示例
- ThinkPHP 模块与操作
- 关于4.X系统使用安卓5.0Material风格AlertDialog全屏问题
- c++标准14取消decltype推算函数返回类型
- Linux命令学习总结:hexdump
- android-xml文件布局
- 安卓开发之多线程与异步任务