//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 的格式要求是 "插入 [该代码片段的中文名]"

{
    "线段树优化建边+Dijkstra最短路": {
        "prefix": "acm-3.7Legacy",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3;",
            "const LL inf=1e18;",
            "int n,m,x,y,w,l,r,o,op,st,head[N<<3];bool pan[N<<3];LL dis[N<<3];",
            "struct QAQ{int w,to,next;}a[(N<<3)+(N<<2)+N*18*2];",
            "inline void add(Re x,Re y,Re z){a[++o].w=z,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 (p<<1)",
            "\t#define pr (p<<1|1)",
            "\t#define mid ((L+R)>>1)",
            "\tint O;",
            "\tstruct QAQ{int in,out;}tr[N<<2];",
            "\tinline void build(Re p,Re L,Re R){",
            "\t\tif(L==R){tr[p].in=tr[p].out=L;return;}",
            "\t\ttr[p].in=++O,tr[p].out=++O,add(tr[p].in,tr[p].out,0);",
            "\t\tbuild(pl,L,mid),build(pr,mid+1,R);",
            "\t\tadd(tr[p].in,tr[pl].in,0),add(tr[p].in,tr[pr].in,0);",
            "\t\tadd(tr[pl].out,tr[p].out,0),add(tr[pr].out,tr[p].out,0);",
            "\t}",
            "\tinline void change(Re p,Re L,Re R,Re l,Re r,Re x,Re w,Re op){",
            "\t\tif(l<=L&&R<=r){",
            "\t\t\tif(op)add(tr[p].out,x,w);",
            "\t\t\telse add(x,tr[p].in,w);",
            "\t\t\treturn;",
            "\t\t}",
            "\t\tif(l<=mid)change(pl,L,mid,l,r,x,w,op);",
            "\t\tif(r>mid)change(pr,mid+1,R,l,r,x,w,op);",
            "\t}",
            "}TR;",
            "struct QWQ{int x;LL d;inline bool operator<(const QWQ &O)const{return d>O.d;}};",
            "priority_queue<QWQ>Q;",
            "inline void dijkstra(Re st){",
            "\tfor(Re i=1;i<=TR.O;++i)dis[i]=inf;",
            "\tQ.push((QWQ){st,dis[st]=0});",
            "\twhile(!Q.empty()){",
            "\t\tRe x=Q.top().x;Q.pop();",
            "\t\tif(pan[x])continue;",
            "\t\tpan[x]=0;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif(dis[to=a[i].to]>dis[x]+a[i].w)",
            "\t\t\t\tQ.push((QWQ){to,dis[to]=dis[x]+a[i].w});",
            "\t}",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(m),in(st);",
            "\tTR.O=n,TR.build(1,1,n);",
            "\twhile(m--){",
            "\t\tin(op),in(x);",
            "\t\tif(op<2)in(y),in(w),TR.change(1,1,n,y,y,x,w,0);",
            "\t\telse if(op<3)in(l),in(r),in(w),TR.change(1,1,n,l,r,x,w,0);",
            "\t\telse in(l),in(r),in(w),TR.change(1,1,n,l,r,x,w,1);",
            "\t}",
            "\tdijkstra(st);",
            "\tfor(Re i=1;i<=n;++i)printf(\"%lld \",dis[i]==inf?-1:dis[i]);",
            "}",
            ""
        ],
        "description": "插入 线段树优化建边+Dijkstra最短路"
    }
}