#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
typedef vector<int> vi;
#define iii tuple<int,int,int>
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef map<int,int> mii;
#ifndef debug
#define cerr if (0) cerr
#endif
int N,H[100005],W[100005];
int pref[100005], last_mins[100005];
const int mod=1e9+7;
int nc2(int n){
return ((n*(n-1))/2)%mod;
}
int rect(int h, int w){
return (nc2(h+1)*nc2(w+1))%mod;
}
int32_t main(){
cin>>N;
int last_min = -1;
for (int i=0; i<N; i++)cin>>H[i];
for (int i=0; i<N; i++){
cin>>W[i];
if (i==0) pref[i]=W[i];
else pref[i]=W[i]+pref[i-1];
last_mins[i]=last_min;
if (i>0 && H[i-1]>H[i]){
last_min = i;
}
}
int ans=0, sum=0;
stack<iii> st;
for (int i=0; i<N; i++){
ans += rect(H[i],W[i]);
ans %= mod;
int total_w = 0;
while (st.size() && get<0>(st.top()) >= H[i]){
auto [h,w,p] = st.top();
st.pop();
total_w += w;
total_w %= mod;
sum = (sum-p + mod);
sum %= mod;
}
ans += (sum*W[i])%mod;
ans %= mod;
ans += ((total_w * nc2(H[i]+1))%mod * W[i])%mod;
ans %= mod;
total_w += W[i];
total_w %= mod;
int t = total_w * nc2(H[i]+1);
t %= mod;
sum += t;
sum %= mod;
st.push({H[i], total_w, t});
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
21 ms |
3444 KB |
Output is correct |
4 |
Correct |
45 ms |
4504 KB |
Output is correct |
5 |
Correct |
41 ms |
4704 KB |
Output is correct |
6 |
Correct |
42 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2396 KB |
Output is correct |
2 |
Correct |
8 ms |
2652 KB |
Output is correct |
3 |
Correct |
30 ms |
3668 KB |
Output is correct |
4 |
Correct |
57 ms |
5460 KB |
Output is correct |
5 |
Correct |
60 ms |
5468 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
6 ms |
2648 KB |
Output is correct |
4 |
Correct |
33 ms |
3676 KB |
Output is correct |
5 |
Correct |
58 ms |
5332 KB |
Output is correct |
6 |
Correct |
60 ms |
5552 KB |
Output is correct |
7 |
Correct |
2 ms |
2592 KB |
Output is correct |
8 |
Correct |
6 ms |
2904 KB |
Output is correct |
9 |
Correct |
30 ms |
4312 KB |
Output is correct |
10 |
Correct |
57 ms |
7904 KB |
Output is correct |
11 |
Correct |
60 ms |
7708 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 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 |
2392 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 |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2468 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
27 ms |
3420 KB |
Output is correct |
12 |
Correct |
42 ms |
4672 KB |
Output is correct |
13 |
Correct |
42 ms |
4692 KB |
Output is correct |
14 |
Correct |
41 ms |
4688 KB |
Output is correct |
15 |
Correct |
2 ms |
2392 KB |
Output is correct |
16 |
Correct |
6 ms |
2652 KB |
Output is correct |
17 |
Correct |
29 ms |
3936 KB |
Output is correct |
18 |
Correct |
58 ms |
5452 KB |
Output is correct |
19 |
Correct |
59 ms |
5584 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
7 ms |
2904 KB |
Output is correct |
22 |
Correct |
29 ms |
4404 KB |
Output is correct |
23 |
Correct |
56 ms |
7760 KB |
Output is correct |
24 |
Correct |
67 ms |
7680 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
6 ms |
2652 KB |
Output is correct |
31 |
Correct |
6 ms |
2652 KB |
Output is correct |
32 |
Correct |
29 ms |
3940 KB |
Output is correct |
33 |
Correct |
29 ms |
3928 KB |
Output is correct |
34 |
Correct |
58 ms |
5200 KB |
Output is correct |
35 |
Correct |
54 ms |
5296 KB |
Output is correct |
36 |
Correct |
57 ms |
5456 KB |
Output is correct |
37 |
Correct |
60 ms |
5328 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
58 ms |
5464 KB |
Output is correct |
40 |
Correct |
58 ms |
5744 KB |
Output is correct |
41 |
Correct |
58 ms |
5712 KB |
Output is correct |
42 |
Correct |
58 ms |
6484 KB |
Output is correct |