Portal: http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983
Data structure of WZJ (vii) |
Difficulty level: C; operating time limit: 1000ms; operating space limit: 51200KB; code length limit: 2000000B |
Question Description |
Give you a tree of n nodes without a root, each point has a weight (starting at 0). Please design a data structure to complete the following functions: Give you A, B, V, please increase the node weights in A to B path by V (including point A and point B). Finally, the weights for each node are output. |
Input |
The first behavior is a positive integer n. Next N-1 behaves on each side, with 2 positive integers, a, a, and a, A and B, representing an edge from A to B. (numbering from 1). The n+1 behavior is a positive integer q, which indicates the Q operation. The next Q behavior is 3 positive integers a, B, v per query. |
Output |
Finally, the weights of each point are output, and the format is shown in the sample. |
Input example |
9 2 1 3 2 4 3 3 5 3 8 9 8 8 7 6 7 5 2 5 10 4 6 3 1 9 5 2 7 10 5 5 100 |
Output example |
5 25 28 3 110 3 13 18 5 |
Other Notes |
1<=n,q<=100000 |
Tree-chain split version:
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 using namespacestd;Ten Const intmaxn=100000+Ten, inf=-1u>>1, maxn3=3*MAXN; One structtedge{intX,y,w,next;} adj[maxn*2];intms=0, FCH[MAXN]; A structedge{int from, To,dist;} E[MAXN]; - voidAddedge (intUintVintW) {adj[++ms]= (Tedge) {u,v,w,fch[u]};fch[u]=ms;return;} - inttop[maxn],dep[maxn],son[maxn],siz[maxn],fa[maxn],sumv[maxn3],addv[maxn3],num[maxn],sz=0, Ql,qr,cv,_sum,n,q; the voidDfsintu) { -siz[u]=1;d ep[u]=dep[fa[u]]+1; - for(intI=fch[u];i;i=Adj[i].next) { - intv=adj[i].y; + if(v!=Fa[u]) { -fa[v]=u; + Dfs (v); A if(Siz[son[u]]<siz[v]) son[u]=v; atsiz[u]+=Siz[v]; - } -}return; - } - voidBuildintUintTP) { -num[u]=++sz;top[u]=TP; in if(Son[u]) build (SON[U],TP); - for(intI=fch[u];i;i=Adj[i].next) { to intv=adj[i].y; + if(v!=fa[u]&&v!=Son[u]) build (v,v); -}return; the } * voidUpdateintOintLintR) { $ if(QL<=L&&R<=QR) addv[o]+=CV;Panax Notoginseng Else{ - intM=l+r>>1,lc=o<<1, rc=lc|1; the if(ql<=M) Update (LC,L,M); + if(qr>m) Update (rc,m+1, R); A}return; the } + voidChangeintAintb) { -_sum=0; $ intF1=top[a],f2=Top[b]; $ while(f1!=F2) { - if(dep[f1]<Dep[f2]) swap (F1,F2), swap (A, b); -Ql=num[f1];qr=num[a];update (1,1, n); thea=fa[f1];f1=Top[a]; - }Wuyi if(dep[a]>dep[b]) swap (A, b); theQl=num[a];qr=num[b];update (1,1, n);return;//for Mao is not a child - } Wu voidClear_set (intOintLintRintadd) { - if(l==r) sumv[l]+=add+Addv[o]; About Else{ $ intM=l+r>>1,lc=o<<1, rc=lc|1; -Clear_set (lc,l,m,add+Addv[o]); -Clear_set (rc,m+1, r,add+Addv[o]); -}return; A } +InlineintRead () { the intx=0, sig=1;CharCh=GetChar (); - while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} $ while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); the returnx*=Sig; the } theInlinevoidWriteintx) { the if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; in for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; the } the voidinit () { Aboutn=read (); the for(intI=1; i<n;i++){ the intA=read (), b=read (); theAddedge (A, B,0); Addedge (B,a,0); +E[i]= (Edge) {A, B,0}; - } theDfs1); Build (1,1);Bayi for(intI=1; i<n;i++){ the if(Dep[e[i]. from]>dep[e[i].to]) swap (E[i]. from, e[i].to); the } - return; - } the voidWork () { theq=read (); the while(q--){ the intA=read (), B=read (), c=read (); -cv=C;change (A, b); the } the return; the }94 voidprint () { theClear_set (1,1N0); the for(intI=1; i<=n;i++){ the write (Sumv[num[i]]); ENT;98 } About return; - }101 intMain () {init (); work ();p rint ();return 0;}
POJ 1007 WZJ Data Structure (vii) operations on the tree