//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-2.6Minimax",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=3e5+3,P=998244353;",
            "int n,m,x,ans,inv1e4,f[N],b[N],p[N],A[N],fa[N],ch[N][3];",
            "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;",
            "}",
            "inline int mi(Re x,Re k){",
            "\tRe s=1;",
            "\twhile(k){",
            "\t\tif(k&1)s=(LL)s*x%P;",
            "\t\tx=(LL)x*x%P,k>>=1;",
            "\t}",
            "\treturn s;",
            "}",
            "inline int inv(Re x){return mi(x,P-2);}",
            "int pt[N];",
            "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 S,lp,rp,mul;}tr[N*19];",
            "\tinline void pushup(Re p){",
            "\t\ttr[p].S=(tr[pl].S+tr[pr].S)%P;",
            "\t}",
            "\tinline void updata(Re p,Re v){",
            "\t\ttr[p].S=(LL)tr[p].S*v%P,tr[p].mul=(LL)tr[p].mul*v%P;",
            "\t}",
            "\tinline void pushdown(Re p){",
            "\t\tif(tr[p].mul!=1)updata(pl,tr[p].mul),updata(pr,tr[p].mul),tr[p].mul=1;",
            "\t}",
            "\tinline void change(Re &p,Re L,Re R,Re x,Re v){",
            "\t\tif(!p)p=++O;tr[p].mul=1;",
            "\t\tif(L==R){tr[p].S=v;return;}",
            "\t\tpushdown(p);",
            "\t\tif(x<=mid)change(pl,L,mid,x,v);",
            "\t\telse change(pr,mid+1,R,x,v);",
            "\t\tpushup(p);",
            "\t}",
            "\tinline int merge(Re p,Re q,Re pv,Re qv,Re v){",
            "//\t\tprintf(\"pv=%d, qv=%d, v=%d\\n\",pv,qv,v);",
            "\t\tif(!p&&!q)return 0;",
            "\t\tif(!p){updata(q,qv);return q;}",
            "\t\tif(!q){updata(p,pv);return p;}",
            "\t\tpushdown(p),pushdown(q);",
            "\t\tRe plS=tr[pl].S,prS=tr[pr].S,qlS=tr[tr[q].lp].S,qrS=tr[tr[q].rp].S;",
            "//\t\tprintf(\"p:%d,%d q:%d,%d\\n\",tr[pl].S,tr[pr].S,tr[tr[q].lp].S,tr[tr[q].rp].S);",
            "\t\tpl=merge(pl,tr[q].lp,(pv+(1-v+P)%P*1ll*qrS%P)%P,(qv+(1-v+P)%P*1ll*prS%P)%P,v);",
            "\t\tpr=merge(pr,tr[q].rp,(pv+(LL)v*qlS%P)%P,(qv+(LL)v*plS%P)%P,v);",
            "\t\tpushup(p);",
            "\t\treturn p;",
            "\t}",
            "\tinline void dfs_(Re p,Re L,Re R){",
            "\t\tif(!p)return;",
            "\t\tif(L==R){f[L]=tr[p].S;return;}",
            "\t\tpushdown(p),dfs_(pl,L,mid),dfs_(pr,mid+1,R);",
            "\t}",
            "\tinline int ask(Re p,Re L,Re R,Re x){",
            "\t\tif(!p)return 0;",
            "\t\tif(L==R)return tr[p].S;",
            "\t\tpushdown(p);",
            "\t\tif(x<=mid)return ask(pl,L,mid,x);",
            "\t\telse return ask(pr,mid+1,R,x);",
            "\t}",
            "}T1;",
            "inline void dfs(Re x){",
            "\tif(!ch[x][0])T1.change(pt[x],1,m,A[x],1);",
            "\telse{",
            "//\t\tprintf(\"x=%d:\\n\",x);",
            "//\t\tfor(Re i=1;i<=m;++i)printf(\"%d \",T1.ask(pt[x],1,m,i));puts(\"\");",
            "\t\tdfs(ch[x][1]);",
            "\t\tif(ch[x][0]<2)pt[x]=pt[ch[x][1]];",
            "\t\telse dfs(ch[x][2]),pt[x]=T1.merge(pt[ch[x][1]],pt[ch[x][2]],0,0,p[x]);",
            "//\t\tprintf(\"x=%d:\\n\",x);",
            "//\t\tfor(Re i=1;i<=m;++i)printf(\"%d \",T1.ask(pt[x],1,m,i));puts(\"\");",
            "\t}",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),inv1e4=inv(10000);",
            "\tfor(Re i=1;i<=n;++i)in(fa[i]),ch[fa[i]][++ch[fa[i]][0]]=i;",
            "\tfor(Re i=1;i<=n;++i)",
            "\t\tif(ch[i][0])in(p[i]),p[i]=(LL)p[i]*inv1e4%P;",
            "\t\telse in(x),b[++m]=A[i]=x;",
            "\tsort(b+1,b+m+1);",
            "\tfor(Re i=1;i<=n;++i)if(!ch[i][0])A[i]=lower_bound(b+1,b+m+1,A[i])-b;",
            "//\tfor(Re i=1;i<=n;++i)if(!ch[i][0])printf(\"%d: %d\\n\",i,A[i]);",
            "\tdfs(1),T1.dfs_(1,1,m);",
            "//\tfor(Re i=1;i<=m;++i)printf(\"%d \",f[i]);puts(\"\");",
            "\tfor(Re i=1;i<=m;++i)(ans+=(LL)i*b[i]%P*f[i]%P*f[i]%P)%=P;",
            "\tprintf(\"%d\\n\",ans);",
            "}",
            ""
        ],
        "description": "插入 整体DP"
    }
}