#include<bits/stdc++.h>
using namespace std;
void local() {
#define taskname ""
if (fopen(taskname".inp", "r")) {
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
}
#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
template<class X, class Y> bool mini(X &a, const Y &b) {return (a > b) ? a = b, true : false;}
template<class X, class Y> bool maxi(X &a, const Y &b) {return (a < b) ? a = b, true : false;}
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
void add(int& x, int y) {
if((x += y) >= mod) x -= mod;
}
void sub(int& x, int y) {
if((x -= y) < 0) x += mod;
}
int hieu(int x, int y) {
sub(x, y); return x;
}
int n, h[N], w[N], dp[N], ri[N], pf[N];
int inv2 = 5e8 + 4;
int cal(int x) {
return 1ll * x * (x + 1) % mod * inv2 % mod;
}
int main() {
fastio; local();
cin >> n;
for(int i = 1; i <= n; i++) cin >> h[i];
for(int i = 1 ;i <= n; i++) cin >> w[i];
for(int i = 1; i <= n; i++) pf[i] = pf[i - 1], add(pf[i], w[i]);
stack<int> st;
for(int i = n; i > 0; i--) {
while(!st.empty() && h[st.top()] >= h[i]) st.pop();
ri[i] = st.empty() ? n + 1 : st.top();
st.emplace(i);
}
int res = 0;
for(int i = n; i > 0; i--) {
add(res, 1ll * cal(w[i]) * cal(h[i]) % mod);
add(res, 1ll * hieu(pf[ri[i] - 1], pf[i]) % mod * w[i] % mod * cal(h[i]) % mod);
add(res, 1ll * dp[ri[i]] * w[i] % mod);
add(dp[i], dp[ri[i]]);
add(dp[i], 1ll * hieu(pf[ri[i] - 1], pf[i - 1]) % mod * cal(h[i]) % mod);
}
cout << res;
}
Compilation message
fancyfence.cpp: In function 'void local()':
fancyfence.cpp:7:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:8:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
9 ms |
1872 KB |
Output is correct |
4 |
Correct |
19 ms |
3408 KB |
Output is correct |
5 |
Correct |
18 ms |
3408 KB |
Output is correct |
6 |
Correct |
19 ms |
3712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
720 KB |
Output is correct |
3 |
Correct |
11 ms |
2392 KB |
Output is correct |
4 |
Correct |
29 ms |
4176 KB |
Output is correct |
5 |
Correct |
23 ms |
4512 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
3 ms |
700 KB |
Output is correct |
4 |
Correct |
11 ms |
2316 KB |
Output is correct |
5 |
Correct |
25 ms |
4268 KB |
Output is correct |
6 |
Correct |
20 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
3 ms |
848 KB |
Output is correct |
9 |
Correct |
13 ms |
2268 KB |
Output is correct |
10 |
Correct |
20 ms |
4188 KB |
Output is correct |
11 |
Correct |
25 ms |
4176 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
696 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
8 ms |
1916 KB |
Output is correct |
12 |
Correct |
16 ms |
3404 KB |
Output is correct |
13 |
Correct |
19 ms |
3564 KB |
Output is correct |
14 |
Correct |
20 ms |
3412 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
13 ms |
2284 KB |
Output is correct |
18 |
Correct |
20 ms |
4180 KB |
Output is correct |
19 |
Correct |
25 ms |
4428 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
852 KB |
Output is correct |
22 |
Correct |
13 ms |
2432 KB |
Output is correct |
23 |
Correct |
25 ms |
4228 KB |
Output is correct |
24 |
Correct |
23 ms |
4180 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
864 KB |
Output is correct |
32 |
Correct |
10 ms |
2144 KB |
Output is correct |
33 |
Correct |
11 ms |
2404 KB |
Output is correct |
34 |
Correct |
20 ms |
4192 KB |
Output is correct |
35 |
Correct |
22 ms |
4088 KB |
Output is correct |
36 |
Correct |
22 ms |
4348 KB |
Output is correct |
37 |
Correct |
27 ms |
4180 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
26 ms |
4368 KB |
Output is correct |
40 |
Correct |
32 ms |
4180 KB |
Output is correct |
41 |
Correct |
22 ms |
4240 KB |
Output is correct |
42 |
Correct |
21 ms |
4580 KB |
Output is correct |