#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define MOD 1000000007
#define MAXN 100000
int n;
LL h[MAXN + 3], w[MAXN + 3], sumWidth[MAXN + 3], sumFhw = 0, ans = 0;
stack<int> st;
LL f(LL k) {
LL k1 = k + 1;
return k%2 == 0 ? ((k/2)%MOD * (k1)%MOD)%MOD : (k%MOD * (k1/2)%MOD)%MOD;
}
int solveBruteForce() {
int psum[55][55];
memset(psum, 0, sizeof(psum));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= h[i]; j++) {
psum[i][j] = 1;
}
cout << endl;
}
for (int i = 1; i <= 50; i++) {
for (int j = 1; j <= 50; j++) {
psum[i][j] += psum[i - 1][j] + psum[i][j - 1] - psum[i - 1][j - 1];
}
}
int ans = 0;
for (int i1 = 1; i1 <= 50; i1++) {
for (int j1 = 1; j1 <= 50; j1++) {
for (int i2 = i1; i2 <= 50; i2++) {
for (int j2 = j1; j2 <= 50; j2++) {
int sum = psum[i2][j2] - psum[i2][j1 - 1] - psum[i1 - 1][j2] + psum[i1 - 1][j1 - 1];
if (sum == (i2 - i1 + 1) * (j2 - j1 + 1)) {
ans += 1;
}
}
}
}
}
return ans;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
sumWidth[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> w[i];
sumWidth[i] = sumWidth[i - 1] + w[i];
}
// cout << solveBruteForce() << endl;
// return 0;
for (int i = 1; i <= n; i++) {
LL rectanglesOnTheCurrentFence = (f(h[i]) * f(w[i]))%MOD;
ans = (ans + rectanglesOnTheCurrentFence)%MOD;
while (!st.empty() && h[st.top()] >= h[i]) {
int top = st.top(); st.pop();
if (st.empty()) {
sumFhw = 0;
} else {
LL totalWidth = sumWidth[top] - sumWidth[st.top()];
LL fhw = (f(h[top]) * (totalWidth%MOD))%MOD;
sumFhw = (sumFhw - fhw + MOD)%MOD;
}
}
int firstLowerIdx = (st.empty() ? 0 : st.top());
LL rectanglesOnTheCurrentFenceAndTallerNeighbors = 0;
if (firstLowerIdx != i - 1) {
LL waysToPickWidth = ((sumWidth[i - 1] - sumWidth[firstLowerIdx])%MOD * w[i])%MOD;
rectanglesOnTheCurrentFenceAndTallerNeighbors = (waysToPickWidth * f(h[i]))%MOD;
ans = (ans + rectanglesOnTheCurrentFenceAndTallerNeighbors)%MOD;
}
LL rectanglesOnTheCurrentFenceAndLowerNeighbors = (sumFhw * w[i])%MOD;
ans = (ans + rectanglesOnTheCurrentFenceAndLowerNeighbors)%MOD;
LL totalWidth = sumWidth[i] - (st.empty() ? 0 : sumWidth[st.top()]);
sumFhw = (sumFhw + (f(h[i]) * (totalWidth%MOD))%MOD)%MOD;
st.push(i);
// cout << "\ni: " << i << endl;
// cout << "rectanglesOnTheCurrentFence: " << rectanglesOnTheCurrentFence << endl;
// cout << "rectanglesOnTheCurrentFenceAndTallerNeighbors: " << rectanglesOnTheCurrentFenceAndTallerNeighbors << endl;
// cout << "rectanglesOnTheCurrentFenceAndLowerNeighbors: " << rectanglesOnTheCurrentFenceAndLowerNeighbors << endl;
// cout << "sumFhw: " << sumFhw << endl;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
28 ms |
1492 KB |
Output is correct |
4 |
Correct |
53 ms |
2632 KB |
Output is correct |
5 |
Correct |
56 ms |
2528 KB |
Output is correct |
6 |
Correct |
55 ms |
2596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
13 ms |
520 KB |
Output is correct |
3 |
Correct |
41 ms |
1400 KB |
Output is correct |
4 |
Correct |
76 ms |
2548 KB |
Output is correct |
5 |
Correct |
85 ms |
2568 KB |
Output is correct |
6 |
Correct |
0 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 |
340 KB |
Output is correct |
3 |
Correct |
12 ms |
456 KB |
Output is correct |
4 |
Correct |
43 ms |
1456 KB |
Output is correct |
5 |
Correct |
77 ms |
2632 KB |
Output is correct |
6 |
Correct |
79 ms |
2600 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
596 KB |
Output is correct |
9 |
Correct |
40 ms |
1528 KB |
Output is correct |
10 |
Correct |
72 ms |
2964 KB |
Output is correct |
11 |
Correct |
83 ms |
2968 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
32 ms |
2056 KB |
Output is correct |
12 |
Correct |
54 ms |
3740 KB |
Output is correct |
13 |
Correct |
76 ms |
3796 KB |
Output is correct |
14 |
Correct |
61 ms |
3696 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
704 KB |
Output is correct |
17 |
Correct |
40 ms |
2364 KB |
Output is correct |
18 |
Correct |
79 ms |
4508 KB |
Output is correct |
19 |
Correct |
81 ms |
4748 KB |
Output is correct |
20 |
Correct |
2 ms |
320 KB |
Output is correct |
21 |
Correct |
10 ms |
704 KB |
Output is correct |
22 |
Correct |
44 ms |
2436 KB |
Output is correct |
23 |
Correct |
74 ms |
4800 KB |
Output is correct |
24 |
Correct |
77 ms |
4880 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
316 KB |
Output is correct |
30 |
Correct |
9 ms |
724 KB |
Output is correct |
31 |
Correct |
9 ms |
648 KB |
Output is correct |
32 |
Correct |
38 ms |
2252 KB |
Output is correct |
33 |
Correct |
51 ms |
2396 KB |
Output is correct |
34 |
Correct |
72 ms |
4280 KB |
Output is correct |
35 |
Correct |
75 ms |
4284 KB |
Output is correct |
36 |
Correct |
83 ms |
4556 KB |
Output is correct |
37 |
Correct |
94 ms |
4492 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
86 ms |
4452 KB |
Output is correct |
40 |
Correct |
79 ms |
4472 KB |
Output is correct |
41 |
Correct |
83 ms |
4556 KB |
Output is correct |
42 |
Correct |
82 ms |
4680 KB |
Output is correct |