#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int m=1e9+7;
int main(){
int n;
cin>>n;
long long int res=0;
vector<long long int>horizontales(n+1);
vector<long long int>alturas(n);
stack<int>pila1;
stack<int>pila2;
vector<pair<int,int>>siguiente(n, make_pair(-1, n));
for (int i=0; i<n; i++) cin>>alturas[i];
horizontales[0]=0;
int x;
for (int i=0; i<n; i++){
cin>>x;
horizontales[i+1]=horizontales[i]+x;
}for (int i=0; i<n; i++){
while (!pila1.empty()){
if (alturas[pila1.top()]>=alturas[i]) pila1.pop();
else{
siguiente[i].first=pila1.top();
break;
}
}pila1.push(i);
while (!pila2.empty()){
if (alturas[pila2.top()]>=alturas[n-i-1]) pila2.pop();
else{
siguiente[n-i-1].second=pila2.top();
break;
}
}pila2.push(n-i-1);
}
int left=0, right, top, bottom=0;
long long int h, w;
for (int i=0; i<n; i++){
left=horizontales[i]-horizontales[siguiente[i].first+1];
right=horizontales[siguiente[i].second]-horizontales[siguiente[i].first+1];
top=alturas[i];
w=(right*(right+1)-left*(left+1))/2;
w%=m;
if (bottom>top) h=top*(top+1)/2;
else h=(top*(top+1)-bottom*(bottom+1))/2;
h%=m;
res+=(w*h)%m;
res%=m;
bottom=top;
}cout<<res<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
428 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |