Submission #447085

#TimeUsernameProblemLanguageResultExecution timeMemory
447085YahliFancy Fence (CEOI20_fancyfence)C++14
100 / 100
115 ms7316 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std; 
using pi = pair<int, int>; 

const int mod = 1e9+7; 
const int inv4 = 25e7+2; 

int calc(int a, int b){
    a %= mod; b %= mod; 
    return ((a*(a+1)%mod)*(b*(b+1)%mod)%mod)*inv4%mod;
}

int32_t main(){
    int n; cin >> n;
    vector<pi> data(n); 
    for (int i = 0; i < n; ++i) cin >> data[i].first; 
    for (int i = 0; i < n; ++i) cin >> data[i].second; 

    //the stack trick
    stack<int> st;
    vector<int> left(n), right(n), ind; 

    for (int i = 0; i < n; ++i){
        while (!st.empty() && data[st.top()].first > data[i].first) st.pop(); 
        if (st.empty() || data[st.top()].first != data[i].first) ind.push_back(i); 
        left[i] = (st.empty() ? -1 : st.top()); 
        st.push(i); 
    }
    st = stack<int>(); 
    for (int i = n-1; i >= 0; --i){
        while (!st.empty() && data[st.top()].first >= data[i].first) st.pop(); 
        right[i] = (st.empty() ? n : st.top());  
        st.push(i); 
    }

    //prefix sums (width)
    vector<int> prefix(n+1); 
    prefix[0] = 0; 
    for (int i = 1; i <= n; ++i) prefix[i] = prefix[i-1] + data[i-1].second % mod; 

    //calculate the rectangles
    int res = 0; 
    
    for (int i : ind){
        int first = left[i] + 1, last = right[i] - 1; 
        int w = (prefix[last+1] - prefix[first] + mod) % mod, f = 0; 

        if (first != 0) f = max(f, data[first-1].first); 
        if (last != n-1) f = max(f, data[last+1].first); 

        res = (calc(data[i].first, w) - calc(f, w) + mod + res) % mod; 
    }

    cout << res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...