#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <stack>
typedef long long llong;
const int MAXN = 100000 + 10;
const int MOD = 1e9 + 7;
const int INF = 2e9;
int n;
llong h[MAXN];
llong w[MAXN];
int prev[MAXN];
int next[MAXN];
llong prefix[MAXN];
std::stack <int> st;
void solve()
{
for (int i = 1 ; i <= n ; ++i)
{
prefix[i] = prefix[i - 1] + w[i];
}
h[0] = h[n + 1] = 0;
st.push(0);
for (int i = 1 ; i <= n ; ++i)
{
while (h[st.top()] > h[i])
{
st.pop();
}
prev[i] = st.top();
st.push(i);
}
while (st.size()) st.pop();
st.push(n + 1);
for (int i = n ; i >= 1 ; --i)
{
while (h[st.top()] >= h[i])
{
st.pop();
}
next[i] = st.top();
st.push(i);
}
llong ans = 0;
for (int i = 1 ; i <= n ; ++i)
{
llong cntPrev = (prefix[i - 1] - prefix[prev[i]]) % MOD;
llong cntNext = (prefix[next[i] - 1] - prefix[i]) % MOD;
ans += ((cntPrev * cntNext) % MOD) * ((h[i] * (h[i] + 1) / 2) % MOD);
ans += ((cntPrev * w[i]) % MOD) * ((h[i] * (h[i] + 1) / 2) % MOD);
ans += ((cntNext * w[i]) % MOD) * ((h[i] * (h[i] + 1) / 2) % MOD);
ans += ((w[i] * (w[i] + 1) / 2) % MOD) * ((h[i] * (h[i] + 1) / 2) % MOD);
ans %= MOD;
}
std::cout << ans << '\n';
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> h[i];
}
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> w[i];
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
8 ms |
3676 KB |
Output is correct |
4 |
Correct |
15 ms |
4956 KB |
Output is correct |
5 |
Correct |
15 ms |
4684 KB |
Output is correct |
6 |
Correct |
14 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2908 KB |
Output is correct |
3 |
Correct |
11 ms |
4068 KB |
Output is correct |
4 |
Correct |
19 ms |
5724 KB |
Output is correct |
5 |
Correct |
19 ms |
5972 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2792 KB |
Output is correct |
4 |
Correct |
9 ms |
4140 KB |
Output is correct |
5 |
Correct |
18 ms |
5712 KB |
Output is correct |
6 |
Correct |
19 ms |
5976 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
3 ms |
2904 KB |
Output is correct |
9 |
Correct |
9 ms |
4052 KB |
Output is correct |
10 |
Correct |
18 ms |
5588 KB |
Output is correct |
11 |
Correct |
18 ms |
5712 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2524 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2520 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2400 KB |
Output is correct |
5 |
Correct |
0 ms |
2400 KB |
Output is correct |
6 |
Correct |
1 ms |
2400 KB |
Output is correct |
7 |
Correct |
0 ms |
2516 KB |
Output is correct |
8 |
Correct |
0 ms |
2400 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
8 ms |
3680 KB |
Output is correct |
12 |
Correct |
15 ms |
5212 KB |
Output is correct |
13 |
Correct |
15 ms |
4700 KB |
Output is correct |
14 |
Correct |
15 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
10 ms |
4080 KB |
Output is correct |
18 |
Correct |
17 ms |
5724 KB |
Output is correct |
19 |
Correct |
18 ms |
5964 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
9 ms |
4184 KB |
Output is correct |
23 |
Correct |
17 ms |
5588 KB |
Output is correct |
24 |
Correct |
18 ms |
5724 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2552 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
9 ms |
3924 KB |
Output is correct |
33 |
Correct |
10 ms |
3932 KB |
Output is correct |
34 |
Correct |
18 ms |
5072 KB |
Output is correct |
35 |
Correct |
18 ms |
5288 KB |
Output is correct |
36 |
Correct |
19 ms |
5416 KB |
Output is correct |
37 |
Correct |
20 ms |
5460 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
18 ms |
5332 KB |
Output is correct |
40 |
Correct |
18 ms |
5468 KB |
Output is correct |
41 |
Correct |
18 ms |
5584 KB |
Output is correct |
42 |
Correct |
18 ms |
5724 KB |
Output is correct |