萌新刚学ST表,5pts代码玄关求条
查看原帖
萌新刚学ST表,5pts代码玄关求条
1026467
_Mount_楼主2025/1/18 10:12

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e5+10,M = 25,inf = LONG_LONG_MAX;
int n,m,k,Q;
int a1[M][N],a2[M][N],a3[M][N],a4[M][N],b1[M][N],b2[M][N];//1是最大值,2是最小值,3是负数最大值,4是非负数最小值
int la,ra,lb,rb,ka,kb,pa,pb;
int ansA1,ansA2,ansA3,ansA4,ansB1,ansB2;

inline int read(){
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    return x*f;
}

signed main(){
	/*
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);
	*/
	n = read(),m = read(),Q = read();
	for(int i = 1,x;i <= n;++ i){
		x = read();
		a1[0][i] = a2[0][i] = x;
		if(x < 0) a3[0][i] = x,a4[0][i] = inf;	
		else a4[0][i] = x,a3[0][i] = -inf;
	}
	for(int i = 1,x;i <= m;++ i){
		x = read();
		b1[0][i] = b2[0][i] = x;
	}
	k = log2(n)+1;
	for(int i = 1;i <= k;++ i){
		for(int j = 1;j <= n-(1<<i)+1;++ j){
			a1[i][j] = max(a1[i-1][j],a1[i-1][j+(1<<i-1)]);
			a2[i][j] = min(a2[i-1][j],a2[i-1][j+(1<<i-1)]);
			a3[i][j] = max(a3[i-1][j],a3[i-1][j+(1<<i-1)]);
			a4[i][j] = min(a4[i-1][j],a4[i-1][j+(1<<i-1)]);
		}
	}
	k = log2(m)+1;
	for(int i = 1;i <= k;++ i){
		for(int j = 1;j <= m-(1<<i)+1;++ j){
			b1[i][j] = max(b1[i-1][j],b1[i-1][j+(1<<i-1)]);
			b2[i][j] = min(b2[i-1][j],b2[i-1][j+(1<<i-1)]);
		}
	}
	while(Q --){
		la = read(),ra = read(),lb = read(),rb = read();
		ka = log2(ra-la+1),kb = log2(rb-lb+1);
		pa = ra-(1<<ka)+1,pb = rb-(1<<kb)+1;
		ansA1 = max(a1[ka][la],a2[ka][pa]),ansA2 = min(a2[ka][la],a2[ka][pa]),ansA3 = max(a3[ka][la],a3[ka][pa]),ansA4 = min(a4[ka][la],a4[ka][pa]);
		ansB1 = max(b1[kb][lb],b1[kb][pb]),ansB2 = min(b2[kb][lb],b2[kb][pb]);
		int ans = 
		-inf;
		ans = max(ans,ansA1 * (ansA1 >= 0 ? ansB2 : ansB1));
		ans = max(ans,ansA2 * (ansA2 >= 0 ? ansB2 : ansB1));
		if(ansA3 != -inf) ans = max(ans,ansA3 * (ansA3 >= 0 ? ansB2 : ansB1));
		if(ansA4 != inf) ans = max(ans,ansA4 * (ansA4 >= 0 ? ansB2 : ansB1));
		printf("%lld\n",ans);
	}
	return 0;
} 

这是提交记录

2025/1/18 10:12
加载中...