分类讨论好题
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5,L = 19,INF = 2e9;
typedef long long LL;
LL a[N],b[N];
int n,m,q,l1,r1,l2,r2,f1[N][L],f2[N][L],f3[N][L],f4[N][L],f5[N][L],f6[N][L];
int qmax_a(int l,int r)
{
int t = log2(r-l+1);
return max(f1[l][t],f1[r-(1<<t)+1][t]);
}
int qmin_a(int l,int r)
{
int t = log2(r-l+1);
return min(f2[l][t],f2[r-(1<<t)+1][t]);
}
int qmax_b(int l,int r)
{
int t = log2(r-l+1);
return max(f3[l][t],f3[r-(1<<t)+1][t]);
}
int qmin_b(int l,int r)
{
int t = log2(r-l+1);
return min(f4[l][t],f4[r-(1<<t)+1][t]);
}
int qmax_fu(int l,int r)
{
int t = log2(r-l+1);
return max(f5[l][t],f5[r-(1<<t)+1][t]);
}
int qmin_zh(int l,int r)
{
int t = log2(r-l+1);
return min(f6[l][t],f6[r-(1<<t)+1][t]);
}
void init()
{
for(int j = 0;(1<<j)<=n;j++)
for(int i = 1;i+(1<<j)-1<=n;i++)
{
if(!j)
{
f1[i][j] = f2[i][j] = a[i];
if(a[i] < 0) f5[i][j] = a[i];
else f5[i][j] = -INF;
if(a[i] >= 0) f6[i][j] = a[i];
else f6[i][j] = INF;
}
else
{
f1[i][j] = max(f1[i][j-1],f1[i+(1<<j-1)][j-1]);
f2[i][j] = min(f2[i][j-1],f2[i+(1<<j-1)][j-1]);
f5[i][j] = max(f5[i][j-1],f5[i+(1<<j-1)][j-1]);
f6[i][j] = min(f6[i][j-1],f6[i+(1<<j-1)][j-1]);
}
}
for(int j = 0;(1<<j)<=m;j++)
for(int i = 1;i+(1<<j)-1<=m;i++)
{
if(!j) f3[i][j] = f4[i][j] = b[i];
else
{
f3[i][j] = max(f3[i][j-1],f3[i+(1<<j-1)][j-1]);
f4[i][j] = min(f4[i][j-1],f4[i+(1<<j-1)][j-1]);
}
}
}
int main()
{
cin>>n>>m>>q;
for(int i = 1;i<=n;i++) cin>>a[i];
for(int i = 1;i<=m;i++) cin>>b[i];
init();
while(q--)
{
cin>>l1>>r1>>l2>>r2;
LL ans = -4e18;
LL bmin = qmin_b(l2,r2),bmax = qmax_b(l2,r2),mina = qmin_a(l1,r1),maxa = qmax_a(l1,r1),zero = qmin_zh(l1,r1);
if(maxa > 0)
{
if(bmin > 0) ans = qmax_a(l1,r1)*bmin;
else ans = qmin_zh(l1,r1)*bmin;
}
if(mina < 0)
{
if(bmax > 0) ans = max(ans,qmax_fu(l1,r1)*bmax);
else ans = max(ans,qmin_a(l1,r1)*bmax);
}
if(zero == 0) ans = max(ans,0ll);
cout << ans << '\n';
}
return 0;
}