// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
constexpr ll inf = 1e18, N = 1e5 + 10, mod = 1e9 + 7, pr = 1000696969;
int w[N], h[N];
int l[N], r[N];
ll ps[N], bpow;
ll poww(ll x, int y) {
ll res = 1;
for(; y; y /= 2, x = x * x % mod)
if(y & 1) {
res *= x;
res %= mod;
}
return res;
}
ll choose(ll x) {
ll ans = (x * (x - 1)) % mod;
ans *= bpow;
ans %= mod;
return ans;
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
bpow = poww(2, mod - 2);
int n; cin >>n;
for(int i = 0; i < n; i++) cin >>h[i];
for(int i = 0; i < n; i++) cin >>w[i];
vector<int> vc;
for(int i = 0; i < n; i++) {
while(!vc.empty() && h[vc.back()] >= h[i]) vc.pop_back();
if(vc.empty()) l[i] = 0;
else l[i] = vc.back() + 1;
vc.push_back(i);
}
vc.clear();
for(int i = n - 1; i >= 0; i--) {
while(!vc.empty() && h[vc.back()] > h[i]) vc.pop_back();
if(vc.empty()) r[i] = n - 1;
else r[i] = vc.back() - 1;
vc.push_back(i);
}
for(int i = 0; i < n; i++) {
ps[i] = w[i];
if(i > 0) ps[i] += ps[i - 1];
ps[i] %= mod;
}
ll ans = 0;
for(int i = 0; i < n; i++) {
int mx = 0;
if(l[i] > 0) mx = max(mx, h[l[i] - 1] + 1);
if(r[i] < n - 1) mx = max(mx, h[r[i] + 1] + 1);
ll cnt = (choose(h[i] + 1) - choose(mx) + mod) % mod;
ll num = ps[r[i]];
if(l[i] > 0) num -= ps[l[i] - 1];
if(num < 0) num += mod;
num = choose(num + 1);
cnt *= num, cnt %= mod;
ans += cnt, ans %= mod;
}
cout<<ans;
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 |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
17 ms |
2320 KB |
Output is correct |
4 |
Correct |
22 ms |
4228 KB |
Output is correct |
5 |
Correct |
23 ms |
4048 KB |
Output is correct |
6 |
Correct |
21 ms |
3816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
596 KB |
Output is correct |
3 |
Correct |
21 ms |
1840 KB |
Output is correct |
4 |
Correct |
25 ms |
3200 KB |
Output is correct |
5 |
Correct |
41 ms |
3124 KB |
Output is correct |
6 |
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 |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
15 ms |
1752 KB |
Output is correct |
5 |
Correct |
29 ms |
3088 KB |
Output is correct |
6 |
Correct |
34 ms |
3164 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
21 ms |
1748 KB |
Output is correct |
10 |
Correct |
41 ms |
4948 KB |
Output is correct |
11 |
Correct |
27 ms |
5012 KB |
Output is correct |
12 |
Correct |
1 ms |
332 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 |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
0 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 |
1 ms |
324 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 |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
336 KB |
Output is correct |
11 |
Correct |
16 ms |
2316 KB |
Output is correct |
12 |
Correct |
23 ms |
4264 KB |
Output is correct |
13 |
Correct |
20 ms |
4040 KB |
Output is correct |
14 |
Correct |
22 ms |
3924 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
5 ms |
828 KB |
Output is correct |
17 |
Correct |
14 ms |
2696 KB |
Output is correct |
18 |
Correct |
34 ms |
5024 KB |
Output is correct |
19 |
Correct |
39 ms |
5192 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
832 KB |
Output is correct |
22 |
Correct |
18 ms |
2652 KB |
Output is correct |
23 |
Correct |
30 ms |
4932 KB |
Output is correct |
24 |
Correct |
38 ms |
5032 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
260 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
4 ms |
724 KB |
Output is correct |
31 |
Correct |
4 ms |
724 KB |
Output is correct |
32 |
Correct |
18 ms |
2384 KB |
Output is correct |
33 |
Correct |
14 ms |
2388 KB |
Output is correct |
34 |
Correct |
27 ms |
4388 KB |
Output is correct |
35 |
Correct |
24 ms |
4376 KB |
Output is correct |
36 |
Correct |
26 ms |
4548 KB |
Output is correct |
37 |
Correct |
28 ms |
4512 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
26 ms |
4556 KB |
Output is correct |
40 |
Correct |
27 ms |
4604 KB |
Output is correct |
41 |
Correct |
26 ms |
4684 KB |
Output is correct |
42 |
Correct |
26 ms |
4912 KB |
Output is correct |