求助!分块做法
查看原帖
求助!分块做法
112631
Lovable_Wind楼主2021/8/11 18:34
#include<bits/stdc++.h>
using namespace std;
const double pi=3.14;
const int inf=0x3f3f3f3f;
const int NIL=-1;
int maxx[5001],l[5001],r[5001],pos[200010];
int a[200010];
int n,q;
void build(){
	int dis=sqrt(n);
	int num=ceil(n*1.0/dis);
	for (int i=1;i<=num;i++){
		l[i]=(i-1)*dis+1;
		r[i]=i*dis;
	}
	r[num]=n;
	for (int i=1;i<=n;i++){
		pos[i]=((i-1)/dis)+1; 
	}
}
void Update(int x,int y){
	if (a[x]<y){
		a[x]=y;
		maxx[pos[x]]=max(maxx[pos[x]],a[x]);
	}
}
int Query(int ll,int rr){
	int res=0;
	for (int i=ll;i<=r[pos[ll]];i++){
		res=max(res,a[i]);
	}
	for (int i=l[pos[rr]];i<=rr;i++){
		res=max(res,a[i]);
	}
	for (int i=pos[ll]+1;i<=pos[rr]-1;i++){
		res=max(maxx[i],res); 
	}
	return res;
}
int read()
{
    int ans=0,flag=1;
    char ch=getchar();
    while( (ch>'9' || ch<'0') && ch!='-' ) ch=getchar();
    if(ch=='-') flag=-1,ch=getchar();
    while(ch>='0' && ch<='9') ans=ans*10+ch-'0',ch=getchar();
    return ans*flag;
}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	cin>>n>>q;
	for (int i=1;i<=n;i++)
		cin>>a[i];
	build();
	int j=1;
	for (int i=1;i<=n;i++){
		if (i>r[j]) j++;
		maxx[j]=max(maxx[j],a[i]);
	}
	for (int i=1;i<=q;i++){
		char opt;
		cin>>opt;
		int x=read(),y=read();
		if (opt=='Q'){
			cout<<Query(x,y)<<endl;
		}else{
			Update(x,y);
		}
	}
}

不知为何,现在一直是20pts

2021/8/11 18:34
加载中...