#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const ll MOD = 1e9 + 7;
const int mn = 1e5 + 5;
int add (int a, int b) {
return a + b - (a + b < MOD ? 0 : MOD);
}
int sub (int a, int b) {
return a - b + (a - b >= 0 ? 0 : MOD);
}
int mul (int a, int b) {
return 1LL * a * b % MOD;
}
int binpow (int a, int b = MOD - 2) {
int ans = 1, lg = 31 - __builtin_clz(b);
for (int i = 0; i <= lg; i++) {
if (b & (1 << i)) ans = mul(ans, a);
a = mul(a, a);
}
return ans;
}
const int half = binpow(2);
int calc (int n) {
return mul(mul(n, add(n, 1)), half);
}
int calcAll (int h, int w) {
return mul(calc(h), calc(w));
}
int calcRight (int h, int w) {
return mul(calc(h), w);
}
int dp[mn], dpRight[mn], h[mn], w[mn], pre[mn];
stack<pii> st;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) {
cin >> w[i];
pre[i] = add(pre[i - 1], w[i]);
}
st.emplace(0, 0);
int ans = 0;
for (int i = 1; i <= n; i++) {
while (st.top().first > h[i]) st.pop();
int delWidth = sub(pre[i - 1], pre[st.top().second]);
int curRight = add(dpRight[st.top().second], calcRight(h[i], delWidth));
st.emplace(h[i], i);
dpRight[i] = add(curRight, calcRight(h[i], w[i]));
ans = add(ans, add(mul(curRight, w[i]), calcAll(h[i], w[i])));
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
1884 KB |
Output is correct |
4 |
Correct |
13 ms |
3676 KB |
Output is correct |
5 |
Correct |
14 ms |
3416 KB |
Output is correct |
6 |
Correct |
14 ms |
3184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
856 KB |
Output is correct |
3 |
Correct |
9 ms |
2532 KB |
Output is correct |
4 |
Correct |
18 ms |
4700 KB |
Output is correct |
5 |
Correct |
19 ms |
4768 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
9 ms |
2532 KB |
Output is correct |
5 |
Correct |
20 ms |
4692 KB |
Output is correct |
6 |
Correct |
17 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
2652 KB |
Output is correct |
10 |
Correct |
16 ms |
4572 KB |
Output is correct |
11 |
Correct |
19 ms |
4700 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
496 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
1884 KB |
Output is correct |
12 |
Correct |
14 ms |
3708 KB |
Output is correct |
13 |
Correct |
14 ms |
3420 KB |
Output is correct |
14 |
Correct |
13 ms |
3156 KB |
Output is correct |
15 |
Correct |
1 ms |
480 KB |
Output is correct |
16 |
Correct |
2 ms |
856 KB |
Output is correct |
17 |
Correct |
9 ms |
2448 KB |
Output is correct |
18 |
Correct |
16 ms |
4552 KB |
Output is correct |
19 |
Correct |
21 ms |
4680 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
2536 KB |
Output is correct |
23 |
Correct |
16 ms |
4632 KB |
Output is correct |
24 |
Correct |
18 ms |
4696 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
588 KB |
Output is correct |
31 |
Correct |
2 ms |
716 KB |
Output is correct |
32 |
Correct |
9 ms |
2132 KB |
Output is correct |
33 |
Correct |
8 ms |
2140 KB |
Output is correct |
34 |
Correct |
16 ms |
3548 KB |
Output is correct |
35 |
Correct |
18 ms |
3676 KB |
Output is correct |
36 |
Correct |
17 ms |
3932 KB |
Output is correct |
37 |
Correct |
17 ms |
3932 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
17 ms |
3740 KB |
Output is correct |
40 |
Correct |
18 ms |
3932 KB |
Output is correct |
41 |
Correct |
17 ms |
3920 KB |
Output is correct |
42 |
Correct |
20 ms |
4176 KB |
Output is correct |