标签:clu i++ n+1 scan ras sqrt str can long
关于回滚莫队见这篇博客。
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
const int maxt=350;
int n,Q,t,num,block,nowl,nowr;
int a[maxn],b[maxn],L[maxt],R[maxt],pos[maxn],cnt[maxn],tmpcnt[maxn];
ll nowans;
ll ans[maxn];
struct Query{int l,r,id;}qr[maxn];
inline bool cmp(Query x,Query y){return (pos[x.l]==pos[y.l])?(x.r<y.r):(pos[x.l]<pos[y.l]);}
inline void erase(int x){cnt[a[x]]--;}
inline void add(int x)
{
cnt[a[x]]++;
nowans=max(nowans,1ll*cnt[a[x]]*b[a[x]]);
}
int main()
{
scanf("%d%d",&n,&Q);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+n+1);num=unique(b+1,b+n+1)-(b+1);
for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+num+1,a[i])-b;
t=sqrt(n);block=n/t;
if(n%t)block++;
for(int i=1;i<=block;i++)L[i]=(i-1)*t+1,R[i]=min(i*t,n);
for(int i=1;i<=n;i++)pos[i]=(i-1)/t+1;
for(int i=1;i<=Q;i++)scanf("%d%d",&qr[i].l,&qr[i].r),qr[i].id=i;
sort(qr+1,qr+Q+1,cmp);
int last=0;nowl=1,nowr=0;
for(int i=1;i<=Q;i++)
{
if(pos[qr[i].l]==pos[qr[i].r])
{
ll res=0;
for(int j=qr[i].l;j<=qr[i].r;j++)tmpcnt[a[j]]++;
for(int j=qr[i].l;j<=qr[i].r;j++)res=max(res,1ll*tmpcnt[a[j]]*b[a[j]]);
for(int j=qr[i].l;j<=qr[i].r;j++)tmpcnt[a[j]]--;
ans[qr[i].id]=res;
continue;
}
if(pos[qr[i].l]!=last)
{
while(nowr>R[pos[qr[i].l]])erase(nowr--);
while(nowl<R[pos[qr[i].l]]+1)erase(nowl++);
nowans=0;last=pos[qr[i].l];
}
while(nowr<qr[i].r)add(++nowr);
ll tmp=nowans;int tmpl=nowl;
while(nowl>qr[i].l)add(--nowl);
ans[qr[i].id]=nowans;
while(nowl<tmpl)erase(nowl++);
nowans=tmp;
}
for(int i=1;i<=Q;i++)printf("%lld\n",ans[i]);
return 0;
}
标签:clu i++ n+1 scan ras sqrt str can long
原文地址:https://www.cnblogs.com/nofind/p/12065821.html