Time limit:30 Sec Memory limit:128 MB
submit:2928 solved:975
[Submit] [Status] [Discuss] Description
A tree of n points, with an initial weight of 1 for each point. There is a Q operation for this tree, and each operation is one of the following four actions:
+ U v C: The weights of the points on the path from U to V are added to the natural number C;
-U1 v1 U2 v2: Delete the edge of the tree (U1,V1), add a new Edge (U2,V2), to ensure that the operation is still a tree;
* U v C: The weights of the points on the path from U to V are multiplied by the natural number C;
/u V: ask for the weights of the points on the path of the U to V and find the remainder of the answer for 51061.
Inputfirst row two integers n,q
Next n-1 line two positive integers per line u,v, describe this tree
the next Q line, each line describes an actionOutputOne line for each/corresponding answer outputSample Input3 2
1 2
2 3
* 1 3 4
/1 1
Sample Output4
HINT
Data size and conventions
10% Data Guarantee, 1<=n,q<=2000
Another 15% of the data is guaranteed, 1<=n,q<=5*10^4, no-operation, and the initial tree is a chain
Another 35% of the data is guaranteed, 1<=n,q<=5*10^4, no-operation
100% Data Guarantee, 1<=N,Q<=10^5,0<=C<=10^4
Exercises
Four operations are obviously to be maintained with a dynamic tree, for the + and * operation, you need to mark, but when the tag is decentralized, you need to consider the first place + or first put the problem.
Assuming that the tree now has the right value of x and two nodes with a weight of Y, Y is the Father node of X, to which the two +a1 *b1 *b2 +a2 Four operations, because Y is the father of X, so just Mark Y, not down to the X node. The value of the X-node should be: (X+A1) *b1*b2+a2=a1*b1*b2*x+a1*b1*b2+a2, so t1=a1*b1*b2, T2=A1*B1*B2+A2, the original can be written as: T1*x+t2, here T1 is the multiplication tag, T2 is the addition mark, the way of decentralization is the form of t1*x+t2, from the above formula can be seen, for multiplication marks the cumulative principle is the addition of multiply, for the principle of additive tag is only multiply. The labeling is hereby delegated
The rest is the normal operation of the dynamic tree.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <algorithm>7#include <queue>8#include <vector>9 using namespacestd;Tentypedef unsignedintUI; One ConstUI maxn=200000; A ConstUI mod=51061; - UI n,q; -UI c[maxn][2],FA[MAXN],KEY[MAXN],SIZE[MAXN],SUM[MAXN],ADD[MAXN],MUL[MAXN],ST[MAXN]; the Chars[Ten]; - BOOLREV[MAXN]; - BOOLIsRoot (UI x) { - returnx!=c[fa[x]][0]&&x!=c[fa[x]][1]; + } - voidCalc (UI x,ui A,ui b) {//node x, for ax+b operation + if(x==0)return; AKey[x]= ((key[x]*a)%mod+b)%MoD; atSum[x]= ((sum[x]*a)%mod+ (b*size[x])%mod)%MoD; -Add[x]= ((add[x]*a)%mod+b)%MoD; -mul[x]= (mul[x]*a)%MoD; - } - voidpushup (UI x) { -size[x]=size[c[x][0]]+size[c[x][1]]+1; inSum[x]= (sum[c[x][0]]+sum[c[x][1]]+KEY[X])%MoD; - } to voidpushdown (UI x) { +UI l=c[x][0],r=c[x][1]; - if(rev[x]==true){ therev[x]^=1; rev[l]^=1; rev[r]^=1; *Swap (c[x][0],c[x][1]); $ }Panax NotoginsengUI tmpa=add[x],tmpm=Mul[x]; -add[x]=0; mul[x]=1; the if(tmpa!=0|| tmpm!=1){ + Calc (L,tmpm,tmpa); Calc (R,tmpm,tmpa); A } the } + voidrotate (UI x) { -UI y=fa[x],z=Fa[y],l,r; $ if(x==c[y][0]) l=0;ElseL=1; r=l^1; $ if(!isroot (y)) { - if(y==c[z][0]) c[z][0]=x; - Elsec[z][1]=x; the } -Fa[x]=z; Fa[y]=x; fa[c[x][r]]=y;WuyiC[Y][L]=C[X][R]; c[x][r]=y; the pushup (y); pushup (x); - } Wu voidsplay (UI x) { -UI top=0; st[++top]=x; About for(UI i=x;! IsRoot (i); i=Fa[i]) { $st[++top]=Fa[i]; - } - for(UI i=top;i;i--) pushdown (St[i]); - while(!isroot (x)) { AUI y=fa[x],z=Fa[y]; + if(!isroot (y)) { the if((x==c[y][0]&&y==c[z][0])|| (x==c[y][1]&&y==c[z][1])){ - rotate (y), rotate (x); $ } the Elserotate (x), rotate (x); the } the Elserotate (x); the } - } in voidaccess (UI x) { theUI t=0; the while(x) { About splay (x); thec[x][1]=t; pushup (x); theT=x; x=Fa[x]; the } + } - voidrever (UI x) { theAccess (x); Splay (x); rev[x]^=1;Bayi } the voidCut (UI X,ui y) { theRever (x); Access (y); Splay (y); c[y][0]=fa[x]=0; - } - voidLink (UI x,ui y) { theRever (x); fa[x]=y; splay (x); the } the intMain () { thescanf"%u%u",&n,&Q); - for(UI i=1; i<=n;i++) key[i]=sum[i]=size[i]=mul[i]=1; the for(UI i=1, u,v;i<=n-1; i++){ thescanf"%u%u",&u,&v); the link (u,v);94 } the for(UI i=1; i<=q;i++){ the UI u1,v1,u2,v2,cc; thescanf"%s", s);98 if(s[0]=='+'){ Aboutscanf"%u%u%u",&u1,&v1,&cc); - Rever (U1); access (v1); splay (v1) ;101Calc (v1,1, CC);102 }103 Else if(s[0]=='-'){104scanf"%u%u%u%u",&u1,&v1,&u2,&v2); the Cut (U1,V1); link (u2,v2);106 }107 Else if(s[0]=='*'){108scanf"%u%u%u",&u1,&v1,&cc);109 Rever (U1); access (v1); splay (v1) ; theCalc (V1,CC,0);111 } the Else{113scanf"%u%u",&u1,&v1); the Rever (U1); access (v1); splay (v1) ; theprintf"%u\n", Sum[v1]); the }117 }118 return 0;119}
Bzoj 2631:tree