#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;
stack<int>pila3;
vector<pair<int,int> >siguiente(n, make_pair(-1, n));
vector<int>v(n, -1);
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)%m;
}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);
while (!pila3.empty()){
if (alturas[pila3.top()]<=alturas[i]){
v[i]=pila3.top();
break;
}else pila3.pop();
}pila3.push(i);
}
long long unsigned int left, right, top, bottom;
long long unsigned int h, w;
for (int i=0; i<n; i++){
left=horizontales[i]+m-horizontales[siguiente[i].first+1];
left%=m;
right=horizontales[siguiente[i].second]+m-horizontales[siguiente[i].first+1];
right%=m;
top=alturas[i];
if (v[i]==-1) bottom=0;
else bottom=alturas[v[i]];
long long unsigned int a, b;
a=right*(right+1)/2%m;
b=left*(left+1)/2%m;
w=(a+m-b)%m;
a=top*(top+1)/2%m;
b=bottom*(bottom+1)/2%m;
h=(a+m-b)%m;
res+=(w*h)%m;
res%=m;
}cout<<res<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
21 ms |
1812 KB |
Output is correct |
4 |
Correct |
41 ms |
3440 KB |
Output is correct |
5 |
Correct |
41 ms |
3160 KB |
Output is correct |
6 |
Correct |
42 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
660 KB |
Output is correct |
3 |
Correct |
29 ms |
1792 KB |
Output is correct |
4 |
Correct |
57 ms |
3412 KB |
Output is correct |
5 |
Correct |
59 ms |
3412 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
6 ms |
604 KB |
Output is correct |
4 |
Correct |
29 ms |
1820 KB |
Output is correct |
5 |
Correct |
58 ms |
3428 KB |
Output is correct |
6 |
Correct |
59 ms |
3672 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
708 KB |
Output is correct |
9 |
Correct |
29 ms |
2064 KB |
Output is correct |
10 |
Correct |
55 ms |
3924 KB |
Output is correct |
11 |
Correct |
57 ms |
3956 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
22 ms |
2396 KB |
Output is correct |
12 |
Correct |
42 ms |
4452 KB |
Output is correct |
13 |
Correct |
42 ms |
4436 KB |
Output is correct |
14 |
Correct |
42 ms |
4184 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
856 KB |
Output is correct |
17 |
Correct |
29 ms |
2972 KB |
Output is correct |
18 |
Correct |
58 ms |
5344 KB |
Output is correct |
19 |
Correct |
59 ms |
5456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
860 KB |
Output is correct |
22 |
Correct |
30 ms |
2944 KB |
Output is correct |
23 |
Correct |
56 ms |
5716 KB |
Output is correct |
24 |
Correct |
58 ms |
5836 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
7 ms |
860 KB |
Output is correct |
31 |
Correct |
6 ms |
904 KB |
Output is correct |
32 |
Correct |
29 ms |
2440 KB |
Output is correct |
33 |
Correct |
31 ms |
2652 KB |
Output is correct |
34 |
Correct |
56 ms |
4692 KB |
Output is correct |
35 |
Correct |
56 ms |
4688 KB |
Output is correct |
36 |
Correct |
68 ms |
4956 KB |
Output is correct |
37 |
Correct |
62 ms |
4944 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
59 ms |
4948 KB |
Output is correct |
40 |
Correct |
62 ms |
4956 KB |
Output is correct |
41 |
Correct |
58 ms |
5200 KB |
Output is correct |
42 |
Correct |
78 ms |
5588 KB |
Output is correct |