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

{
    "小Z的袜子": {
        "prefix": "acm-2.9optimize",
        "body": [
            "#pragma GCC optimize(3,\"Ofast\",\"inline\")",
            "#pragma GCC optimize(2)",
            "#include<algorithm>",
            "#include<cstdio>",
            "#include<cmath>",
            "#define Re register int",
            "#define F(x,a,b) for(Re x=a;x<=b;++x)",
            "#define LL long long",
            "using std::sort;",
            "const int N=5e4+3;",
            "int n,q,t,L,R,ip,fu,len,gs[N],pt[N],co[N];LL ans,gcd,fz[N],fm[N];",
            "struct QAQ{int l,r,id;}Q[N];",
            "inline void in(Re &x){",
            "    x=fu=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 void sakura(Re p,Re x){ans-=(LL)gs[fu=co[p]]*gs[fu],gs[fu]+=x,ans+=(LL)gs[fu]*gs[fu];} ",
            "inline bool cmp(QAQ a,QAQ b){return pt[a.l]==pt[b.l]?a.r<b.r:a.l<b.l;}",
            "inline LL gcd_(LL a,LL b){while(b^=a^=b^=a%=b);return a;}",
            "int main(){",
            "\tin(n),in(q),t=sqrt(n);",
            "\tF(i,1,n)in(co[i]),pt[i]=i/t+1;",
            "\tF(i,1,q)in(Q[i].l),in(Q[i].r),Q[i].id=i;",
            "\tsort(Q+1,Q+q+1,cmp);",
            "\tL=1,R=0;",
            "\tF(i,1,q){",
            "\t\twhile(L<Q[i].l)sakura(L++,-1);",
            "\t\twhile(L>Q[i].l)sakura(--L,1);",
            "\t\twhile(R<Q[i].r)sakura(++R,1);",
            "\t\twhile(R>Q[i].r)sakura(R--,-1);",
            "\t\tip=Q[i].id;",
            "\t\tif(Q[i].l==Q[i].r){fz[ip]=0,fm[ip]=1;continue;}",
            "\t\tfz[ip]=ans-(len=Q[i].r-Q[i].l+1);",
            "\t\tfm[ip]=(LL)len*(len-1);",
            "\t\tgcd=gcd_(fz[ip],fm[ip]),fz[ip]/=gcd,fm[ip]/=gcd;",
            "\t}",
            "\tF(i,1,q)printf(\"%lld/%lld\\n\",fz[i],fm[i]);",
            "}",
            ""
        ],
        "description": "插入 小Z的袜子"
    }
}