Submission #13892

#TimeUsernameProblemLanguageResultExecution timeMemory
13892comet역사적 조사 (JOI14_historical)C++98
100 / 100
924 ms199540 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...