답안 #503806

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503806 2022-01-08T23:27:54 Z MasterTaster Fancy Fence (CEOI20_fancyfence) C++14
0 / 100
2 ms 332 KB
#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXN 100010
#define MOD 1000000007

using namespace std;

ll two=500000004; //inv
ll n, dp[MAXN], pos[MAXN], h[MAXN], w[MAXN], pref[MAXN], cnt[MAXN], ress;

ll add(ll x, ll y)
{
    ll ret=x+y;
    if (ret>=MOD) ret-=MOD;
    return ret;
}
ll mul(ll x, ll y)
{
    ll ret=x*y;
    if (ret>=MOD) ret%=MOD;
    return ret;
}
ll sum(ll x, ll y)
{
    ll ret=mul(mul(y, add(y, 1)), two);
    //ret-=mul(mul(x-1, x), two);
    //if (ret<0) ret+=MOD;
    return ret;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    freopen("input5.txt", "r", stdin);
    freopen("output5.txt", "w", stdout);

    cin>>n;
    for (int i=1; i<=n; i++) cin>>h[i];
    for (int i=1; i<=n; i++) cin>>w[i];


    stack<pii> st;
    st.push({-1, 0});
    for (int i=1; i<=n; i++)
    {
        while (st.top().xx>=h[i]) st.pop();
        pos[i]=st.top().yy;
        st.push({h[i], i});
    }

    pref[0]=0;
    for (int i=1; i<=n; i++) pref[i]=add(pref[i-1], w[i]);


    ll x, y;
    //cnt[1]=sum(1, h[1]);
    for (int i=1; i<=n; i++)
    {
        int ind=pos[i];
        cnt[i]=cnt[ind];

        x=pref[i]-pref[ind];
        y=sum(1, h[i]);
        cnt[i]=add(cnt[i], mul(x, y));
    }

    //dp[1]=mul(sum(1, w[1]), sum(1, h[1]));
    for (int i=1; i<=n; i++)
    {
        int ind=pos[i];
        dp[i]=mul(cnt[ind], w[i]);

        x=mul(pref[i-1]-pref[ind], w[i]);
        y=sum(1, h[i]);
        dp[i]=add(dp[i], mul(x, y));

        x=sum(1, w[i]);
        y=sum(1, h[i]);
        dp[i]=add(dp[i], mul(x, y));
    }

    for (int i=1; i<=n; i++) { ress=add(dp[i], ress); /*cout<<cnt[i]<<" "<<dp[i]<<endl;*/ }
    //cout<<endl;

    cout<<ress;
}
/*

3
1 2 1
1 2 3

*/

Compilation message

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |     freopen("input5.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:40:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     freopen("output5.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 332 KB Output isn't correct
2 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 Incorrect 2 ms 332 KB Output isn't correct
2 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 Incorrect 2 ms 332 KB Output isn't correct
2 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 Incorrect 2 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -