#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast,unroll-loops")
#define ll long long
#define all(x) x.begin(), x.end()
#define ld long double
#define pii pair<int, int>
#define int long long
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int INF = 1e9 + 7;
const int MAXN = 1e5 + 5;
ll bin_pow(int a, int b){
if(!b) return 1;
if(b & 1) return (a * bin_pow(a, b - 1)) % INF;
else return bin_pow((a * a) % INF, b / 2);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// ifstream cin("input.txt");
// ofstream cout("output.txt");
int n;
cin >> n;
vector<int> h(n + 1);
vector<int> w(n + 1);
bool flg = 1;
for(int i = 1; i <= n; i++){
cin >> h[i];
if(i != 1 && h[i] != h[i - 1]) flg = 0;
}
for(int i = 1; i <= n; i++){
cin >> w[i];
}
vector<ll> dp(n + 1, 0);
stack<pair<int, int> > st;
st.push({0, 0});
vector<ll> pref(n + 1);
for(int i = 1; i <= n; i++){
pref[i] += pref[i - 1];
pref[i] += w[i];
pref[i] %= INF;
}
vector<int> ans(n + 1, 0);
for(int i = 1; i <= n; i++){
while(st.top().first >= h[i]){
st.pop();
}
ll sumw = pref[i] - pref[st.top().second];
ll x = 0;
x = h[i];
x *= h[i] + 1;
x %= INF;
x *= bin_pow(2, INF - 2);
x %= INF;
x *= sumw;
x %= INF;
dp[i] += dp[st.top().second];
dp[i] %= INF;
dp[i] += x;
dp[i] %= INF;
ans[i] += ans[i - 1];
ans[i] += dp[i];
ans[i] += dp[st.top().second] * (w[i] - 1);
ans[i] %= INF;
x = 1;
x *= h[i];
x %= INF;
x *= h[i] + 1;
x %= INF;
x *= bin_pow(2, INF - 2);
x %= INF;
x *= sumw - w[i];
x *= w[i] - 1;
x %= INF;
ans[i] += x;
ans[i] %= INF;
x = 1;
x *= h[i] + 1;
x %= INF;
x *= h[i];
x %= INF;
x *= bin_pow(2, INF - 2);
x %= INF;
x *= w[i] - 1;
x %= INF;
x *= w[i];
x %= INF;
x *= bin_pow(2, INF - 2);
x %= INF;
ans[i] += x;
ans[i] %= INF;
st.push({h[i], i});
}
// cout << "goida" << endl;
// for(int i = 1; i <= n; i++){
// cout << dp[i] << " ";
// }
cout << ans[n] << endl;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:33:10: warning: variable 'flg' set but not used [-Wunused-but-set-variable]
33 | bool flg = 1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
17 ms |
2908 KB |
Output is correct |
4 |
Correct |
34 ms |
5468 KB |
Output is correct |
5 |
Correct |
34 ms |
5468 KB |
Output is correct |
6 |
Correct |
33 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |