"BZOJ4034" [HAOI2015] tree operation tree chain split + segment tree

Source: Internet
Author: User

Action on "BZOJ4034" [HAOI2015] Tree description

There is a tree with a number of N, the root of point 1, and the tree point has edge rights. And then there's M-a

Operation, divided into three kinds: Operation 1: The point of a node x to increase the right of a. Action 2: Add a to the point in a subtree that has a node x as its root. Action 3: Ask a node x to the root of the path in a little bit of the right and. Input

The first line consists of two integers N, M. Represents the number of points and operands.

The next row of N integers represents the initial weights of the nodes in the tree. Next N-1 line three positive integer fr, to, indicating that there is an edge in the tree (FR, to). The next M line, each line represents one operation. The first number represents the type of operation (1-3) followed by the parameters of the operation (x or x a). Output

For each query operation, output the answer to the query. The answers are separated by line breaks.

Sample Input5 5
1 2 3) 4 5
1 2
1 4
2 3
2 5
3 3
1 2 1
3 5
2 1 2
3 3Sample Output6
9
13
HINT

For 100% of data, n,m<=100000, and the absolute value of all input data is not

Will exceed 10^6. Solving: Tree section template problem, note to open long long
#include <stdio.h>#include<string.h>#include<iostream>#defineLson x<<1#defineRson x<<1|1using namespaceStd;typedefLong Longll;Const intmaxn=100010;intn,m,cnt,tot;ll S[MAXN<<2],tag[maxn<<2];intto[maxn<<1],next[maxn<<1],V[MAXN],U[MAXN],FA[MAXN],HEAD[MAXN];intDEEP[MAXN],SIZE[MAXN],SON[MAXN],P[MAXN],TOP[MAXN];Charstr[Ten];intReadin () {intret=0, sig=1;CharGC;  while(gc<'0'|| Gc>'9') sig= (gc=='-')?-1:1, gc=GetChar ();  while(gc>='0'&&gc<='9') ret=ret*Ten+gc-'0', gc=GetChar (); returnret*Sig;}voidAddintAintb) {to[cnt]=C; NEXT[CNT]=Head[a]; Head[a]=cnt++;}voidDFS1 (intx) {Size[x]=1;  for(inti=head[x];i!=-1; i=Next[i]) {        if(to[i]!=Fa[x]) {Fa[to[i]]=Y; Deep[to[i]]=deep[x]+1;            DFS1 (To[i]); SIZE[X]+=Size[to[i]]; if(Size[to[i]]>size[son[x]]) son[x]=To[i]; }    }}voidDFS2 (intXintTP) {Top[x]=TP; P[X]=++tot; V[P[X]]=U[x]; if(Son[x]) DFS2 (SON[X],TP);  for(inti=head[x];i!=-1; i=Next[i])if(to[i]!=son[x]&&to[i]!=fa[x]) DFS2 (To[i],to[i]);}voidPushup (intx) {S[x]=s[lson]+S[rson];}voidPushdown (intLintRintx) {    intMid=l+r>>1; Tag[lson]+=tag[x],tag[rson]+=Tag[x]; S[lson]+=tag[x]* (Long Long) (mid-l+1), s[rson]+=tag[x]* (Long Long) (Rmid); TAG[X]=0;}voidBuildintLintRintx) {    if(l==R) {S[x]=V[l]; return ; }    intMid=l+r>>1; Build (L,mid,lson), Build (mid+1, R,rson); Pushup (x);}voidUpdata (intLintRintXintAintBintc) {    if(a<=l&&r<=b) {s[x]+=c* (Long Long) (r-l+1), tag[x]+=C; return ;    } pushdown (l,r,x); intMid=l+r>>1; if(b<=mid) Updata (L,MID,LSON,A,B,C); Else if(A>mid) Updata (mid+1, R,rson,a,b,c); ElseUpdata (L,mid,lson,a,b,c), Updata (mid+1, R,rson,a,b,c); Pushup (x);} ll query (intLintRintXintAintb) {    if(a<=l&&r<=b)returnS[x];    Pushdown (L,R,X); intMid=l+r>>1; if(B<=mid)returnquery (L,MID,LSON,A,B); if(A>mid)returnQuery (mid+1, r,rson,a,b); returnQuery (l,mid,lson,a,b) +query (mid+1, r,rson,a,b);}voidChange () {intX=readin (), y=Readin (); Updata (1N1, p[x],p[x],y);}voidAdd () {intX=readin (), y=Readin (); Updata (1N1, p[x],p[x]+size[x]-1, y);//Modify an entire subtrees tree}voidGetans () {intx=Readin (); ll ans=0;  while(top[x]!=1) {ans+=query (1N1, p[top[x]],p[x]); X=Fa[top[x]]; } ans+=query (1N1,1, p[x]); printf ("%lld\n", ans);}intMain () {n=readin (), m=Readin (); memset (Head,-1,sizeof(head)); inti,a,b;  for(i=1; i<=n;i++) u[i]=Readin ();  for(i=1; i<n;i++) {a=readin (), b=Readin ();    Add (A, B), add (B,a); } deep[1]=1; DFS1 (1); DFS2 (1,1); Build (1N1);  for(i=1; i<=m;i++) {a=Readin (); Switch(a) { Case 1: Change (); Break;  Case 2: Add (); Break;  Case 3: Getans (); Break; }    }    return 0;}

"BZOJ4034" [HAOI2015] tree operation tree chain split + segment tree

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.