传送们

简单的回滚莫队,调了半天发现排序的时候把m达成了n。。。

代码:

#include<bits/stdc++.h>
#define N 100005
#define ll long long
using namespace std;
ll cnt[N],tot[N],val[N],ans[N],rk[N],mp[N],tmp;
int n,m,blo[N],block,num,pos=1,siz;
struct Q{int l,r,id;}q[N];
inline ll read(){
    ll ans=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans;
}
inline void write(ll x){
    if(x>9)write(x/10);
    putchar((x%10)^48);
}
inline ll max(ll a,ll b){return a>b?a:b;}
inline bool cmp(Q a,Q b){return blo[a.l]==blo[b.l]?a.r<b.r:blo[a.l]<blo[b.l];}
inline void update(int x){++cnt[rk[x]],tmp=max(tmp,cnt[rk[x]]*val[x]);}
inline void reupdate(int x){--cnt[rk[x]];}
inline ll query(int ql,int qr){
    ll ret=0;
    for(int i=ql;i<=qr;++i)tot[rk[i]]=0;
    for(int i=ql;i<=qr;++i)++tot[rk[i]],ret=max(ret,tot[rk[i]]*val[i]);
    return ret;
}
inline int solve(int i,int blos){
    tmp=0;
    for(int j=1;j<=n;++j)cnt[j]=0;
    int L=min(blos*block,n),ql=L+1,qr=L;
    for(;blo[q[i].l]==blos;++i){
        if(blo[q[i].l]==blo[q[i].r]){ans[q[i].id]=query(q[i].l,q[i].r);continue;}
        while(qr<q[i].r)update(++qr);
        ll remov=tmp;
        while(ql>q[i].l)update(--ql);
        ans[q[i].id]=tmp,tmp=remov;
        while(ql<L+1)reupdate(ql++);
    }
    return i;
}
int main(){
    n=read(),m=read(),block=sqrt(n);
    for(int i=1;i<=n;++i)mp[i]=val[i]=read(),blo[i]=(i-1)/block+1;
    sort(mp+1,mp+n+1),siz=unique(mp+1,mp+n+1)-mp-1;
    for(int i=1;i<=n;++i)rk[i]=lower_bound(mp+1,mp+siz+1,val[i])-mp;
    for(int i=1;i<=m;++i)q[i].l=read(),q[i].r=read(),q[i].id=i;
    sort(q+1,q+m+1,cmp);
    for(int i=1;i<=blo[n];++i)pos=solve(pos,i);
    for(int i=1;i<=m;++i)write(ans[i]),puts("");
    return 0;
}
05-11 11:10