Statistics of tree-chain split tree

Source: Internet
Author: User

/* Title Description Description
There are n nodes on a tree, numbered 1 to N, each with a weight of W.

We will ask you to do some work on this tree in the following form:

I. Change u t: Changing the weight of the node U to t
Ii. QMAX u V: ask for the maximum weight of the node on the path from point u to V
Iii. qsum u V: ask for the weights and values of the nodes on the path from point u to V

Note: Nodes on the path from point u to v include U and V itself

Enter a description input Description
The first behavior of the input file is an integer n, which represents the number of nodes.

The next n–1 line, with 2 integers a and b per line, indicates that there is an edge connected between Node A and Node B.

Next n rows, one integer per line, and the integer of line I, WI represents the weight of node I.

The next 1 lines, an integer q, represent the total number of operations.

The next Q line, one operation per line, is given in the form "Change U T" or "QMAX u V" or "Qsum u V".

Outputs description Output Description
For each "QMAX" or "qsum" operation, each line outputs an integer representing the result of the required output.
Tree chain split template problem, support tree query and modification, there is nothing new, that is, 2 times the DFS Plus line tree plus LCA.



*/
#include <cstdio>
#include <iostream>
using namespace Std;
int zhi[30002],n,m,head[30002],next[60002],u[60002],dui[30002],deep[30002];
int size[30002],f[30002],lc[30002][15],n1,lian[30002];
struct SHU
{
int l,r,max,sum;
}A[100005];
void dfs1 (int a1)
{
Size[a1]=1;
F[a1]=1;
for (int i=1;i<=14;i++)
{
if (deep[a1]< (1<<i))
Break
LC[A1][I]=LC[LC[A1][I-1]][I-1];
}
for (int b=head[a1];b;b=next[b])
if (!f[u[b]])
{
deep[u[b]]=deep[a1]+1;
LC[U[B]][0]=A1;
DFS1 (U[b]);
SIZE[A1]+=SIZE[U[B]];
}
Return
}
void dfs2 (int a1,int A2)
{
int k=0;
n1++;
DUI[A1]=N1;
LIAN[A1]=A2;
for (int i=head[a1];i;i=next[i])
if (SIZE[K]&LT;SIZE[U[I]]&AMP;&AMP;DEEP[U[I]]&GT;DEEP[A1])
K=u[i];
if (k==0)
Return
DFS2 (K,A2);
for (int i=head[a1];i;i=next[i])
if (U[I]!=K&AMP;&AMP;DEEP[U[I]]&GT;DEEP[A1])
DFS2 (U[i],u[i]);
Return
}
void build (int a1,int a2,int A3)
{
A[A1].L=A2;
A[A1].R=A3;
if (A2+1==A3)
Return
int mid= (A2+A3) >>1;
Build (A1*2,a2,mid);
Build (A1*2+1,MID,A3);
Return
}
void cha (int a1,int a2,int A3)
{
int lr=a[a1].l,rr=a[a1].r,mid= (LR+RR) >>1;
if (LR+1==RR)
{
A[A1].MAX=A3;
A[A1].SUM=A3;
Return
}
if (A2<mid)
Cha (a1*2,a2,a3);
Else
Cha (a1*2+1,a2,a3);
A[a1].sum=a[a1*2].sum+a[a1*2+1].sum;
A[a1].max=max (A[a1*2].max,a[a1*2+1].max);
Return
}
int LCA (int a1,int A2)
{
if (DEEP[A1]&LT;DEEP[A2])
Swap (A1,A2);
int T=DEEP[A1]-DEEP[A2];
for (int i=0;i<=14;i++)
if (t&1<<i)
A1=lc[a1][i];
for (int i=14;i>=0;i--)
if (Lc[a1][i]!=lc[a2][i])
{
A1=lc[a1][i];
A2=lc[a2][i];
}
if (A1==A2)
return A1;
Else
return lc[a1][0];
}
int xisu (int a1,int a2,int A3)
{
int sum1=0;
if (A[A1].L&GT;=A2&AMP;&AMP;A3&GT;=A[A1].R)
return a[a1].sum;
int mid= (A[A1].L+A[A1].R) >>1;
if (A2<mid)
Sum1+=xisu (A1*2,A2,A3);
if (A3>mid)
Sum1+=xisu (A1*2+1,A2,A3);
return sum1;
}
int Lihe (int a1,int A2)
{
int sum1=0;
for (; LIAN[A1]!=LIAN[A2];)
{
Sum1+=xisu (1,dui[lian[a1]],dui[a1]+1);
A1=LC[LIAN[A1]][0];
}
Sum1+=xisu (1,dui[a2],dui[a1]+1);
return sum1;
}
int Xima (int a1,int a2,int A3)
{
int sum1=-10000000;
if (A[A1].L&GT;=A2&AMP;&AMP;A3&GT;=A[A1].R)
return A[a1].max;
int mid= (A[A1].L+A[A1].R) >>1;
if (A2<mid)
Sum1=max (Sum1,xima (A1*2,A2,A3));
if (A3>mid)
Sum1=max (Sum1,xima (A1*2+1,A2,A3));
return sum1;
}
int Lima (int a1,int A2)
{
int sum1=-10000000;
for (; LIAN[A1]!=LIAN[A2];)
{
Sum1=max (Sum1,xima (1,dui[lian[a1]],dui[a1]+1));
A1=LC[LIAN[A1]][0];
}
Sum1=max (Sum1,xima (1,dui[a2],dui[a1]+1));
return sum1;
}
int main ()
{
scanf ("%d", &n);
for (int i=1;i<n;i++)
{
int a1,a2;
scanf ("%d%d", &AMP;A1,&AMP;A2);
NEXT[I]=HEAD[A1];
Head[a1]=i;
U[I]=A2;
NEXT[I+N]=HEAD[A2];
Head[a2]=i+n;
U[I+N]=A1;
}
for (int i=1;i<=n;i++)
scanf ("%d", &zhi[i]);
DFS1 (1);
DFS2 (a);
Build (1,1,n+1);
scanf ("%d", &m);
for (int i=1;i<=n;i++)
Cha (1,dui[i],zhi[i]);
for (int i=0;i<m;i++)
{
int a1,a2;
Char ch[10];
scanf ("%s%d%d", CH,&AMP;A1,&AMP;A2);
if (ch[0]== ' C ')
{
ZHI[A1]=A2;
Cha (1,DUI[A1],A2);
}
Else
{
int T=lca (A1,A2);
if (ch[1]== ' S ')
printf ("%d\n", Lihe (a1,t) +lihe (a2,t)-zhi[t]);
Else
printf ("%d\n", Max (Lima (A1,T), Lima (A2,T)));
}
}
}

Statistics of tree-chain split 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.