//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-2.9歴史の研究",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<cmath>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1e5+3,M=1e5+3;",
            "int n,m,T,BL,BLN,A[N],b[N],Li[325],Ri[325],cnt[N],cnt_[N],belong[N];LL Ans[M];",
            "struct Query{",
            "\tint l,r,id;",
            "\tinline bool operator<(const Query &O)const{",
            "\t\tRe B1=belong[l],B2=belong[O.l];",
            "\t\treturn B1!=B2?B1<B2:r<O.r;",
            "\t}",
            "}Q[M];",
            "inline void in(Re &x){",
            "    Re 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;",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(T),BL=sqrt(n)+1,BLN=n/BL+1;",
            "\tfor(Re i=1;i<=BLN;++i){",
            "\t\tLi[i]=(i-1)*BL+1,Ri[i]=min(i*BL,n);",
            "\t\tfor(Re j=Li[i];j<=Ri[i];++j)belong[j]=i;",
            "\t}",
            "\tfor(Re i=1;i<=n;++i)in(A[i]),b[i]=A[i];",
            "\tsort(b+1,b+n+1),m=unique(b+1,b+n+1)-b-1;",
            "\tfor(Re i=1;i<=n;++i)A[i]=lower_bound(b+1,b+m+1,A[i])-b;",
            "\tfor(Re i=1;i<=T;++i)in(Q[i].l),in(Q[i].r),Q[i].id=i;",
            "\tsort(Q+1,Q+T+1);Re i=1;",
            "\tfor(Re p=1;p<=BLN;++p){",
            "\t\tRe nowL=Ri[p]+1,nowR=Ri[p];LL ans=0;",
            "\t\tmemset(cnt,0,sizeof(cnt));",
            "\t\twhile(belong[Q[i].l]==p){",
            "\t\t\tRe L=Q[i].l,R=Q[i].r;",
            "\t\t\tif(belong[L]==belong[R]){",
            "\t\t\t\tLL tmp=0;",
            "\t\t\t\tfor(Re j=L;j<=R;++j)cnt_[A[j]]=0;",
            "\t\t\t\tfor(Re j=L;j<=R;++j)++cnt_[A[j]],tmp=max(tmp,(LL)b[A[j]]*cnt_[A[j]]);",
            "\t\t\t\tAns[Q[i].id]=tmp;",
            "\t\t\t}",
            "\t\t\telse{",
            "\t\t\t\twhile(nowR<R)cnt[A[++nowR]]++,ans=max(ans,(LL)b[A[nowR]]*cnt[A[nowR]]);",
            "\t\t\t\tLL tmp=ans;",
            "\t\t\t\twhile(nowL>L)cnt[A[--nowL]]++,tmp=max(tmp,(LL)b[A[nowL]]*cnt[A[nowL]]);",
            "\t\t\t\tAns[Q[i].id]=tmp;",
            "\t\t\t\twhile(nowL<Ri[p]+1)--cnt[A[nowL++]];",
            "\t\t\t}",
            "\t\t\t++i;",
            "\t\t}",
            "\t}",
            "\tfor(Re i=1;i<=T;++i)printf(\"%lld\\n\",Ans[i]);",
            "}",
            ""
        ],
        "description": "插入 回滚莫队"
    }
}