Submission #321777

# Submission time Handle Problem Language Result Execution time Memory
321777 2020-11-13T11:01:33 Z alextodoran 3D Histogram (COCI20_histogram) C++17
0 / 110
240 ms 269784 KB
/**
 ____ ____ ____ ____ ____
||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 simL (int ls, deque <int> dq1, deque <int> &dq2)
{
    for(int i : dq2)
    {
        while(dq1.empty() == false && a[dq1.back()] >= a[i])
            dq1.pop_back();
        if(dq1.empty() == true)
            L[i] = i - ls;
        else
            L[i] = i - dq1.back() - 1;
        dq1.push_back(i);
        recalc.push_back(i);
    }
}
void simR (int rs, deque <int> dq1, deque <int> &dq2)
{
    for(int i : dq2)
    {
        while(dq1.empty() == false && a[dq1.back()] >= a[i])
            dq1.pop_back();
        if(dq1.empty() == true)
            R[i] = rs - i;
        else
            R[i] = dq1.back() - i - 1;
        dq1.push_back(i);
        recalc.push_back(i);
    }
}

void join (int u, int v)
{
    u = findRoot(u);
    v = findRoot(v);
    simL(lSide[u], stL[u], stR[v]);
    simL(lSide[u], stL[u], stL[v]);
    simR(rSide[v], stR[v], stR[u]);
    simR(rSide[v], stR[v], stL[u]);
    while(stL[u].empty() == false && a[stL[u].back()] >= a[stL[v].front()])
        stL[u].pop_back();
    while(stR[v].empty() == false && a[stR[v].back()] >= a[stR[u].front()])
        stR[v].pop_back();
    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);
        for(int r = 1; r <= n; r++)
            if(active[r] == true)
                maxArea = max(maxArea, area(r));
        recalc.clear();
        ans = max(ans, maxArea * b[i]);
    }
    cout << ans << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 240 ms 269784 KB Output is correct
2 Correct 213 ms 269696 KB Output is correct
3 Correct 187 ms 269636 KB Output is correct
4 Incorrect 160 ms 269676 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 240 ms 269784 KB Output is correct
2 Correct 213 ms 269696 KB Output is correct
3 Correct 187 ms 269636 KB Output is correct
4 Incorrect 160 ms 269676 KB Output isn't correct
5 Halted 0 ms 0 KB -