15tps,TLE求条
查看原帖
15tps,TLE求条
734533
封禁用户楼主2023/8/1 12:40

测评记录

RT,T了19个点,又是死活调不出来pwp。

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10;
int n,q;
int c[N],b[N],cnt[N];
int len,where[N];
int l_where[N],r_where[N];//第i个块的左右端点的位置
struct node{
	int l,r,id;
}Q[N];
bool cmp(node a,node b){
	if(a.l/len!=b.l/len){
		return a.l<b.l;
	}
	else{
		return a.r<b.r;
	}
}
int now;
void get_lr(){
	now=1;
	for(;now<=n/len;now++){
		l_where[now]=(now-1)*len+1;
		r_where[now]=(now)*len;
	}
	if(r_where[now]<n){
		l_where[++now]=r_where[now]+1;
		r_where[now]=n;
	}
}
void get_where(){
	for(int i=1;i<=now;i++){
		for(int j=l_where[i];j<=r_where[i];j++){
			where[j]=i;
		}
	}
}
int add(int x){
	cnt[x]++;
	return b[x]*cnt[x];	
}
void del(int x){
	cnt[x]--;
}
int ANS[N];
void solve(){
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>c[i],b[i]=c[i];
	}
	sort(b+1,b+n+1);
	int idx=unique(b+1,b+n+1)-(b+1);
	for(int i=1;i<=n;i++){
		c[i]=lower_bound(b+1,b+idx+1,c[i])-b;
	}
	len=sqrt(n);
	get_lr();
	for(int i=1;i<=q;i++){
		cin>>Q[i].l>>Q[i].r,Q[i].id=i;
	}
	get_where();
	sort(Q+1,Q+q+1,cmp);
	int l=1,r=0,last_where,last_l=0;
	int maxx=0;
	for(int i=1;i<=q;i++){
		if(where[Q[i].l]==where[Q[i].r]){//在一个块里 
			int now_max=0;
			for(int j=Q[i].l;j<=Q[i].r;j++){
				cnt[c[j]]++;
			}
			for(int j=Q[i].l;j<=Q[i].r;j++){
				now_max=max(now_max,b[c[j]]*cnt[c[j]]);
			}
			for(int j=Q[i].l;j<=Q[i].r;j++){
				cnt[c[j]]--;
			}
			ANS[Q[i].id]=now_max;
		}
		else{
			if(where[Q[i].l]!=last_where){//在新的块里
				//弄成:l=k+1,r=k;k是Q[i].l所在块的右端 
				while(r>r_where[where[Q[i].l]]){
					del(c[r--]);
				}
				while(l<r_where[where[Q[i].l]]+1){
					del(c[l++]);
				}
				last_where=where[Q[i].l];
				maxx=0;
			}	//cout<<l<<" "<<r<<"\n";
			while(r<Q[i].r){
				maxx=max(maxx,add(c[++r]));
			}
			int now_max=maxx;
			last_l=l;
			while(last_l>Q[i].l){
				now_max=max(now_max,add(c[--last_l]));
			}
			ANS[Q[i].id]=now_max;
			while(last_l<l){
				del(c[last_l++]);
			}
		
		}
	}
	for(int i=1;i<=q;i++){
		cout<<ANS[i]<<"\n";
	}
}
signed main(){
	solve();
	return 0;
}
2023/8/1 12:40
加载中...