Ruby

Source: Internet
Author: User

Tags: out col build root i++ digital Getch = = Roo

Title Description

Enter an n-point tree without a root, and the initial point weight for each node is 0.

For M operations, each operation is a three digit x,k,b.

In particular, the first modification, the X and x adjacent to all points of the point, if the original is w I, then change to kw i + B; in other words, multiply K plus B.

Then ask X and X to the point of all points adjacent to the right and, because the result may be large, the output to 1000000007 modulo results. For 100% of data, meet 1≤n,m≤200000.


Input

The first row of two integers n,m.

Next n? 1 lines describe a tree. Two integers per line x, y means there is an edge between x and Y. The subscript of the point starts at 1.

Next M line. Three integer x,k,b per line represents an operation.


Output

The result of each operation output sum modulo.


Sample input

3 6
1 2
2 3
1 0 1
3 1000 0
3 1000 0
3 1000 0
3 1000 0
2 1 0


Sample output

2
1000
1000000
1000000000
999993007
999993008


Exercises

The BFS sequence of each point is calculated so that all of its sons are numbered consecutively so that they can be manipulated on the segment tree. Note To determine if the current node has a father, whether there is a son.

#include <queue>#include<cmath>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#definell Long LongConst intmaxn=200000+ -;Const intp=1000000007;intN,M,X,K,B,ID[MAXN],FA[MAXN],SON[MAXN],FSON[MAXN];intfir[maxn],nex[maxn*2],to[maxn*2],ecnt;ll W[MAXN];BOOLPP[MAXN];structnode{ll V,add,mul,l,r;} st[maxn*4];voidBuildintroot,ll l,ll R) {ST[ROOT].L=l;st[root].r=R; St[root].add=0; st[root].mul=1; if(l==r) st[root].v=0; Else{        intM= (l+r) >>1; Build (Root*2, l,m); Build (Root*2+1, m+1, R); ST[ROOT].V= (st[root*2].v+st[root*2+1].V)%p; }}voidPushdown (introot) {St[root*2].v= (st[root*2].v*st[root].mul+st[root].add* (st[root*2].r-st[root*2].l+1))%p; St[root*2+1].v= (st[root*2+1].v*st[root].mul+st[root].add* (st[root*2+1].r-st[root*2+1].l+1))%p; St[root*2].mul= (st[root*2].mul*st[root].mul)%p; St[root*2+1].mul= (st[root*2+1].mul*st[root].mul)%p; St[root*2].add= (st[root*2].add*st[root].mul+st[root].add)%p; St[root*2+1].add= (st[root*2+1].add*st[root].mul+st[root].add)%p; St[root].add=0; st[root].mul=1;}voidU1 (introot,ll l,ll r,ll val) {    if(st[root].l>r| | ST[ROOT].R&LT;L)return ; if(st[root].l>=l&&st[root].r<=R) {ST[ROOT].V= (st[root].v*val)%p; St[root].add= (st[root].add*val)%p; St[root].mul= (st[root].mul*val)%p; }    Else{pushdown (root); U1 (Root*2, L,r,val); U1 (Root*2+1, L,r,val); ST[ROOT].V= (st[root*2].v+st[root*2+1].V)%p; }}voidU2 (introot,ll l,ll r,ll val) {    if(st[root].l>r| | ST[ROOT].R&LT;L)return ; if(st[root].l>=l&&st[root].r<=R) {ST[ROOT].V= (st[root].v+val* (st[root].r-st[root].l+1))%p; St[root].add= (st[root].add+val)%p; }    Else{pushdown (root); U2 (Root*2, L,r,val); U2 (Root*2+1, L,r,val); ST[ROOT].V= (st[root*2].v+st[root*2+1].V)%p; }}ll Query (introot,ll l,ll R) {    if(st[root].l>r| | ST[ROOT].R&LT;L)return 0; if(ST[ROOT].L&GT;=L&AMP;&AMP;ST[ROOT].R&LT;=R)returnst[root].v; Else{pushdown (root); return(Query (root*2, L,r) +query (root*2+1, l,r))%p; }}voidAddintUintv) {nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;}voidBFs () {Queue<int>Q; inttot=0; Q.push (1);  while(!Q.empty ()) {        intx=Q.front (); ID[X]=++tot;        Q.pop ();  for(intE=fir[x];e;e=nex[e])if(to[e]!=Fa[x]) {Son[x]++; if(son[x]==1) fson[x]=To[e]; Fa[to[e]]=x;        Q.push (To[e]); }}}template<typename t>voidRead (t&AA) {    Charcc ll Ff;aa=0; Cc=getchar (); ff=1;  while((cc<'0'|| Cc>'9') &&cc!='-') cc=GetChar (); if(cc=='-') ff=-1, cc=GetChar ();  while(cc>='0'&&cc<='9') aa=aa*Ten+cc-'0', cc=GetChar (); AA*=ff;}intMain () {Freopen ("ruby.in","R", stdin); Freopen ("Ruby.out","W", stdout);    Read (n), read (m); Build (1,1, N);  for(intI=1; i<n;i++){        intx, y;        Read (x), read (y);    Add (x, y), add (y,x);    } BFS ();  while(m--){        intX,k,b;ll ans=0;        Read (x), read (k), read (b); U1 (1, id[x],id[x],k); U2 (1, id[x],id[x],b); Ans+=query (1, id[x],id[x]); if(Son[x]) {U1 (1, id[fson[x]],id[fson[x]]+son[x]-1, K); U2 (1, id[fson[x]],id[fson[x]]+son[x]-1, B); Ans= (Ans+query (1, id[fson[x]],id[fson[x]]+son[x]-1))%p; }        if(Fa[x]) {U1 (1, id[fa[x]],id[fa[x]],k); U2 (1, id[fa[x]],id[fa[x]],b); Ans= (Ans+query (1, Id[fa[x]],id[fa[x]]))%p; } printf ("%lld\n", ans); }    return 0;} 

Ruby

Related Article

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.

Tags Index: