Submission #1300449

#TimeUsernameProblemLanguageResultExecution timeMemory
1300449hiepsimauhongFancy Fence (CEOI20_fancyfence)C++20
100 / 100
21 ms5164 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long

#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)

#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 1, L){FOR(KO, 1, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';

#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FILE "FILE"

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inv = (mod + 1) / 2;
const int oo = 1e18;

struct Modint{
        int x;

        Modint(){x = 0;}
        Modint(int _x){ x = (_x % mod + mod) % mod; }

        inline Modint operator + (const Modint &other) const{ return (x + other.x) % mod; }
        inline Modint operator - (const Modint &other) const{ return (x - other.x + mod) % mod; }
        inline Modint operator * (const Modint &other) const{ return (1LL * x * other.x) % mod; }

        inline void operator += (const Modint &other){ *this = *this + other; }
        inline void operator -= (const Modint &other){ *this = *this - other; }
        inline void operator *= (const Modint &other){ *this = *this * other; }

        friend ostream& operator << (ostream& os, const Modint &t){ return os << t.x; }
};

int n;
int h[N], w[N], pre[N];
int L[N], R[N];
stack<int> st;

Modint f(Modint x){
        return x * (x + 1) * inv;
}

signed main(){ quickly
        if(fopen(FILE".inp", "r")){
                freopen(FILE".inp", "r", stdin);
                freopen(FILE".out", "w", stdout);
        }

        cin >> n;

        FOR(i, 1, n){
                cin >> h[i];
        }

        FOR(i, 1, n){
                cin >> w[i];
                pre[i] = pre[i - 1] + w[i];
        }

        FOR(i, 1, n){
                while(!st.empty() && h[st.top()] >= h[i]){
                        st.pop();
                }
                if(!st.empty()) L[i] = st.top();
                st.push(i);
        }

        st = stack<int>();

        FOD(i, n, 1){
                while(!st.empty() && h[st.top()] > h[i]){
                        st.pop();
                }
                R[i] = n + 1;
                if(!st.empty()) R[i] = st.top();
                st.push(i);
        }

        Modint ans = 0;
        FOR(i, 1, n){
                int x = h[i];
                int y = w[i];

                int l = L[i] + 1, r = R[i] - 1;
                int k = pre[r] - pre[l - 1];

                int u = pre[i - 1] - pre[l - 1];
                int v = pre[r] - pre[i];

                ans += f(x) * u * v + f(x) * f(y) + f(x) * y * (u + v);

        }

        cout << ans;
}

Compilation message (stderr)

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:56:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |                 freopen(FILE".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:57:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |                 freopen(FILE".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...