답안 #898427

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898427 2024-01-04T16:16:30 Z votranngocvy Fancy Fence (CEOI20_fancyfence) C++14
0 / 100
1 ms 2396 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
int h[N],w[N],pre[N],l[N],r[N];

void add(int &a,int b) {
    a += b;
    while (a >= mod) a -= mod;
}

void sub(int &a,int b) {
    a -= b;
    while (a < 0) a += mod;
}

int sum(int l,int r) {
    return (pre[r] - pre[l - 1] + mod) % mod;
}

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

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> h[i];
    pre[0] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> w[i];
        pre[i] = (pre[i - 1] + w[i]) % mod;
    }
    stack<int>st;
    st.push(0);
    for (int i = 1; i <= n; i++) {
        while (!st.empty() && h[st.top()] > h[i]) st.pop();
        l[i] = st.top() + 1;
        st.push(i);
    }
    while (!st.empty()) st.pop();
    st.push(n + 1);
    for (int i = n; i > 0; i--) {
        while (!st.empty() && h[st.top()] > h[i]) st.pop();
        r[i] = st.top() - 1;
        st.push(i);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        add(ans,calc(h[i],sum(l[i],r[i])));
        sub(ans,calc(h[i],sum(l[i],i - 1)));
        sub(ans,calc(h[i],sum(i + 1,r[i])));
    }
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Incorrect 1 ms 2396 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Incorrect 1 ms 2396 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Incorrect 1 ms 2396 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Incorrect 1 ms 2396 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Incorrect 1 ms 2396 KB Output isn't correct
7 Halted 0 ms 0 KB -