Fzu OJ 2277 change tree array +dfs sequence

Source: Internet
Author: User

Problem 2277 Change
Time limit:2000 mSec Memory limit:262144 KB problem Description

There is a rooted tree with n nodes and number from 1-n. Root's number is the 1.Each node has a value AI.

Initially all the node's value is 0.

We have Q operations. There is kinds of operations.

1 v x k:a[v]+=x, A[v ']+=x-k (V ' is-child of V), A[v ']+=x-2*k (V ' was child of V ') and so on.

2 v:output a[v] MoD 1000000007 (10^9 + 7).

Input

First line contains an integer t (1≤t≤3), represents there is t test cases.

In each test case:

The first line contains a number n.

The second line contains n-1 number, P2,P3,..., PN. Pi is the father of I.

The third line contains a number Q.

Next q lines, each line contains an operation. ("1 v x k" or "2 V")

1≤n≤3*10^5

1≤pi < I

1≤q≤3*10^5

1≤v≤n; 0≤x < 10^9 + 7; 0≤k < 10^9 + 7

Output

For each operation 2, outputs the answer.

Sample Input131 131 1 2 2 sample Output21 source eighth Fujian College student Program Design contest-Replay (thanks to the Undertaker Xiamen Polytechnic Institute)

LLD WA Day

#pragmaComment (linker, "/stack:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#defineLL __int64#definePi (4*atan (1.0))#defineEPS 1e-8#defineBug (x) cout<< "Bug" <<x<<endl;Const intn=3e5+Ten, m=2e6+Ten, inf=1e9+Ten;ConstLL inf=1e18+Ten, mod=1e9+7;structisss{intV,nex;} Edge[n<<1];inthead[n],edg;int inch[N], out[N],tot; LL Deep[n];voidAddintUintv) {    ++EDG; EDGE[EDG].V=v; Edge[edg].nex=Head[u]; Head[u]=EDG;}voidDfsintUintFaintDP) {    inch[u]=++tot; Deep[u]=DP;  for(intI=head[u];i;i=Edge[i].nex) {        intv=edge[i].v; if(V==FA)Continue; DFS (V,U,DP+1); }     out[u]=tot;}structayt{LL Tree[n]; voidinit () {memset (tree,0,sizeof(tree)); }    intLowbit (intx) {returnx&-x; }    voidUpdateintx,ll c) {         while(x<N) {tree[x]+=C; X+=lowbit (x); }} LL Query (intx) {LL ans=0;  while(x) {ans+=Tree[x]; X-=lowbit (x); }        returnans; }}TX,TK;voidinit () {tot=0; memset (Head,0,sizeof(head)); memset (Deep,0,sizeof(deep));    Tx.init ();    Tk.init (); EDG=0;}intMain () {intT; scanf ("%d",&T);  while(t--)    {        intN; scanf ("%d",&N);        Init ();  for(intI=2; i<=n;i++)        {            intF; scanf ("%d",&f);            Add (f,i);        Add (i,f); } DFS (1,-1,0); intQ; scanf ("%d",&q);  while(q--)        {            intt,v;            LL x,k; scanf ("%d%d",&t,&v); if(t==1) {scanf ("%i64d%i64d",&x,&k); X+=1ll*deep[v]*K; X%=MoD; Tx.update (inch[v],x]; Tx.update ( out[v]+1,-x); Tk.update (inch[V],k]; Tk.update ( out[v]+1,-k); }            Else{LL xx=tx.query (inch[v]); LL y=tk.query (inch[v]); Y%=MoD; LL ans=xx-1ll*deep[v]*y; Ans%=mod;ans+=mod;ans%=MoD; printf ("%i64d\n", ans); }        }    }    return 0;}

Fzu OJ 2277 change tree array +dfs sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.