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

{
    "CDQ分治": {
        "prefix": "acm-2.12三维偏序",
        "body": [
            "//#pragma GCC optimize(3,\"Ofast\",\"inline\")",
            "//#pragma GCC optimize(2)",
            "#include<algorithm>",
            "#include<cstdio>",
            "#define Re register int",
            "using std::sort;",
            "const int N=1e5+5,K=2e5+5;",
            "int n,m,k,C[K],ans[N];",
            "struct QAQ{int a,b,c,cnt,ans;}a[N],Q[N];",
            "inline void in(Re &x){",
            "    int fu=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')fu|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=fu?-x:x;",
            "}",
            "inline bool cmp1(QAQ p,QAQ o){return p.a!=o.a?p.a<o.a:(p.b!=o.b?p.b<o.b:p.c<o.c);}",
            "inline bool cmp2(QAQ p,QAQ o){return p.b<o.b;}",
            "inline void add(Re x,Re v){while(x<=k)C[x]+=v,x+=x&-x;}",
            "inline int ask(Re x){",
            "\tRe ans=0;",
            "\twhile(x)ans+=C[x],x-=x&-x;",
            "\treturn ans;",
            "}",
            "inline void CDQ(Re L,Re R){",
            "\tif(L==R)return;",
            "\tRe mid=L+R>>1;",
            "\tCDQ(L,mid),CDQ(mid+1,R);",
            "\tsort(Q+L,Q+mid+1,cmp2),sort(Q+mid+1,Q+R+1,cmp2);",
            "\tRe j=L;",
            "\tfor(Re i=mid+1;i<=R;++i){",
            "\t\twhile(j<=mid&&Q[j].b<=Q[i].b)add(Q[j].c,Q[j].cnt),++j;",
            "\t\tQ[i].ans+=ask(Q[i].c);",
            "\t}",
            "\tfor(Re i=L;i<j;++i)add(Q[i].c,-Q[i].cnt);",
            "}",
            "int main(){",
            "\tin(n),in(k);",
            "\tfor(Re i=1;i<=n;++i)in(a[i].a),in(a[i].b),in(a[i].c);",
            "\tsort(a+1,a+n+1,cmp1);",
            "\tfor(Re i=1;i<=n;++i){",
            "\t\tif(i>2&&a[i].a==a[i-1].a&&a[i].b==a[i-1].b&&a[i].c==a[i-1].c)++Q[m].cnt;",
            "\t\telse Q[++m]=a[i],Q[m].cnt=1;",
            "\t}",
            "\tCDQ(1,m);",
            "\tfor(Re i=1;i<=m;++i)ans[Q[i].ans+Q[i].cnt-1]+=Q[i].cnt;",
            "\tfor(Re i=0;i<n;++i)printf(\"%d\\n\",ans[i]);",
            "}",
            ""
        ],
        "description": "插入 CDQ分治"
    }
}