#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N = 1e5 + 10, MOD = 1e9 + 7;
int power(int a, int b) {
if(b == 0)
return 1;
int res = power(a, b / 2);
res = 1LL * res * res % MOD;
if(b & 1) res = 1LL * res * a % MOD;
return res;
}
int two = power(2, MOD - 2);
int C2(int a) { return 1LL * a * (a - 1) % MOD * two % MOD; }
int H[N], W[N], L[N], PS[N], M[N], len[N], n;
ll ans;
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> H[i];
for(int i = 1; i <= n; i++)
cin >> W[i], PS[i] = (W[i] + PS[i - 1]) % MOD;
stack<int> st;
for(int i = 1; i <= n; i++) {
while(!st.empty() and H[i] <= H[st.top()]) st.pop();
L[i] = ((int) st.size() > 0 ? st.top() + 1 : 1);
if(!st.empty()) M[i] = max(M[i], H[st.top()]);
st.push(i);
}
st = stack<int>();
for(int i = n; i >= 1; i--) {
while(!st.empty() and H[i] < H[st.top()]) st.pop();
len[i] = (PS[((int) st.size() > 0 ? st.top() - 1 : n)] - PS[L[i] - 1] + MOD) % MOD;
if(!st.empty()) M[i] = max(M[i], H[st.top()]);
st.push(i);
}
for(int i = 1; i <= n; i++)
ans += 1LL * C2(H[i] + 1) * C2(1 + (len[i])) % MOD, ans %= MOD;
for(int i = 1; i <= n; i++)
ans -= 1LL * C2(M[i] + 1) * C2(1 + (len[i])) % MOD, ans = (ans + MOD) % MOD;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
9 ms |
1492 KB |
Output is correct |
4 |
Correct |
21 ms |
4048 KB |
Output is correct |
5 |
Correct |
22 ms |
3940 KB |
Output is correct |
6 |
Correct |
19 ms |
3844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
11 ms |
1608 KB |
Output is correct |
4 |
Correct |
23 ms |
2972 KB |
Output is correct |
5 |
Correct |
24 ms |
3028 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
14 ms |
1616 KB |
Output is correct |
5 |
Correct |
23 ms |
3068 KB |
Output is correct |
6 |
Correct |
25 ms |
3056 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
3 ms |
536 KB |
Output is correct |
9 |
Correct |
14 ms |
1748 KB |
Output is correct |
10 |
Correct |
23 ms |
3148 KB |
Output is correct |
11 |
Correct |
28 ms |
3096 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
10 ms |
1492 KB |
Output is correct |
12 |
Correct |
19 ms |
4076 KB |
Output is correct |
13 |
Correct |
19 ms |
3916 KB |
Output is correct |
14 |
Correct |
18 ms |
3788 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
12 ms |
2644 KB |
Output is correct |
18 |
Correct |
26 ms |
4948 KB |
Output is correct |
19 |
Correct |
32 ms |
5068 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
728 KB |
Output is correct |
22 |
Correct |
13 ms |
2656 KB |
Output is correct |
23 |
Correct |
22 ms |
4776 KB |
Output is correct |
24 |
Correct |
23 ms |
4928 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
356 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
356 KB |
Output is correct |
30 |
Correct |
4 ms |
724 KB |
Output is correct |
31 |
Correct |
4 ms |
720 KB |
Output is correct |
32 |
Correct |
16 ms |
2388 KB |
Output is correct |
33 |
Correct |
13 ms |
2408 KB |
Output is correct |
34 |
Correct |
24 ms |
4360 KB |
Output is correct |
35 |
Correct |
23 ms |
4400 KB |
Output is correct |
36 |
Correct |
27 ms |
4520 KB |
Output is correct |
37 |
Correct |
26 ms |
4580 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
32 ms |
4536 KB |
Output is correct |
40 |
Correct |
29 ms |
4612 KB |
Output is correct |
41 |
Correct |
23 ms |
4588 KB |
Output is correct |
42 |
Correct |
25 ms |
4684 KB |
Output is correct |