#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
const int MOD = 1'000'000'007;
const int MX = 400'005;
//long double PI = 3.14159265358979323846264338327950;
int main(){
cin.tie(0);ios_base::sync_with_stdio(0);
//freopen ("in.txt","r",stdin);
//int n_casos=1; cin>>n_casos;
//while(n_casos--) caso_teste();
int n; cin>>n;
ll h[n+1], w[n+1];
stack<pair<ll,ll>> s;
s.push(make_pair(0,0));//par (altura,largura)
ll ans=0ll;
for(int i=0; i<n; i++){
cin>>h[i];
}
for(int i=0; i<n; i++){
cin>>w[i];
}
h[n]=0; w[n]=0;
for(int i=0; i<n+1; i++){
ll largura;
while((s.top()).f>h[i]){
largura=s.top().s%MOD;
ll altura;
ll ALTURA=s.top().f;
s.pop();
altura=max(h[i],s.top().f);
ALTURA-=altura;
ans+=( (((((ALTURA*(ALTURA+1ll))/2ll)%MOD) + (altura*ALTURA)%MOD)%MOD) * (((largura*(largura+1ll))/2ll)%MOD) )%MOD;
ans%=MOD;
//cout<<ALTURA<<' '<<altura<<' '<<ans<<'\n';
if(s.top().f>=h[i])s.top().s+=largura;
else w[i]+=largura;
}
if(h[i]==s.top().f){
s.top().s+=w[i];
}else{
s.push({h[i],w[i]});
}
}
cout<<ans%MOD<<'\n';;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
12 ms |
1580 KB |
Output is correct |
4 |
Correct |
23 ms |
3004 KB |
Output is correct |
5 |
Correct |
27 ms |
3040 KB |
Output is correct |
6 |
Correct |
26 ms |
3008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
588 KB |
Output is correct |
3 |
Correct |
14 ms |
1976 KB |
Output is correct |
4 |
Correct |
26 ms |
3780 KB |
Output is correct |
5 |
Correct |
29 ms |
3900 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
588 KB |
Output is correct |
4 |
Correct |
15 ms |
1996 KB |
Output is correct |
5 |
Correct |
27 ms |
3784 KB |
Output is correct |
6 |
Correct |
36 ms |
3896 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
716 KB |
Output is correct |
9 |
Correct |
15 ms |
2352 KB |
Output is correct |
10 |
Correct |
29 ms |
5316 KB |
Output is correct |
11 |
Correct |
30 ms |
5444 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
12 ms |
1612 KB |
Output is correct |
12 |
Correct |
22 ms |
3016 KB |
Output is correct |
13 |
Correct |
22 ms |
3020 KB |
Output is correct |
14 |
Correct |
21 ms |
3016 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
3 ms |
664 KB |
Output is correct |
17 |
Correct |
13 ms |
1996 KB |
Output is correct |
18 |
Correct |
26 ms |
3772 KB |
Output is correct |
19 |
Correct |
29 ms |
3904 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
4 ms |
844 KB |
Output is correct |
22 |
Correct |
15 ms |
2360 KB |
Output is correct |
23 |
Correct |
29 ms |
5320 KB |
Output is correct |
24 |
Correct |
32 ms |
5428 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
5 ms |
588 KB |
Output is correct |
31 |
Correct |
4 ms |
716 KB |
Output is correct |
32 |
Correct |
18 ms |
1844 KB |
Output is correct |
33 |
Correct |
15 ms |
2064 KB |
Output is correct |
34 |
Correct |
28 ms |
3524 KB |
Output is correct |
35 |
Correct |
28 ms |
3628 KB |
Output is correct |
36 |
Correct |
30 ms |
3784 KB |
Output is correct |
37 |
Correct |
30 ms |
3768 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
32 ms |
3760 KB |
Output is correct |
40 |
Correct |
35 ms |
3780 KB |
Output is correct |
41 |
Correct |
29 ms |
3908 KB |
Output is correct |
42 |
Correct |
29 ms |
4628 KB |
Output is correct |