//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  (Treeland Tour)": {
        "prefix": "acm-2.6树上最长上升子序列",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=6003;",
            "int n,m,x,y,t,o,Ans,b[N],A[N],pt[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 Segment_Tree{",
            "\t#define pl (tr[p].lp)",
            "\t#define pr (tr[p].rp)",
            "\t#define mid ((L+R)>>1)",
            "\tint O;",
            "\tstruct QAQ{int lp,rp,up,down;}tr[N*13*2];",
            "\tinline void pushup(Re p){",
            "\t\ttr[p].up=max(tr[pl].up,tr[pr].up);",
            "\t\ttr[p].down=max(tr[pl].down,tr[pr].down);",
            "\t}",
            "\tinline void change1(Re &p,Re L,Re R,Re x,Re v){",
            "\t\tif(!p)p=++O;",
            "\t\tif(L==R){tr[p].up=max(tr[p].up,v);return;}",
            "\t\tif(x<=mid)change1(pl,L,mid,x,v);",
            "\t\telse change1(pr,mid+1,R,x,v);",
            "\t\tpushup(p);",
            "\t}",
            "\tinline void change2(Re &p,Re L,Re R,Re x,Re v){",
            "\t\tif(!p)p=++O;",
            "\t\tif(L==R){tr[p].down=max(tr[p].down,v);return;}",
            "\t\tif(x<=mid)change2(pl,L,mid,x,v);",
            "\t\telse change2(pr,mid+1,R,x,v);",
            "\t\tpushup(p);",
            "\t}",
            "\tinline int ask1(Re p,Re L,Re R,Re l,Re r){",
            "\t\tif(!p)return 0;",
            "\t\tif(l<=L&&R<=r)return tr[p].up;",
            "\t\tRe ans=0;",
            "\t\tif(l<=mid)ans=max(ans,ask1(pl,L,mid,l,r));",
            "\t\tif(r>mid)ans=max(ans,ask1(pr,mid+1,R,l,r));",
            "\t\treturn ans;",
            "\t}",
            "\tinline int ask2(Re p,Re L,Re R,Re l,Re r){",
            "\t\tif(!p)return 0;",
            "\t\tif(l<=L&&R<=r)return tr[p].down;",
            "\t\tRe ans=0;",
            "\t\tif(l<=mid)ans=max(ans,ask2(pl,L,mid,l,r));",
            "\t\tif(r>mid)ans=max(ans,ask2(pr,mid+1,R,l,r));",
            "\t\treturn ans;",
            "\t}",
            "\tinline int merge(Re p,Re q,Re L,Re R){",
            "\t\tif(!p||!q)return p+q;",
            "\t\tif(L==R){tr[p].up=max(tr[p].up,tr[q].up),tr[p].down=max(tr[p].down,tr[q].down);return p;}",
            "\t\tAns=max(Ans,max(tr[pl].up+tr[tr[q].rp].down,tr[pr].down+tr[tr[q].lp].up));",
            "\t\tpl=merge(pl,tr[q].lp,L,mid);",
            "\t\tpr=merge(pr,tr[q].rp,mid+1,R);",
            "\t\tpushup(p);return p;",
            "\t}",
            "}TR;",
            "inline void dfs(Re x,Re fa){",
            "\tRe up=0,down=0;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=fa){",
            "\t\t\tdfs(to,x);",
            "\t\t\tRe tmp1=TR.ask1(pt[to],1,t,1,A[x]-1),tmp2=TR.ask2(pt[to],1,t,A[x]+1,t);",
            "\t\t\tAns=max(Ans,max(up+tmp2+1,down+tmp1+1));",
            "\t\t\tup=max(up,tmp1),down=max(down,tmp2);",
            "\t\t\tpt[x]=TR.merge(pt[x],pt[a[i].to],1,t);",
            "\t\t}",
            "\tTR.change1(pt[x],1,t,A[x],up+1),TR.change2(pt[x],1,t,A[x],down+1);",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),m=n-1;",
            "\tfor(Re i=1;i<=n;++i)in(A[i]),b[i]=A[i];",
            "\tsort(b+1,b+n+1),t=unique(b+1,b+n+1)-b;",
            "\tfor(Re i=1;i<=n;++i)A[i]=lower_bound(b+1,b+t+1,A[i])-b;",
            "\twhile(m--)in(x),in(y),add(x,y),add(y,x);",
            "\tdfs(1,0),printf(\"%d\\n\",Ans);",
            "}",
            ""
        ],
        "description": "插入 整体DP  (Treeland Tour)"
    }
}