Codeforces Round #336 (Div. 1) D. Power Tree

D. Power Tree time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output 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:

Codeforces Round #336 (Div. 1) D. Power Tree
文章图片
【Codeforces Round #336 (Div. 1) D. Power Tree】 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.
Input The first line of the input contains two space separated integers v1 and q (1?≤?v1?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?
  • 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.
Output For each query, print out the power of the given vertex modulo 109?+?7.
Sample test(s) input
2 5 1 1 3 1 2 5 1 3 7 1 4 11 2 1

output
344

input
5 5 1 1 4 1 2 3 2 2 1 2 7 2 1

output
14 94

Note 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 #include #include #include 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 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 YZY freopen("yzy.txt","r",stdin); #endif cin >> 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; }


    推荐阅读