#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
#define debug(x) cerr << "[" << #x << "] = [" << x << "]\n"
template<class T> ostream& operator<< (ostream& out, vector<T>& v) {
out << '[';
for (int i = 0; i < v.size(); ++i) {
if (i > 0) {
out << ", ";
}
out << v[i];
}
return out << ']';
}
struct Line {
ll m, b;
ll eval(ll x) {
return m * x + b;
}
};
double isect(Line a, Line b) {
return (double)(a.b - b.b) / (b.m - a.m);
}
int n, a[200000], b[200000];
ll ans = 0;
void cmp(vector<int>& la, vector<int>& lb, vector<int>& ra, vector<int>& rb) {
for (int i = 0, j = 0; i < la.size(); ++i) {
while(j < ra.size() && ra[j] >= la[i] && rb[j] >= lb[i]) ++j;
ans = max(ans, (ll)(i + j + 1) * la[i] * lb[i]);
}
/*debug(la);
debug(lb);
debug(ra);
debug(rb);*/
deque<Line> dq;
for (int i = 0, j = 0; i < la.size(); ++i) {
while(j < ra.size() && ra[j] >= la[i]) {
Line cur = {rb[j], (ll)rb[j] * (j + 2)};
while(dq.size() > 1) {
if (cur.m == dq[0].m) {
if (cur.b <= dq[0].b) break;
dq.pop_front();
}
else {
if (isect(cur, dq[1]) >= isect(dq[0], dq[1])) dq.pop_front();
else break;
}
}
dq.push_front(cur);
while(!dq.empty() && dq.back().m > lb[i])
dq.pop_back();
++j;
}
while(!dq.empty() && dq.back().m > lb[i])
dq.pop_back();
if (dq.empty()) continue;
int l = 0, r = dq.size() - 1;
while(l < r) {
int mid = (l + r + 1) / 2;
double x = mid == 0 ? -1e9 : isect(dq[mid - 1], dq[mid]);
if (x <= i) l = mid;
else r = mid - 1;
}
//cerr << la[i] << " " << dq[l].eval(i) << "\n";
ans = max(ans, la[i] * dq[l].eval(i));
}
}
void solve(int l, int r) {
if (l == r) {
ans = max(ans, (ll)a[l] * b[l]);
return;
}
int mid = (l + r) / 2;
vector<int> la(1, a[mid]), lb(1, b[mid]), ra(1, a[mid + 1]), rb(1, b[mid + 1]);
for (int i = mid - 1; i >= l; --i) {
la.push_back(min(la.back(), a[i]));
lb.push_back(min(lb.back(), b[i]));
}
for (int i = mid + 2; i <= r; ++i) {
ra.push_back(min(ra.back(), a[i]));
rb.push_back(min(rb.back(), b[i]));
}
//cerr << "RANGE OF [" << l << ", " << r << "]\n";
cmp(la, lb, ra, rb);
//cerr << "--SWAPPED--\n";
cmp(ra, rb, la, lb);
solve(l, mid);
solve(mid + 1, r);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
solve(0, n - 1);
cout << ans;
return 0;
}
Compilation message
histogram.cpp: In function 'void cmp(std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
histogram.cpp:35:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0, j = 0; i < la.size(); ++i) {
| ~~^~~~~~~~~~~
histogram.cpp:36:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while(j < ra.size() && ra[j] >= la[i] && rb[j] >= lb[i]) ++j;
| ~~^~~~~~~~~~~
histogram.cpp:44:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0, j = 0; i < la.size(); ++i) {
| ~~^~~~~~~~~~~
histogram.cpp:45:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while(j < ra.size() && ra[j] >= la[i]) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
189 ms |
5184 KB |
Output is correct |
14 |
Correct |
161 ms |
5212 KB |
Output is correct |
15 |
Correct |
198 ms |
5256 KB |
Output is correct |
16 |
Correct |
231 ms |
5472 KB |
Output is correct |
17 |
Correct |
260 ms |
5436 KB |
Output is correct |
18 |
Correct |
271 ms |
5308 KB |
Output is correct |
19 |
Correct |
269 ms |
5628 KB |
Output is correct |
20 |
Correct |
245 ms |
5320 KB |
Output is correct |
21 |
Correct |
206 ms |
5428 KB |
Output is correct |
22 |
Correct |
244 ms |
5548 KB |
Output is correct |
23 |
Correct |
20 ms |
844 KB |
Output is correct |
24 |
Correct |
204 ms |
5216 KB |
Output is correct |