//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.3LCT",
        "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];",
            "struct Link_Cut_Tree{",
            "\t#define pl tr[p].ps[0]",
            "\t#define pr tr[p].ps[1]",
            "\t#define pf tr[p].fa",
            "\tstruct QAQ{int fa,ps[2];}tr[N];Mat TR[N];",
            "\tinline void pushup(Re p){",
            "\t\tTR[p]=G[p];",
            "\t\tif(pl)TR[p]=TR[pl]*TR[p];",
            "\t\tif(pr)TR[p]=TR[p]*TR[pr];",
            "\t}",
            "\tinline int which(Re p){return tr[pf].ps[1]==p;}",
            "\tinline int nort(Re p){return tr[pf].ps[0]==p||tr[pf].ps[1]==p;}",
            "\tinline void connect(Re p,Re fa,Re o){tr[pf=fa].ps[o]=p;}",
            "\tinline void rotate(Re p){",
            "\t\tRe fa=pf,fas=which(p);",
            "\t\tRe pa=tr[fa].fa,pas=which(fa);",
            "\t\tRe x=tr[p].ps[fas^1];",
            "\t\tif(nort(fa))tr[pa].ps[pas]=p;pf=pa;",
            "\t\tconnect(x,fa,fas),connect(fa,p,fas^1);",
            "\t\tpushup(fa),pushup(p);",
            "\t}",
            "\tinline void splay(Re p){",
            "\t\tfor(Re fa;nort(p);rotate(p))",
            "\t\t\tif(nort(fa=pf))rotate(which(p)==which(fa)?fa:p);",
            "\t}",
            "\tinline void access(Re p){",
            "\t\tfor(Re son=0;p;son=p,p=pf){",
            "\t\t\tsplay(p);",
            "\t\t\tif(pr)G[p].a[1][1]+=max(TR[pr].a[1][1],TR[pr].a[2][1]),G[p].a[1][2]=G[p].a[1][1],G[p].a[2][1]+=TR[pr].a[1][1];",
            "\t\t\tpr=son;",
            "\t\t\tif(pr)G[p].a[1][1]-=max(TR[pr].a[1][1],TR[pr].a[2][1]),G[p].a[1][2]=G[p].a[1][1],G[p].a[2][1]-=TR[pr].a[1][1];",
            "\t\t\tpushup(p);",
            "\t\t}",
            "\t}",
            "\tinline void change(Re x,Re v){access(x),splay(x),G[x].a[2][1]+=v-A[x],A[x]=v,pushup(x);}//�޸�x�ĵ�Ȩ(G[x][1])",
            "\tinline LL ask(){splay(1);return max(TR[1].a[1][1],TR[1].a[2][1]);}",
            "}T1;",
            "LL g[N][2];",
            "inline void dfs(Re x,Re fa){",
            "\tg[x][1]=A[x],T1.tr[x].fa=fa;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)if((to=a[i].to)!=fa)",
            "\t\tdfs(to,x),g[x][0]+=max(g[to][0],g[to][1]),g[x][1]+=g[to][0];",
            "\tG[x].a[1][1]=G[x].a[1][2]=g[x][0];",
            "\tG[x].a[2][1]=g[x][1],G[x].a[2][2]=-inf;",
            "\tT1.pushup(x);",
            "}",
            "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);",
            "\tdfs(1,0);",
            "\twhile(T--)in(x),in(y),T1.change(x,y),printf(\"%lld\\n\",T1.ask());",
            "}",
            ""
        ],
        "description": "插入 动态DP  (带修树上最大点权独立集)"
    }
}