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

{
    "曼哈顿距离最小生成树": {
        "prefix": "acm-3.2Object Clustering",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register LL",
            "using namespace std;",
            "const LL N=1e4+3;",
            "inline void in(Re &x){",
            "\tint f=0;x=0;char ch=getchar();",
            "\twhile(ch<'0'||ch>'9')f|=ch=='-',ch=getchar();",
            "\twhile(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();",
            "\tx=f?-x:x;",
            "}",
            "LL n,m,x,y,t,K,Ans,b[N];",
            "struct QAQ{LL x,y,v;inline bool operator<(const QAQ &O)const{return v<O.v;}}a[N<<3];",
            "struct Point{LL x,y,id;inline bool operator<(const Point &O)const{return x!=O.x?x<O.x:y<O.y;}}P[N];",
            "inline LL Abs(LL a){return a<0?-a:a;}",
            "inline LL Dis(Re x,Re y){return Abs(P[x].x-P[y].x)+Abs(P[x].y-P[y].y);}",
            "struct BIT{",
            "\tLL n,C[N],id[N];",
            "\tinline void CL(){memset(C,127,sizeof(C));}",
            "\tinline void add(Re x,Re v,Re ID){while(x){if(v<C[x])C[x]=v,id[x]=ID;x-=x&-x;}}",
            "\tinline LL ask(Re x){Re Min=1e18,ID=0;while(x<=n){if(C[x]<Min)Min=C[x],ID=id[x];x+=x&-x;}return ID;}",
            "}TR;",
            "inline LL find_(Re x){",
            "\tRe l=1,r=t;",
            "\twhile(l<r){",
            "\t\tRe mid=(l+r+1)>>1;",
            "\t\tif(b[mid]<=x)l=mid;",
            "\t\telse r=mid-1;",
            "\t}",
            "\treturn l;",
            "}",
            "LL fa[N];",
            "inline LL find(Re x){return fa[x]==x?x:fa[x]=find(fa[x]);}",
            "inline void kruscal(){",
            "\tfor(Re O=1;O<=4;++O){",
            "\t\tif(!(O&1))for(Re i=1;i<=n;++i)swap(P[i].x,P[i].y);",
            "\t\telse if(O==3)for(Re i=1;i<=n;++i)P[i].x*=-1;",
            "\t\tsort(P+1,P+n+1);",
            "\t\tfor(Re i=1;i<=n;++i)b[i]=P[i].y-P[i].x;",
            "\t\tsort(b+1,b+n+1),t=unique(b+1,b+n+1)-b-1;",
            "\t\tTR.n=t,TR.CL();",
            "\t\tfor(Re i=n;i>=1;--i){",
            "\t\t\tRe I=find_(P[i].y-P[i].x),j=TR.ask(I);",
            "\t\t\tif(j)a[++m]=(QAQ){P[i].id,P[j].id,Dis(i,j)};",
            "\t\t\tTR.add(I,P[i].x+P[i].y,i);",
            "\t\t}",
            "\t}",
            "\tsort(a+1,a+m+1);",
            "\tfor(Re i=1;i<=n;++i)fa[i]=i;",
            "\tfor(Re i=1,t=0;i<=m&&t<n-1;++i)if((x=find(a[i].x))!=(y=find(a[i].y))){",
            "\t\tfa[x]=y,Ans+=a[i].v;",
            "\t\tif(++t==K)printf(\"%lld\\n\",a[i].v);",
            "\t}",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(K),K=n-1-K+1;",
            "\tfor(Re i=1;i<=n;++i)in(P[i].x),in(P[i].y),P[i].id=i;",
            "\tkruscal();",
            "}",
            ""
        ],
        "description": "插入 曼哈顿距离最小生成树"
    }
}