#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 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];
}
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]) {
LL fhw = (f(h[st.top()]) * w[st.top()])%MOD;
sumFhw = (sumFhw - fhw + MOD)%MOD;
st.pop();
}
int firstLowerIdx = (st.empty() ? 0 : st.top());
LL rectanglesOnTheCurrentFenceAndTallerNeighbors = 0;
if (firstLowerIdx != i - 1) {
rectanglesOnTheCurrentFenceAndTallerNeighbors = (((sumWidth[i - 1] - sumWidth[firstLowerIdx])%MOD * w[i])%MOD * f(h[i]))%MOD;
ans = (ans + rectanglesOnTheCurrentFenceAndTallerNeighbors)%MOD;
}
LL rectanglesOnTheCurrentFenceAndLowerNeighbors = (sumFhw * w[i])%MOD;
ans = (ans + rectanglesOnTheCurrentFenceAndLowerNeighbors)%MOD;
st.push(i);
sumFhw = (sumFhw + (f(h[i]) * w[i])%MOD)%MOD;
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 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 |
1 ms |
224 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
9 ms |
724 KB |
Output is correct |
3 |
Correct |
44 ms |
2436 KB |
Output is correct |
4 |
Correct |
89 ms |
4556 KB |
Output is correct |
5 |
Correct |
85 ms |
4560 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 |
340 KB |
Output is correct |
3 |
Correct |
9 ms |
724 KB |
Output is correct |
4 |
Correct |
42 ms |
2336 KB |
Output is correct |
5 |
Correct |
80 ms |
4468 KB |
Output is correct |
6 |
Correct |
80 ms |
4660 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
696 KB |
Output is correct |
9 |
Correct |
42 ms |
2524 KB |
Output is correct |
10 |
Incorrect |
75 ms |
4796 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 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 |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |