标签:变量 mat max output 转移 回滚 一个 out --
Input
Output
Sample Input
5 5 9 8 7 8 9 1 2 3 4 4 4 1 4 2 4
Sample Output
9 8 8 16 16
Hint
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> using namespace std; struct xxx{ int l,r,lr,lblock,rblock,id; }q[101000]; struct xxx2{ int a,id; }data[101000]; int cnt[101000],a[101000],b[101000],cnt2[101000];long long ans[101000]; bool cmp(xxx a,xxx b){return a.lblock!=b.lblock?a.lblock<b.lblock:a.r<b.r;} bool cmp2(xxx2 a,xxx2 b){return a.a<b.a;} int main() { int n,Q;scanf("%d%d",&n,&Q);int T=(int)sqrt((double)n); for(int i=1;i<=n;i++){scanf("%d",&data[i].a);data[i].id=i;} sort(data+1,data+n+1,cmp2); int last=0; for(int i=1;i<=n;i++) { if(data[i].a==data[i-1].a)a[data[i].id]=last; else a[data[i].id]=++last; b[data[i].id]=data[i].a; } for(int i=1;i<=Q;i++) { scanf("%d%d",&q[i].l,&q[i].r); q[i].lblock=(q[i].l-1)/T+1;q[i].rblock=(q[i].r-1)/T+1; q[i].id=i;q[i].lr=T*q[i].lblock; } sort(q+1,q+Q+1,cmp); int l=1,r=0;long long Max=-1; for(int i=1;i<=Q;i++) { if(q[i].lblock==q[i].rblock) { for(int j=q[i].l;j<=q[i].r;j++){cnt2[a[j]]++;ans[q[i].id]=max(ans[q[i].id],1ll*cnt2[a[j]]*b[j]);} for(int j=q[i].l;j<=q[i].r;j++)cnt2[a[j]]--;continue; } if(q[i].lr!=l){l=q[i].lr,r=q[i].lr-1;memset(cnt,0,sizeof(cnt));Max=-1;} while(r<q[i].r){cnt[a[++r]]++;Max=max(Max,1ll*cnt[a[r]]*b[r]);} long long Max2=Max;int _l=l; while(_l>q[i].l){cnt[a[--_l]]++;Max=max(Max,1ll*cnt[a[_l]]*b[_l]);} for(int j=q[i].l;j<l;j++)cnt[a[j]]--; ans[q[i].id]=Max;Max=Max2; } for(int i=1;i<=Q;i++)printf("%lld\n",ans[i]); return 0; }
标签:变量 mat max output 转移 回滚 一个 out --
原文地址:http://www.cnblogs.com/lher/p/7875480.html