看到dalao们都捉了此题
第一眼:这tm不就是区间众数嘛
然后脑子有坑去学回滚莫队
没学会,大力搞分块我还能在线呢~~~~
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x; } int a[110000],lslen,ls[110000]; int block,st[110000]; int s[410][110000],c[110000],tim,ti[110000]; LL mx[410][410]; int main() { freopen("a.in","r",stdin); freopen("a.out","w",stdout); int n=read(),Q=read(); lslen=0; for(int i=1;i<=n;i++) a[i]=read(), ls[++lslen]=a[i]; sort(ls+1,ls+lslen+1); lslen=unique(ls+1,ls+lslen+1)-ls-1; for(int i=1;i<=n;i++) a[i]=lower_bound(ls+1,ls+lslen+1,a[i])-ls; block=int(sqrt(double(n+1)))+1; for(int i=1;i<=n;i++)st[i]=(i-1)/block+1; memset(s,0,sizeof(s)); for(int i=1;i<=block;i++) { LL ans=0,id=i; for(int j=(i-1)*block+1;j<=n;j++) { s[i][a[j]]++; if( (LL(s[i][a[j]]))*(LL(ls[a[j]]))>ans ) ans=(LL(s[i][a[j]]))*(LL(ls[a[j]])); if(j%block==0)mx[i][id]=ans,id++; } } int l,r; tim=0;memset(ti,0,sizeof(ti)); for(int i=1;i<=Q;i++) { l=read();r=read(); if(st[l]==st[r]) { LL ans=0;tim++; for(int j=l;j<=r;j++) { if(ti[a[j]]!=tim) ti[a[j]]=tim, c[a[j]]=0; c[a[j]]++; if( (LL(c[a[j]]))*(LL(ls[a[j]]))>ans ) ans=(LL(c[a[j]]))*(LL(ls[a[j]])); } printf("%lld ",ans); } else { LL ans=mx[st[l]+1][st[r]-1]; tim++; for(int j=l;j<=st[l]*block;j++) { if(ti[a[j]]!=tim) ti[a[j]]=tim, c[a[j]]=0; c[a[j]]++; if( (LL(c[a[j]]+s[st[l]+1][a[j]]-s[st[r]][a[j]]))*(LL(ls[a[j]]))>ans ) ans=(LL(c[a[j]]+s[st[l]+1][a[j]]-s[st[r]][a[j]]))*(LL(ls[a[j]])); } for(int j=(st[r]-1)*block+1;j<=r;j++) { if(ti[a[j]]!=tim) ti[a[j]]=tim, c[a[j]]=0; c[a[j]]++; if( (LL(c[a[j]]+s[st[l]+1][a[j]]-s[st[r]][a[j]]))*(LL(ls[a[j]]))>ans ) ans=(LL(c[a[j]]+s[st[l]+1][a[j]]-s[st[r]][a[j]]))*(LL(ls[a[j]])); } printf("%lld ",ans); } } return 0; }