#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define h first
#define w second
using namespace std;
typedef long long lint;
typedef pair<lint,lint> ii;
lint H[100005];
lint W[100005];
lint mod = 1000000007;
void fix(lint &x){
x %= mod;
if(x < 0) x += mod;
}
lint within(lint h, lint w){
lint A = h * (h+1) / 2;
fix(A);
lint B = w * (w + 1) / 2;
fix(B);
return (A*B)%mod;
}
lint acc = 0; ///sum of h*(h+1)*w of previous blocks
lint ans = 0;
vector<ii> S;
lint cal(lint h, lint w){
lint toadd = h*(h+1)/2;
fix(toadd);
toadd *= w;
fix(toadd);
return toadd;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int i = 0;i < n;i++) cin >> H[i];
for(int i = 0;i < n;i++) cin >> W[i];
for(int i = 0;i < n;i++){
lint h = H[i], w = W[i];
lint popWidth = 0;
while(!S.empty()){
ii T = S.back();
if(T.h > h){
popWidth += T.w;
S.pop_back();
acc -= cal(T.h, T.w);
fix(acc);
}
else break;
}
fix(popWidth);
acc += cal(h, popWidth);
fix(acc);
ans += acc * w;
ans += within(h,w);
fix(ans);
popWidth += w;
fix(popWidth);
S.push_back(ii(h, popWidth));
acc += cal(h,w);
fix(acc);
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 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 |
10 ms |
2344 KB |
Output is correct |
4 |
Correct |
19 ms |
5204 KB |
Output is correct |
5 |
Correct |
20 ms |
4312 KB |
Output is correct |
6 |
Correct |
19 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1116 KB |
Output is correct |
3 |
Correct |
13 ms |
3272 KB |
Output is correct |
4 |
Correct |
27 ms |
6104 KB |
Output is correct |
5 |
Correct |
23 ms |
6100 KB |
Output is correct |
6 |
Correct |
1 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 |
3 ms |
1116 KB |
Output is correct |
4 |
Correct |
12 ms |
3316 KB |
Output is correct |
5 |
Correct |
24 ms |
6100 KB |
Output is correct |
6 |
Correct |
29 ms |
6360 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
12 ms |
3292 KB |
Output is correct |
10 |
Correct |
22 ms |
5972 KB |
Output is correct |
11 |
Correct |
23 ms |
6104 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
476 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
464 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
10 ms |
2472 KB |
Output is correct |
12 |
Correct |
22 ms |
5388 KB |
Output is correct |
13 |
Correct |
19 ms |
4316 KB |
Output is correct |
14 |
Correct |
19 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
1048 KB |
Output is correct |
17 |
Correct |
12 ms |
3340 KB |
Output is correct |
18 |
Correct |
23 ms |
6088 KB |
Output is correct |
19 |
Correct |
25 ms |
6852 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
12 ms |
3292 KB |
Output is correct |
23 |
Correct |
27 ms |
5848 KB |
Output is correct |
24 |
Correct |
23 ms |
6092 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
3 ms |
692 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
12 ms |
2140 KB |
Output is correct |
33 |
Correct |
13 ms |
2140 KB |
Output is correct |
34 |
Correct |
24 ms |
3576 KB |
Output is correct |
35 |
Correct |
24 ms |
3676 KB |
Output is correct |
36 |
Correct |
26 ms |
3916 KB |
Output is correct |
37 |
Correct |
25 ms |
3948 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
25 ms |
3924 KB |
Output is correct |
40 |
Correct |
31 ms |
4300 KB |
Output is correct |
41 |
Correct |
25 ms |
4304 KB |
Output is correct |
42 |
Correct |
26 ms |
5076 KB |
Output is correct |