MnZn刚学回滚莫队1ms,求条
查看原帖
MnZn刚学回滚莫队1ms,求条
823535
xht_G楼主2025/1/19 10:07

RT,暴力部分十分正确,但是看了一下发现莫队部分写挂了,求条

#include<bits/stdc++.h>
#define PII pair<int,int>
#define DB double
#define LL long long
#define comp complex<double>
#define int long long
//#define int __int128
//const int mod=1e9+7;
//const int mod=998244353;
const LL inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
const DB pi=acos(-1);
namespace FastIO{
	inline int read(int mod=0){
		int x=0,f=1;char ch=getchar();
		if(mod==0){while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}}
		else{while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x*10%mod+ch-48)%mod;ch=getchar();}}
		return x*f;}
	inline char cread(){char x;while(x==0||x==' '||x=='\n'||x=='\r')x=getchar();return x;}
	inline void write(int x){if(x<0)putchar('-'),x=-x;if(x==0)return;write(x/10),putchar(x%10+'0');}
}
using namespace FastIO;
using namespace std;
int a[100005],ans[100005];
int l[505],r[505],id[100005],len=500;
int b[100005],cnt[100005],c[100005],m=0;
struct Node{
	int l,r,id;
}qry[100005];
bool cmp(Node x,Node y){
	if(id[x.l]!=id[y.l])return id[x.l]<id[y.l];
	return x.r<y.r;
}
signed main(){
	int n=read(0),q=read(0);
	for(int i=1;i<=n;++i)a[i]=read(0),b[i]=a[i];
	sort(b+1,b+1+n),m=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<=n;++i)a[i]=lower_bound(b+1,b+1+m,a[i])-b;
	for(int i=1;i<=n;++i)id[i]=(i-1)/len+1; 
	for(int i=1;i<=n;i+=len)
		l[id[i]]=i,r[id[i]]=min(i+len-1,n);
	for(int i=1;i<=q;++i)qry[i].l=read(0),qry[i].r=read(0),qry[i].id=i;
	sort(qry+1,qry+1+q,cmp);
	for(int i=1,l=1,r=0,lst=0,now=0;i<=q;++i){
		l=qry[i].r;
		if(id[qry[i].l]!=id[qry[i-1].l]){
			r=l-1,now=0,lst=0;
			for(int i=1;i<=m;++i)c[i]=0;
		}
		if(id[qry[i].l]==id[qry[i].r]){
			int res=0;
			for(int j=qry[i].l;j<=qry[i].r;++j)++cnt[a[j]];
			for(int j=qry[i].l;j<=qry[i].r;++j)res=max(res,cnt[a[j]]*b[a[j]]);
			for(int j=qry[i].l;j<=qry[i].r;++j)--cnt[a[j]];
			ans[qry[i].id]=res;
			continue;
		}
		while(r<qry[i].r)++r,++c[a[r]],now=max(now,c[a[r]]*b[a[r]]);
		lst=now;
		while(l>qry[i].l)--l,++c[a[l]],now=max(now,c[a[l]]*b[a[l]]);
		ans[qry[i].id]=now;
		while(l<qry[i].r)--c[a[l]],++l;
		now=lst;
	}
	for(int i=1;i<=q;++i)printf("%lld\n",ans[i]);
	return 0;
}
2025/1/19 10:07
加载中...