bzoj:3730: Shock Wave

Source: Internet
Author: User

Description

There are n cities on a piece of land, connecting to each other through the N-1, forming a tree structure, the distance of the neighboring two cities is 1, and the value of the first city is Value[i].
Unfortunately, earthquakes often occur in this land, and as times evolve, the values of cities tend to change.
Next you need to process m operations online:
0 x K Indicates an earthquake occurred, the epicenter of the city is x, the impact range is k, all the x distance from the city will be affected, the earthquake caused by the economic loss of all the affected city value and.
1 x y means the value of the first x city becomes Y.
In order to reflect the program's online nature, the operation of X, Y, K will need to be different or your program last output to decrypt, if there is no previous output, the default output is 0.

Input

The first line consists of two positive integers n and M.
The second line contains n positive integers, and the number of I represents value[i].
Next N-1 line, each row contains two positive integers u, V, indicating that there is a non-facing edge between U and V.
Next m lines, each row contains three numbers, indicating m operations.

Output

Contains several rows, one positive integer for each query output, to represent the answer.

Sample Input8 1
1 10 100 1000 10000 100000 1000000 10000000
1 2
1 3
2 4
2 5
6 S
3 7
3 8
0 3 1Sample Output11100101points, each center of gravity builds a segment tree to record the value of the city within that block. In addition, a tree of broken lines is built, representing the value of the block within the subtree of a son of the center of gravity. (The segment tree is merged to be the original segment tree).each node is connected to a new edge to a lighter depth of gravity. When modified, traverse the new edge of the pitch, and modify it violently at the lower center of gravity of the segment tree and the de-emphasis line tree. Queries also traverse all new edges, then accumulate and go heavy. Code for a long time ...
#include <cstdio>#include<algorithm>#defineMN 200001using namespacestd;intRead_p,read_ca;inlineintRead () {read_p=0; read_ca=GetChar ();  while(read_ca<'0'|| Read_ca>'9') read_ca=GetChar ();  while(read_ca>='0'&&read_ca<='9') read_p=read_p*Ten+read_ca- -, read_ca=GetChar (); returnread_p;}structna{intX,y,z,ne;} b[200001],bb[6000001];structtree{intL,r,k;} t[10000001];intn,m,ma,la[mn],num=0, fa[mn],s[mn],size,root,va[mn],nm=0, ro[mn],lo[mn],op[mn],rot[mn],uu,nnmm=0, lla[mn],nnum=0;BOOLV[MN];Const intinf=1e9;inlinevoid inch(intXintY) {b[++num].y=y;b[num].ne=la[x];la[x]=num;} Inlinevoid inch(intUintXintYintZ) {bb[++nnum].x=x;bb[nnum].y=y;bb[nnum].z=z;bb[nnum].ne=lla[u];lla[u]=Nnum;} Inlinevoid inch(int&p,intLintRintXintK) {if(!p) p=++nm;t[p].k+=k;if(L==R)return;intMid=l+r>>1;if(X<=mid)inch(T[P].L,L,MID,X,K);Else inch(t[p].r,mid+1, r,x,k);} InlineintQueintPintLintRintx) {if(!p| | X&LT;L)return 0;if(X==R)returnT[P].K;intMid=l+r>>1;if(X<=mid)returnQue (t[p].l,l,mid,x);Else returnQue (t[p].l,l,mid,mid) +que (t[p].r,mid+1, r,x);} InlinevoidGsintXintf) {    intu=0; s[x]=1;  for(intI=la[x];i;i=b[i].ne)if(!v[b[i].y]&&b[i].y!=f) GS (B[I].Y,X), S[x]+=s[b[i].y],u=u>s[b[i].y]?U:S[B[I].Y]; if(U<size-s[x]) u=size-S[x]; if(U&LT;MA) root=x,ma=u;} InlinevoidDfsintXintFintdis) {    if(dis==1) uu=++nnmm; if(f)inch(Ro[root],1, N,dis,va[x]),inch(Rot[uu],1, N,dis,va[x]),inch(X,root,uu,dis);  for(intI=la[x];i;i=b[i].ne)if(b[i].y!=f&& (!V[B[I].Y])) DFS (b[i].y,x,dis+1);} InlinevoidWorkintXintSizintf) {Size=siz;ma=inf;gs (x,0);d FS (Root,0,0); x=root;v[root]=1;  for(intI=la[x];i;i=b[i].ne)if(!V[b[i].y]) work (b[i].y,s[b[i].y],x);} InlineintQuintXintk) {    intAns=que (Ro[x],1, n,k) +Va[x];  for(RegisterintJ=lla[x];j;j=bb[j].ne) {if(k>=bb[j].z) Ans+=va[bb[j].x];ans+=que (ro[bb[j].x],1, n,k-bb[j].z)-que (Rot[bb[j].y],1, n,k-bb[j].z);} returnans;}intMain () {intX,y,z;registerintJ; N=read (); m=read ();  for(intI=1; i<=n;i++) va[i]=read ();  for(intI=1; i<n;i++) X=read (), Y=read (),inch(x, Y),inch(Y,X); Work (1N0); intLa=0;  while(m--) {x=read (); Y=read (); Z=read (); y^=la;z^=LA; if(x) {Z-=Va[y];  for(Registerintj=lla[y];j;j=bb[j].ne)inch(Ro[bb[j].x],1, N,bb[j].z,z),inch(Rot[bb[j].y],1, n,bb[j].z,z); Va[y]+=Z; }        Elseprintf"%d\n", la=qu (y,z)); }}
222832KB. 10736MS. C++. 2552B

bzoj:3730: Shock Wave

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.