#include<bits/stdc++.h>
using namespace std;
int n;
int MOD = 1e9 + 7;
long long calcOwnRectangles(long long int h, long long int w){
long long chooseH = (h*(h+1)/2)%MOD;
long long chooseW = (w*(w+1)/2)%MOD;
long long ans = (chooseH*chooseW)%MOD;
return ans;
}
long long getEndings(long long h, long long w){
long long WOptions = w;
long long HOptions = (h*(h+1)/2)%MOD;
long long ans = (WOptions*HOptions)%MOD;
return ans;
}
int main(){
cin >> n;
int heights[n];
int widths[n];
long long dp[n];
long long int ans = 0;
for(int i = 0; i < n; ++i) cin >> heights[i];
for(int i = 0; i < n; ++i) cin >> widths[i];
vector<pair<long long int, long long int>> MyStack;
MyStack.reserve(n);
for(int i = 0; i < n; ++i){
long long int myH = heights[i];
long long int myW = widths[i];
int lPos = MyStack.size()-1;
while(lPos >= 0 and MyStack[lPos].first > myH){
myW += MyStack[lPos].second;
myW %= MOD;
MyStack.pop_back();
lPos = MyStack.size()-1;
}
MyStack.push_back({myH, myW});
long long indepRectangles = calcOwnRectangles(myH, myW);
ans += indepRectangles;
ans %= MOD;
//cout << ans << endl;
dp[MyStack.size()-1] = getEndings(myH, myW);
if(MyStack.size() == 1){
//Este es el primero
continue;
}else{
//Este NO es el primero --> para los rectángulos se pueden continuar los que acaban en el anterior
ans += (dp[MyStack.size()-2]*myW)%MOD;
ans %= MOD;
//Tambien se puede hacer que el numero de los que acaban en esta posicion incorpore los que acaban en la anterior
dp[MyStack.size()-1] += dp[MyStack.size()-2];
dp[MyStack.size()-1] %= MOD;
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
10 ms |
824 KB |
Output is correct |
3 |
Correct |
41 ms |
2712 KB |
Output is correct |
4 |
Correct |
84 ms |
5276 KB |
Output is correct |
5 |
Correct |
84 ms |
5424 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
724 KB |
Output is correct |
4 |
Correct |
41 ms |
2792 KB |
Output is correct |
5 |
Correct |
82 ms |
5260 KB |
Output is correct |
6 |
Correct |
86 ms |
5404 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
11 ms |
724 KB |
Output is correct |
9 |
Correct |
44 ms |
2836 KB |
Output is correct |
10 |
Correct |
80 ms |
5204 KB |
Output is correct |
11 |
Correct |
81 ms |
5276 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |