Submission #949474

# Submission time Handle Problem Language Result Execution time Memory
949474 2024-03-19T09:28:25 Z hngwlog Real Mountains (CCO23_day1problem2) C++14
0 / 25
1 ms 348 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define _size(x) (int)x.size()

const long long mod = 1e6 + 3;

int n;
long long ans = 0;
vector<long long> h, newH;

void solve() {
    
    set<pair<long long, long long>> st;
    for (int i = 0; i < n; i++) {
        if (h[i] == newH[i]) {
            auto it = st.upper_bound({h[i], newH[i]});
            if (it == st.begin()) st.insert({h[i], newH[i]});
            else {
                it--;
                if (it->se < h[i]) st.insert({h[i], newH[i]});
            }
            continue;
        }
        vector<pair<long long, long long>> g;
        auto it = st.upper_bound({h[i], h[i]});
        if (it != st.begin()) {
            it--;
            if (it->se < h[i]) it++;
        }
        long long x = h[i];
        while (true) {
            g.push_back({it->fi, it->se});
            if (it->fi > x) {
                if (it->fi > newH[i]) {
                    ans = (ans + it->fi * (newH[i] - x)) % mod;
                    g.pop_back();
                    break;
                }
                ans += it->fi * (it->fi - x);
                x = it->fi;
            }
            long long maxx = min(it->se, newH[i]);
            ans = (ans + (maxx + x + 1) * (maxx - x) / 2) % mod;
            x = maxx;
            if (x == newH[i]) break;
            it++;
        }
        for (auto seg: g) st.erase({seg.fi, seg.se});
        if (_size(g)) st.insert({min(g[0].fi, h[i]), max(g.back().se, newH[i])});
        else st.insert({h[i], newH[i]});    
    }
}

int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
    
    cin >> n;
    h.resize(n);
    long long maxValue = 0;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
        maxValue = max(maxValue, h[i]);
    }
    newH.resize(n);
    for (int i = 0; i < n; i++) {
        if (h[i] == maxValue) break;
        newH[i] = max(i ? newH[i - 1] : 0, h[i]);
    }
    for (int i = n - 1; i >= 0; i--) {
        if (h[i] == maxValue) break;
        newH[i] = max(i < n - 1 ? newH[i + 1] : 0, h[i]);
    }
    for (int i = 0; i < n; i++) if (newH[i] == 0) newH[i] = maxValue;
    // for (int i = 0; i < n; i++) cout << "#" << i << ": " << h[i] << " and " << newH[i] << '\n'; 
    solve();
    reverse(h.begin(), h.end());
    reverse(newH.begin(), newH.end());
    solve();
    for (int i = 0; i < n; i++) ans = (ans + (newH[i] - 1 + h[i]) * (newH[i] - h[i]) / 2) % mod;
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -