#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
using namespace std;
#define MOD 1000000007
#define int long long
#define endl '\n'
int cal(int a, int b){
int f=(a*(a+1))%MOD*((MOD+1)/2)%MOD;
int s=(b*(b+1))%MOD*((MOD+1)/2)%MOD;
return (f*s)%MOD;
}
void solve(){
int n; cin>>n;
vector<int> h(n+1),w(n+1);
for(int i=1; i<=n; i++) cin>>h[i];
for(int i=1; i<=n; i++) cin>>w[i];
if(n<=1000){
int ans=0;
for(int i=1; i<=n; i++){
int cur=0,mx=h[i];
ans=(ans+cal(h[i],w[i]))%MOD;
for(int j=i-1; j>=1; j--){
mx=min(mx,h[j]);
cur=(cur+w[j])%MOD;
ans=(ans-cal(cur,mx)+MOD)%MOD;
ans=(ans+cal((cur+w[i])%MOD,mx))%MOD;
ans=(ans+cal((cur-w[j])%MOD,mx))%MOD;
ans=(ans-cal((w[i]+(cur-w[j])%MOD)%MOD,mx)+MOD)%MOD;
}
}
cout<<ans;
}else{
bool war=0;
for(int i=1; i<n; i++){
if(h[i]>h[i+1]) war=1;
}
if(war==0){
vector<int> dw(n+2);
for(int i=n; i>=1; i--) dw[i]=(dw[i+1]+w[i])%MOD;
stack<int> f;
f.push(n+1);
int ans=0;
for(int i=n; i>=1; i--){
int pos=i+1;
while(f.size()>1&&h[f.top()]>=h[i]){
f.pop();
ans=(ans-cal(dw[pos]-dw[f.top()],h[i])+MOD)%MOD;
pos=f.top();
}
ans=(ans+cal(dw[i]-dw[f.top()],h[i]))%MOD;
f.push(i);
}
cout<<ans;
}else{
int mx=0;
int ans=0;
for(int i=1; i<=n; i++) mx=max(mx,h[i]);
for(int i=1; i<=mx; i++){
int cur=0;
for(int j=1; j<=n; j++){
if(h[j]>=i) cur=(cur+w[j])%MOD;
else{
ans=(ans+cal(cur,i))%MOD;
ans=(ans-cal(cur,i-1)+MOD)%MOD;
cur=0;
}
}
ans=(ans+cal(cur,i))%MOD;
ans=(ans-cal(cur,i-1)+MOD)%MOD;
}
cout<<ans;
}
}
}
signed main(){
// freopen("hps.in", "r", stdin);
// freopen("hps.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
16 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
16 ms |
344 KB |
Output is correct |
3 |
Correct |
7 ms |
1116 KB |
Output is correct |
4 |
Correct |
14 ms |
3024 KB |
Output is correct |
5 |
Correct |
15 ms |
3416 KB |
Output is correct |
6 |
Correct |
13 ms |
3160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
612 KB |
Output is correct |
3 |
Correct |
11 ms |
1624 KB |
Output is correct |
4 |
Correct |
18 ms |
2652 KB |
Output is correct |
5 |
Correct |
22 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
18 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
9 ms |
1628 KB |
Output is correct |
5 |
Correct |
18 ms |
2652 KB |
Output is correct |
6 |
Correct |
18 ms |
2652 KB |
Output is correct |
7 |
Correct |
16 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
9 ms |
1504 KB |
Output is correct |
10 |
Correct |
18 ms |
2652 KB |
Output is correct |
11 |
Correct |
18 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
16 ms |
464 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
16 ms |
472 KB |
Output is correct |
9 |
Correct |
16 ms |
348 KB |
Output is correct |
10 |
Correct |
16 ms |
476 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
16 ms |
472 KB |
Output is correct |
14 |
Correct |
16 ms |
348 KB |
Output is correct |
15 |
Correct |
16 ms |
596 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
16 ms |
476 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
18 ms |
472 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 |
348 KB |
Output is correct |
10 |
Correct |
16 ms |
472 KB |
Output is correct |
11 |
Correct |
8 ms |
1116 KB |
Output is correct |
12 |
Correct |
13 ms |
3276 KB |
Output is correct |
13 |
Correct |
14 ms |
3164 KB |
Output is correct |
14 |
Correct |
13 ms |
3164 KB |
Output is correct |
15 |
Correct |
16 ms |
488 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
10 ms |
2396 KB |
Output is correct |
18 |
Correct |
19 ms |
4596 KB |
Output is correct |
19 |
Correct |
20 ms |
4516 KB |
Output is correct |
20 |
Correct |
16 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
10 ms |
2396 KB |
Output is correct |
23 |
Correct |
18 ms |
4436 KB |
Output is correct |
24 |
Correct |
24 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
16 ms |
468 KB |
Output is correct |
28 |
Correct |
16 ms |
492 KB |
Output is correct |
29 |
Correct |
16 ms |
348 KB |
Output is correct |
30 |
Execution timed out |
1034 ms |
856 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |