/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
int n, ls[N], rs[N];
ll w[N], h[N], pref[N];
ll comb2(ll x){
return (x*(x+1)/2) % MOD;
}
ll p(int l, int r){
if(l > 0)
return pref[r] - pref[l - 1];
return pref[r];
}
void solve(){
cin >> n;
for(int i = 0; i < n; ++i) cin >> w[i];
for(int i = 0; i < n; ++i) cin >> h[i];
pref[0] = w[0];
for(int i = 1; i < n; ++i) pref[i] = pref[i - 1] + w[i];
deque<pair<int, int>> q;
ls[0] = -1;
q.pb({h[0], 0});
for(int i = 1; i < n; ++i){
while(!q.empty() && q.back().first > h[i]) q.pop_back();
if(!q.empty()) ls[i] = q.back().second;
else ls[i] = -1;
q.pb({h[i], i});
}
q.clear();
rs[n - 1] = n;
q.pb({h[n - 1], n - 1});
for(int i = n - 2; i >= 0; --i){
while(!q.empty() && q.back().first > h[i]) q.pop_back();
if(!q.empty()) rs[i] = q.back().second;
else rs[i] = n;
q.pb({h[i], i});
}
ll ans = 0;
for(int i = 0; i < n; ++i){
ll a = (p(ls[i] + 1, i) * p(i, rs[i] - 1) % MOD);
(ans += (a - comb2(w[i] - 1)) * comb2(h[i])) %= MOD;
}
cout << (ans + MOD) % MOD;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:69:16: warning: unused variable 'aa' [-Wunused-variable]
69 | int T = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |