This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 ("out.txt","w",stdout);
//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=0;
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;
ll altura=s.top().f;
ll ALTURA=s.top().f;
s.pop();
altura=max(h[i],s.top().f);
ans+=( (((ALTURA*(ALTURA+1ll))/2ll)%MOD) * (((largura*(largura+1ll))/2ll)%MOD) )%MOD;
ans-=( (((altura*(altura+1ll))/2ll)%MOD) * (((largura*(largura+1ll))/2ll)%MOD) )%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<<'\n';;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |