/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200002;
const int L_MAX = 1000002;
int n;
int a[N_MAX], b[N_MAX];
vector <pair <int, int> > vp;
bool active[N_MAX];
int root[N_MAX];
deque <int> stL[N_MAX], stR[N_MAX];
int lSide[N_MAX], rSide[N_MAX];
int L[N_MAX], R[N_MAX];
int findRoot (int u)
{
if(root[u] == u)
return u;
return root[u] = findRoot(root[u]);
}
vector <int> recalc;
ll maxArea;
ll area (int i)
{
return 1LL * (L[i] + R[i] + 1) * a[i];
}
void join (int u, int v)
{
u = findRoot(u);
v = findRoot(v);
recalc.push_back(stL[v].front());
while(stL[u].empty() == false && a[stL[u].back()] >= a[stL[v].front()])
stL[u].pop_back();
if(stL[u].empty() == true)
L[stL[v].front()] = stL[v].front() - lSide[u];
else
L[stL[v].front()] = stL[v].front() - stL[u].back() - 1;
recalc.push_back(stR[u].front());
while(stR[v].empty() == false && a[stR[v].back()] >= a[stR[u].front()])
stR[v].pop_back();
if(stR[v].empty() == true)
R[stR[u].front()] = rSide[v] - stR[u].front();
else
R[stR[u].front()] = stR[v].back() - stR[u].front() - 1;
if((int)stL[u].size() < (int)stL[v].size())
{
while(stL[u].empty() == false)
{
stL[v].push_front(stL[u].back());
stL[u].pop_back();
}
}
else
{
while(stL[v].empty() == false)
{
stL[u].push_back(stL[v].front());
stL[v].pop_front();
}
swap(stL[u], stL[v]);
}
if((int)stR[u].size() < (int)stR[v].size())
{
while(stR[u].empty() == false)
{
stR[v].push_back(stR[u].front());
stR[u].pop_front();
}
}
else
{
while(stR[v].empty() == false)
{
stR[u].push_front(stR[v].back());
stR[v].pop_back();
}
swap(stR[u], stR[v]);
}
root[u] = v;
lSide[v] = lSide[u];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i] >> b[i];
for(int i = 1; i <= n; i++)
vp.push_back(make_pair(b[i], i));
sort(vp.begin(), vp.end());
reverse(vp.begin(), vp.end());
for(int i = 1; i <= n; i++)
{
root[i] = i;
active[i] = false;
stL[i].push_back(i);
stR[i].push_back(i);
L[i] = R[i] = 0;
lSide[i] = rSide[i] = i;
}
ll ans = 0;
for(pair <int, int> p : vp)
{
int i = p.second;
active[i] = true;
if(i > 1 && active[i - 1] == true)
join(i - 1, i);
if(i < n && active[i + 1] == true)
join(i, i + 1);
recalc.push_back(i);
for(int r : recalc)
maxArea = max(maxArea, area(r));
int k = findRoot(i);
recalc.clear();
ans = max(ans, maxArea * b[i]);
}
cout << ans << "\n";
return 0;
}
Compilation message
histogram.cpp: In function 'int main()':
histogram.cpp:139:13: warning: unused variable 'k' [-Wunused-variable]
139 | int k = findRoot(i);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
269764 KB |
Output is correct |
2 |
Correct |
159 ms |
269704 KB |
Output is correct |
3 |
Correct |
204 ms |
269660 KB |
Output is correct |
4 |
Incorrect |
191 ms |
269716 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
269764 KB |
Output is correct |
2 |
Correct |
159 ms |
269704 KB |
Output is correct |
3 |
Correct |
204 ms |
269660 KB |
Output is correct |
4 |
Incorrect |
191 ms |
269716 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |