This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
int N,Q,x[100010],c[100010][500],p[100010],s[100010],sz;
ll d[500][500];
int main(){
scanf("%d%d",&N,&Q);
for(int i=0;i<N;i++)scanf("%d",&x[i]),s[sz++]=x[i];
sort(s,s+sz);
sz=unique(s,s+sz)-s;
for(int i=0;i<N;i++)x[i]=lower_bound(s,s+sz,x[i])-s;
for(int i=0;i<N;i++){
if(!(i&255)){
for(int j=0;j<sz;j++)c[j][(i>>8)+1]=c[j][i>>8];
for(int j=0;j<=(i>>8);j++)d[j][(i>>8)+1]=d[j][i>>8];
}
c[x[i]][(i>>8)+1]++;
for(int j=0;j<=(i>>8);j++)d[j][(i>>8)+1]=max(d[j][(i>>8)+1],1ll*s[x[i]]*(c[x[i]][(i>>8)+1]-c[x[i]][j]));
}
int S,E,l,r;
ll ans;
while(Q--){
scanf("%d%d",&S,&E);
S--;
l=(S+255)>>8;
r=E>>8;
if(l>r){
ans=0;
for(int i=S;i<E;i++)p[x[i]]++;
for(int i=S;i<E;i++)ans=max(ans,1ll*s[x[i]]*p[x[i]]),p[x[i]]--;
}
else{
ans=d[l][r];
for(int i=S;i<(l<<8);i++)p[x[i]]++;
for(int i=(r<<8);i<E;i++)p[x[i]]++;
for(int i=S;i<(l<<8);i++)ans=max(ans,1ll*s[x[i]]*(p[x[i]]+c[x[i]][r]-c[x[i]][l])),p[x[i]]--;
for(int i=(r<<8);i<E;i++)ans=max(ans,1ll*s[x[i]]*(p[x[i]]+c[x[i]][r]-c[x[i]][l])),p[x[i]]--;
}
printf("%lld\n",ans);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |