#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 = 5000 + 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;
}
컴파일 시 표준 에러 (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 time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |