#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
const int sz = 1e5 + 5;
long long INV;
long long h[sz], w[sz];
long long s[sz], f[sz];
long long bp(long long a, long long b){
long long ans = 1;
while(b){
if(b & 1){
ans = (ans * a) % MOD;
}
a = (a % MOD) * (a % MOD) % MOD;
b >>= 1;
}
return ans;
}
long long c2(long long x){
return (x * (x+1) % MOD) * (INV % MOD) % MOD;
}
long long add(long long a, long long b){
return ((a % MOD) + (b % MOD)) % MOD;
}
long long mul(long long a, long long b){
return (a % MOD) * (b % MOD) % MOD;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("main.inp","r",stdin);
//freopen("main.out","w",stdout);
int n;
cin>>n;
for(int i=1;i<=n;++i){
cin>>h[i];
}
for(int i=1;i<=n;++i){
cin>>w[i];
s[i] = (s[i-1] + w[i]) % MOD;
}
INV = bp(2, MOD-2);
stack<int> st; //ascending stack
st.push(0);
long long ans = 0;
for(int i=1;i<=n;++i){
while(st.size() && h[st.top()] >= h[i]){ //h[st.top()] < h[i]
st.pop();
}
long long len = (s[i] - s[st.top()] + MOD) % MOD;
long long l1 = (len - w[i] + MOD) % MOD;
long long A = 0; //equal heights.
long long B = 0; //smaller heights.
//# of sub-rect in current section.
A += mul(c2(h[i]), c2(w[i]));
//# of sub-rect of section with height >= h[i] appended to current section.
A += mul(mul(w[i], l1), c2(h[i]));
A %= MOD;
B += mul(f[st.top()], w[i]);
B %= MOD;
f[i] = add(f[st.top()], mul(len, c2(h[i])));
st.push(i);
ans = (ans + A + B) % MOD;
}
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2512 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
8 ms |
3536 KB |
Output is correct |
4 |
Correct |
15 ms |
4700 KB |
Output is correct |
5 |
Correct |
16 ms |
4564 KB |
Output is correct |
6 |
Correct |
16 ms |
4692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
3 ms |
2764 KB |
Output is correct |
3 |
Correct |
10 ms |
3928 KB |
Output is correct |
4 |
Correct |
18 ms |
5468 KB |
Output is correct |
5 |
Correct |
20 ms |
5468 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
9 ms |
4068 KB |
Output is correct |
5 |
Correct |
19 ms |
5468 KB |
Output is correct |
6 |
Correct |
20 ms |
5564 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
9 ms |
4188 KB |
Output is correct |
10 |
Correct |
18 ms |
5972 KB |
Output is correct |
11 |
Correct |
18 ms |
6236 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
8 ms |
3616 KB |
Output is correct |
12 |
Correct |
16 ms |
4952 KB |
Output is correct |
13 |
Correct |
16 ms |
4700 KB |
Output is correct |
14 |
Correct |
16 ms |
4696 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
9 ms |
3932 KB |
Output is correct |
18 |
Correct |
18 ms |
5464 KB |
Output is correct |
19 |
Correct |
19 ms |
5460 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
9 ms |
4020 KB |
Output is correct |
23 |
Correct |
18 ms |
6232 KB |
Output is correct |
24 |
Correct |
27 ms |
6236 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2556 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
9 ms |
3832 KB |
Output is correct |
33 |
Correct |
10 ms |
3932 KB |
Output is correct |
34 |
Correct |
18 ms |
5204 KB |
Output is correct |
35 |
Correct |
18 ms |
5212 KB |
Output is correct |
36 |
Correct |
19 ms |
5276 KB |
Output is correct |
37 |
Correct |
19 ms |
5460 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
18 ms |
5460 KB |
Output is correct |
40 |
Correct |
18 ms |
5320 KB |
Output is correct |
41 |
Correct |
19 ms |
5544 KB |
Output is correct |
42 |
Correct |
18 ms |
5724 KB |
Output is correct |