답안 #447083

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447083 2021-07-24T12:41:46 Z Yahli Fancy Fence (CEOI20_fancyfence) C++14
0 / 100
2 ms 332 KB
#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){
    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; 

    //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, 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Incorrect 0 ms 204 KB Output isn't correct
7 Halted 0 ms 0 KB -