#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 int addition){
long long chooseH = (h*(h+1)/2)%MOD;
long long chooseW = ((w*(w+1)/2)%MOD + (addition*w)%MOD )%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];
long long addW = 0;
int lPos = MyStack.size()-1;
while(lPos >= 0 and MyStack[lPos].first > myH){
addW += MyStack[lPos].second;
addW %= MOD;
MyStack.pop_back();
lPos = MyStack.size()-1;
}
MyStack.push_back({myH, myW+addW});
long long indepRectangles = calcOwnRectangles(myH, myW, addW);
ans += indepRectangles;
ans %= MOD;
//cout << myH << " " << myW << " "<< ans << endl;
dp[MyStack.size()-1] = getEndings(myH, (myW+addW)%MOD);
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 << myH << " " << ans << endl;
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
300 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
35 ms |
1896 KB |
Output is correct |
4 |
Correct |
57 ms |
4264 KB |
Output is correct |
5 |
Correct |
54 ms |
3608 KB |
Output is correct |
6 |
Correct |
54 ms |
3100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
8 ms |
724 KB |
Output is correct |
3 |
Correct |
43 ms |
2708 KB |
Output is correct |
4 |
Correct |
80 ms |
5280 KB |
Output is correct |
5 |
Correct |
85 ms |
5400 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
9 ms |
812 KB |
Output is correct |
4 |
Correct |
39 ms |
2716 KB |
Output is correct |
5 |
Correct |
82 ms |
5276 KB |
Output is correct |
6 |
Correct |
91 ms |
5448 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
724 KB |
Output is correct |
9 |
Correct |
42 ms |
2848 KB |
Output is correct |
10 |
Correct |
77 ms |
5148 KB |
Output is correct |
11 |
Correct |
80 ms |
5276 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
252 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
2 ms |
312 KB |
Output is correct |
11 |
Correct |
29 ms |
1908 KB |
Output is correct |
12 |
Correct |
55 ms |
4252 KB |
Output is correct |
13 |
Correct |
57 ms |
3616 KB |
Output is correct |
14 |
Correct |
57 ms |
3148 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
724 KB |
Output is correct |
17 |
Correct |
44 ms |
2824 KB |
Output is correct |
18 |
Correct |
80 ms |
5328 KB |
Output is correct |
19 |
Correct |
80 ms |
5416 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
8 ms |
776 KB |
Output is correct |
22 |
Correct |
41 ms |
2764 KB |
Output is correct |
23 |
Correct |
76 ms |
5148 KB |
Output is correct |
24 |
Correct |
81 ms |
5280 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
9 ms |
596 KB |
Output is correct |
31 |
Correct |
9 ms |
568 KB |
Output is correct |
32 |
Correct |
37 ms |
1876 KB |
Output is correct |
33 |
Correct |
43 ms |
1996 KB |
Output is correct |
34 |
Correct |
74 ms |
3484 KB |
Output is correct |
35 |
Correct |
73 ms |
3508 KB |
Output is correct |
36 |
Correct |
80 ms |
3748 KB |
Output is correct |
37 |
Correct |
77 ms |
3740 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
82 ms |
3868 KB |
Output is correct |
40 |
Correct |
99 ms |
3736 KB |
Output is correct |
41 |
Correct |
78 ms |
3916 KB |
Output is correct |
42 |
Correct |
83 ms |
4512 KB |
Output is correct |