//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "动态DP  (带修树上最大点权独立集)": {
        "prefix": "acm-4.3树剖",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define Mat Matrix",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3;",
            "const LL inf=1e18;",
            "int n,m,o,x,y,T,A[N],head[N];",
            "struct QAQ{int to,next;}a[N<<1];",
            "inline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "struct Matrix{",
            "\tLL a[3][3];Mat(){a[1][1]=a[1][2]=a[2][1]=a[2][2]=-inf;}",
            "\tinline Mat operator*(const Mat &O)const{",
            "\t\tMat A;",
            "\t\tfor(Re i=1;i<=2;++i)",
            "\t\t\tfor(Re j=1;j<=2;++j)",
            "\t\t\t\tfor(Re k=1;k<=2;++k)",
            "\t\t\t\t\tA.a[i][j]=max(A.a[i][j],a[i][k]+O.a[k][j]);",
            "\t\treturn A;",
            "\t}",
            "}G[N];",
            "int idx[N];",
            "struct killed_Tree{",
            "\tstruct Segment_Tree{",
            "\t\t#define pl (p<<1)",
            "\t\t#define pr (p<<1|1)",
            "\t\t#define mid ((L+R)>>1)",
            "\t\tMat tr[N<<2];",
            "\t\tinline void pushup(Re p){tr[p]=tr[pl]*tr[pr];}",
            "\t\tinline void build(Re p,Re L,Re R){",
            "\t\t\tif(L==R){tr[p]=G[idx[L]];return;}",
            "\t\t\tbuild(pl,L,mid),build(pr,mid+1,R);",
            "\t\t\tpushup(p);",
            "\t\t}",
            "\t\tinline void change(Re p,Re L,Re R,Re x,Mat &v){",
            "\t\t\tif(L==R){tr[p]=v;return;}",
            "\t\t\tif(x<=mid)change(pl,L,mid,x,v);",
            "\t\t\telse change(pr,mid+1,R,x,v);",
            "\t\t\tpushup(p);",
            "\t\t}",
            "\t\tinline Mat ask(Re p,Re L,Re R,Re l,Re r){",
            "\t\t\tif(l<=L&&R<=r)return tr[p];",
            "\t\t\tif(r<=mid)return ask(pl,L,mid,l,r);",
            "\t\t\telse if(l>mid)return ask(pr,mid+1,R,l,r);",
            "\t\t\telse return ask(pl,L,mid,l,r)*ask(pr,mid+1,R,l,r);",
            "\t\t}",
            "\t}TR;",
            "\tint id_O,fa[N],st[N],ed[N],son[N],top[N],deep[N],size[N];LL f[N][2],g[N][2];",
            "\tinline void dfs1(Re x,Re Fa){",
            "\t\tdeep[x]=deep[fa[x]=Fa]+1,size[x]=1;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=Fa){",
            "\t\t\t\tdfs1(to,x),size[x]+=size[to];",
            "\t\t\t\tif(size[to]>size[son[x]])son[x]=to;",
            "\t\t\t}",
            "\t}",
            "\tinline void dfs2(Re x,Re rt){",
            "\t\tidx[st[x]=++id_O]=x,ed[rt]=max(ed[rt],id_O),top[x]=rt;",
            "\t\tf[x][0]=g[x][0]=0,f[x][1]=g[x][1]=A[x];",
            "\t\tif(son[x])dfs2(son[x],rt),f[x][0]+=max(f[son[x]][0],f[son[x]][1]),f[x][1]+=f[son[x]][0];",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif((to=a[i].to)!=fa[x]&&to!=son[x]){",
            "\t\t\t\tdfs2(to,to);",
            "\t\t\t\tf[x][0]+=max(f[to][0],f[to][1]),f[x][1]+=f[to][0];",
            "\t\t\t\tg[x][0]+=max(f[to][0],f[to][1]),g[x][1]+=f[to][0];",
            "\t\t\t}",
            "\t\tG[x].a[1][1]=G[x].a[1][2]=g[x][0];",
            "\t\tG[x].a[2][1]=g[x][1],G[x].a[2][2]=-inf;",
            "\t}",
            "\tinline LL ask(){Mat F=TR.ask(1,1,n,st[1],ed[1]);return max(F.a[1][1],F.a[2][1]);}",
            "\tinline void change(Re x,Re v){//�޸�x�ĵ�Ȩ",
            "\t\tG[x].a[2][1]+=v-A[x],A[x]=v;//�޸�G[x][1]",
            "\t\twhile(x){",
            "\t\t\tMat F1=TR.ask(1,1,n,st[top[x]],ed[top[x]]);",
            "\t\t\tTR.change(1,1,n,st[x],G[x]);",
            "\t\t\tMat F2=TR.ask(1,1,n,st[top[x]],ed[top[x]]);",
            "\t\t\tx=fa[top[x]];",
            "\t\t\tG[x].a[1][1]+=max(F2.a[1][1],F2.a[2][1])-max(F1.a[1][1],F1.a[2][1]),G[x].a[1][2]=G[x].a[1][1];",
            "\t\t\tG[x].a[2][1]+=F2.a[1][1]-F1.a[1][1];",
            "\t\t}",
            "\t}",
            "}T1;",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),m=n-1;",
            "\tfor(Re i=1;i<=n;++i)in(A[i]);",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tT1.dfs1(1,0),T1.dfs2(1,1),T1.TR.build(1,1,n);",
            "\twhile(T--)in(x),in(y),T1.change(x,y),printf(\"%lld\\n\",T1.ask());",
            "}",
            ""
        ],
        "description": "插入 动态DP  (带修树上最大点权独立集)"
    }
}