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

{
    "[P5574]任务分配问题": {
        "prefix": "acm-5.1分治套莫队套树状数组",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=25003,M=27;",
            "int n,K,A[N];LL dp[M][N];",
            "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;",
            "}",
            "struct BIT{",
            "\tint nowL,nowR,C[N];LL ans;",
            "\tinline void add(Re x,Re v){if(!x)return;while(x<=n)C[x]+=v,x+=x&-x;}",
            "\tinline int ask(Re x){Re ans=0;while(x)ans+=C[x],x-=x&-x;return ans;}",
            "\tinline LL calc(Re L,Re R){",
            "\t\twhile(nowR<R)++nowR,ans+=ask(A[nowR]-1),add(A[nowR],1);",
            "\t\twhile(nowL>L)--nowL,ans+=nowR-nowL-ask(A[nowL]),add(A[nowL],1);",
            "\t\twhile(nowR>R)add(A[nowR],-1),ans-=ask(A[nowR]-1),--nowR;",
            "\t\twhile(nowL<L)add(A[nowL],-1),ans-=nowR-nowL-ask(A[nowL]),++nowL;",
            "\t\treturn ans;",
            "\t}",
            "}TR;",
            "inline void sakura(Re l,Re r,Re L,Re R,Re k){",
            "\tif(l>r)return;Re mid=l+r>>1,w=0;LL tmp;",
            "\tfor(Re j=L;j<=R&&j<mid;++j)",
            "\t\tif((tmp=dp[k-1][j]+TR.calc(j+1,mid))<=dp[k][mid])",
            "\t\t\tdp[k][mid]=tmp,w=j;",
            "\tsakura(l,mid-1,L,w,k),sakura(mid+1,r,w,R,k);",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(K);",
            "\tfor(Re i=1;i<=n;++i)in(A[i]);",
            "\tmemset(dp,19,sizeof(dp)),dp[0][0]=0;",
            "\tTR.nowL=1,TR.nowR=0;",
            "\tfor(Re k=1;k<=K;++k)sakura(k,n,k-1,n-1,k);",
            "\tprintf(\"%lld\\n\",dp[K][n]);",
            "}",
            ""
        ],
        "description": "插入 [P5574]任务分配问题"
    }
}