1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 100005;
int n, m, q;
LL a[MAXN], b[MAXN];
// zheng fu ling
int azs[MAXN], afs[MAXN], als[MAXN];
int bzs[MAXN], bfs[MAXN], bls[MAXN];
bool Ahz(int l, int r) { return azs[r]>azs[l-1]; } // A have zheng?
bool Ahf(int l, int r) { return afs[r]>afs[l-1]; } // A have fu?
bool Ahl(int l, int r) { return als[r]>als[l-1]; } // A have ling?
bool Bhz(int l, int r) { return bzs[r]>bzs[l-1]; }
bool Bhf(int l, int r) { return bfs[r]>bfs[l-1]; }
bool Bhl(int l, int r) { return bls[r]>bls[l-1]; }
struct SegmentTree {
LL maxx[4*MAXN], minn[4*MAXN];
SegmentTree() {
for (int i = 0; i < 4*MAXN; i++) {
maxx[i] = -1e18;
minn[i] = 1e18;
}
}
void add(int u, int l, int r, int pos, LL v) {
if (pos < l || pos > r) return ;
if (l == r && l == pos) {
maxx[u] = max(maxx[u], v);
minn[u] = min(minn[u], v);
return;
}
int mid = (l+r)>>1;
add(u<<1, l, mid, pos, v);
add(u<<1|1, mid+1, r, pos, v);
maxx[u] = max(maxx[u<<1], maxx[u<<1|1]);
minn[u] = min(minn[u<<1], minn[u<<1|1]);
}
LL queryMax(int u, int l, int r, int x, int y) {
if (l > y || r < x) return -1e18;
if (l >= x && r <= y) return maxx[u];
int mid = (l+r)>>1;
return max(queryMax(u<<1, l, mid, x, y), queryMax(u<<1|1, mid+1, r, x, y));
}
LL queryMin(int u, int l, int r, int x, int y) {
if (l > y || r < x) return 1e18;
if (l >= x && r <= y) return minn[u];
int mid = (l+r)>>1;
return min(queryMin(u<<1, l, mid, x, y), queryMin(u<<1|1, mid+1, r, x, y));
}
} az, af, bz, bf;
int main()
{
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] > 0) {
az.add(1, 1, n, i, a[i]);
} else if (a[i] < 0) {
af.add(1, 1, n, i, a[i]);
}
azs[i] = azs[i-1]+(a[i]>0);
afs[i] = afs[i-1]+(a[i]<0);
als[i] = als[i-1]+(a[i]==0);
}
for (int i = 1; i <= m; i++) {
scanf("%lld", &b[i]);
if (b[i] > 0) {
bz.add(1, 1, m, i, b[i]);
} else if (b[i] < 0) {
bf.add(1, 1, m, i, b[i]);
}
bzs[i] = bzs[i-1]+(b[i]>0);
bfs[i] = bfs[i-1]+(b[i]<0);
bls[i] = bls[i-1]+(b[i]==0);
}
while (q--) {
int l, r, x, y;
scanf("%d%d%d%d", &l, &r, &x, &y);
if (Bhz(x, y) && Bhl(x, y) && Bhf(x, y)) {
if (Ahl(l, r)) {
printf("0\n");
} else {
LL ans = -1e18;
// A最小正数*B最小负数
if (Ahz(l, r))
ans = az.queryMin(1, 1, n, l, r)*bf.queryMin(1, 1, m, x, y);
// A最大负数*B最大正数
if (Ahf(l, r))
ans = max(ans, af.queryMax(1, 1, n, l, r)*bz.queryMax(1, 1, m, x, y));
printf("%lld\n", ans);
}
} else if (Bhz(x, y) && Bhl(x, y)) {
if (Ahz(l, r) || Ahl(l, r)) {
printf("0\n");
} else {
// A最大负数*B最大正数
printf("%lld\n", af.queryMax(1, 1, n, l, r)*bz.queryMax(1, 1, m, x, y));
}
} else if (Bhz(x, y) && Bhf(x, y)) {
if (Ahl(l, r)) {
printf("0\n");
} else{
LL ans = -1e18;
if (Ahz(l, r))
ans = az.queryMin(1, 1, n, l, r)*bf.queryMin(1, 1, m, x, y);
if (Ahf(l, r))
ans = max(ans, af.queryMax(1, 1, n, l, r)*bz.queryMax(1, 1, m, x, y));
printf("%lld\n", ans);
}
} else if (Bhl(x, y) && Bhf(x, y)) {
if (Ahf(l, r) || Ahl(l, r)) {
printf("0\n");
} else {
// A 最小正数*B最小负数
printf("%lld\n", az.queryMin(1, 1, n, l, r)*bf.queryMin(1, 1, m, x, y));
}
} else if (Bhz(x, y)) {
if (Ahz(l, r)) {
// A 最大正数*B最小正数
printf("%lld\n", az.queryMax(1, 1, n, l, r)*bz.queryMin(1, 1, m, x, y));
} else if (Ahl(l, r)) {
printf("0\n");
} else {
// A 最大负数*B最大正数
printf("%lld\n", af.queryMax(1, 1, n, l, r)*bz.queryMax(1, 1, m, x, y));
}
} else if (Bhf(x, y)) {
if (Ahf(l, r)) {
// A 最小负数*B最大负数
printf("%lld\n", af.queryMin(1, 1, n, l, r)*bf.queryMax(1, 1, m, x, y));
} else if (Ahl(l, r)) {
printf("0\n");
} else {
// A 最小正数*B最小负数
printf("%lld\n", az.queryMin(1, 1, n, l, r)*bf.queryMin(1, 1, m, x, y));
}
} else if (Bhl(x, y)) {
printf("0\n");
}
}
return 0;
}
|