#include <stdio.h>
#define N 200000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long ans;
void solve_batch(int *aa, int *bb, int *ll, int *rr, int *jj, int cnt) {
int h, i, j;
for (h = 0; h < cnt; h++) {
j = jj[h];
for (i = ll[h]; i < rr[h]; i++)
ans = max(ans, (long long) aa[i] * bb[j] * (j - i + 1));
}
}
void solve(int *aa, int *bb, int l, int r) {
static int qqa[N], qqb[N], ppa[N], ppb[N];
int m = (l + r) / 2, i, j;
if (r - l == 1) {
ans = max(ans, (long long) aa[l] * bb[l]);
return;
}
solve(aa, bb, l, m), solve(aa, bb, m, r);
for (i = m - 1; i >= l; i--)
qqa[i] = min(i + 1 == m ? INF : qqa[i + 1], aa[i]);
for (i = m - 1; i >= l; i--)
qqb[i] = min(i + 1 == m ? INF : qqb[i + 1], bb[i]);
for (j = m; j < r; j++)
ppa[j] = min(j == m ? INF : ppa[j - 1], aa[j]);
for (j = m; j < r; j++)
ppb[j] = min(j == m ? INF : ppb[j - 1], bb[j]);
#if 0
for (i = m - 1, j = r - 1; i >= l; i--) {
while (j >= m && (qqa[i] > ppa[j] || qqb[i] > ppb[j]))
j--;
if (j >= l)
ans = max(ans, (long long) qqa[i] * qqb[i] * (j - i + 1));
}
for (i = l, j = m; j < r; j++) {
while (i < m && ppa[j] > qqa[i])
i++;
iia[j] = i;
}
for (i = l, j = m; j < r; j++) {
while (i < m && ppb[j] > qqb[i])
i++;
iib[j] = i;
}
for (j = m; j < r; j++) {
i = max(iia[j], iib[j]);
if (i < m)
ans = max(ans, (long long) ppa[j] * ppb[j] * (j - i + 1));
}
cnt = 0;
for (j = m; j < r; j++)
if (iia[j] < iib[j]) {
if (cnt > 0 && iib[jj[cnt - 1]] < iia[j])
solve_batch(qqb, ppa, iia, iib, jj, cnt), cnt = 0;
jj[cnt++] = j;
}
if (cnt > 0)
solve_batch(qqb, ppa, iia, iib, jj, cnt), cnt = 0;
cnt = 0;
for (j = m; j < r; j++)
if (iib[j] < iia[j]) {
if (cnt > 0 && iia[jj[cnt - 1]] < iib[j])
solve_batch(qqa, ppb, iib, iia, jj, cnt), cnt = 0;
jj[cnt++] = j;
}
if (cnt > 0)
solve_batch(qqa, ppb, iib, iia, jj, cnt), cnt = 0;
#else
for (i = l; i < m; i++)
for (j = m; j < r; j++)
ans = max(ans, (long long) min(qqa[i], ppa[j]) * min(qqb[i], ppb[j]) * (j - i + 1));
#endif
}
int main() {
static int aa[N], bb[N];
int n, i;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d%d", &aa[i], &bb[i]);
ans = 0;
solve(aa, bb, 0, n);
printf("%lld\n", ans);
return 0;
}
Compilation message
histogram.c: In function 'main':
histogram.c:89:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
histogram.c:91:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf("%d%d", &aa[i], &bb[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
332 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
332 KB |
Output is correct |
4 |
Correct |
5 ms |
332 KB |
Output is correct |
5 |
Correct |
6 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
332 KB |
Output is correct |
7 |
Correct |
4 ms |
332 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
332 KB |
Output is correct |
10 |
Correct |
5 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
332 KB |
Output is correct |
2 |
Correct |
7 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
332 KB |
Output is correct |
4 |
Correct |
5 ms |
332 KB |
Output is correct |
5 |
Correct |
6 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
332 KB |
Output is correct |
7 |
Correct |
4 ms |
332 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
332 KB |
Output is correct |
10 |
Correct |
5 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
332 KB |
Output is correct |
13 |
Execution timed out |
2565 ms |
2612 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |