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>
int a[111111];
int t[111111],tn;
int c[111111][555];
long long d[555][555];
int p[111111];
int main()
{
long long res;
int i,j,k,s,e,n,m,l,r;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
t[i]=a[i];
}
std::sort(t,t+n);
tn=std::unique(t,t+n)-t;
for(i=0;i<n;i++)a[i]=std::lower_bound(t,t+tn,a[i])-t;
for(i=0;i<n;i++)
{
if(!(i&255))
{
for(j=0;j<tn;j++)c[j][(i>>8)+1]=c[j][i>>8];
for(j=0;j<(i>>8);j++)d[j][(i>>8)+1]=d[j][i>>8];
}
c[a[i]][(i>>8)+1]++;
for(j=0;j<=(i>>8);j++)if(1LL*t[a[i]]*(c[a[i]][(i>>8)+1]-c[a[i]][j])>d[j][(i>>8)+1])d[j][(i>>8)+1]=1LL*t[a[i]]*(c[a[i]][(i>>8)+1]-c[a[i]][j]);
}
while(m--)
{
scanf("%d%d",&s,&e);
s--;
l=(s+255)>>8;
r=e>>8;
if(l<r)
{
res=d[l][r];
for(i=s;i<(l<<8);i++)p[a[i]]++;
for(i=r<<8;i<e;i++)p[a[i]]++;
for(i=s;i<(l<<8);i++)
{
if(1LL*t[a[i]]*(p[a[i]]+c[a[i]][r]-c[a[i]][l])>res)res=1LL*t[a[i]]*(p[a[i]]+c[a[i]][r]-c[a[i]][l]);
p[a[i]]--;
}
for(i=r<<8;i<e;i++)
{
if(1LL*t[a[i]]*(p[a[i]]+c[a[i]][r]-c[a[i]][l])>res)res=1LL*t[a[i]]*(p[a[i]]+c[a[i]][r]-c[a[i]][l]);
p[a[i]]--;
}
}
else
{
res=0;
for(i=s;i<e;i++)p[a[i]]++;
for(i=s;i<e;i++)
{
if(1LL*t[a[i]]*p[a[i]]>res)res=1LL*t[a[i]]*p[a[i]];
p[a[i]]--;
}
}
printf("%lld\n",res);
}
}
# | 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... |