#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
const int LIM = 1e5+1, MOD = 1e9+7;
int n, h[LIM], w[LIM], e[LIM], dp[LIM];
int nC2(int x){ return ((x * (x-1)) / 2) % MOD; }
int cnt(int x, int y){
return (nC2(x+1) * nC2(y+1)) % MOD;
}
int find(int u){
return e[u] < 0 ? u : e[u] = find(e[u]);
}
void unite(int u, int v, int currH){
u = find(u), v = find(v);
if(u != v){
if(e[u] > e[v]) swap(u, v);
dp[u] += cnt(w[u], h[u]) - cnt(w[u], currH);
dp[v] += cnt(w[v], h[v]) - cnt(w[v], currH);
e[u] += e[v], e[v] = u;
(w[u] += w[v]) %= MOD;
(dp[u] += dp[v]) %= MOD;
h[u] = currH;
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1; i<=n; ++i) cin >> h[i];
for(int i=1; i<=n; ++i) cin >> w[i];
int a[n]; iota(a, a+n, 1);
sort(a, a+n, [&](int &x, int &y){
return h[x] > h[y];
});
fill(e, e+LIM, -1);
for(int &i : a){
if(i != 1 && h[find(i-1)] >= h[i]) unite(i, i-1, h[i]);
if(i != n && h[find(i+1)] >= h[i]) unite(i, i+1, h[i]);
}
int r = find(1);
dp[r] += cnt(w[r], h[r]);
dp[r] %= MOD;
cout << (dp[r] + MOD) % MOD;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
15 ms |
3180 KB |
Output is correct |
4 |
Correct |
28 ms |
5316 KB |
Output is correct |
5 |
Correct |
28 ms |
5296 KB |
Output is correct |
6 |
Correct |
28 ms |
5316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
4 ms |
1612 KB |
Output is correct |
3 |
Correct |
17 ms |
3532 KB |
Output is correct |
4 |
Correct |
32 ms |
6084 KB |
Output is correct |
5 |
Correct |
35 ms |
6340 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
4 ms |
1500 KB |
Output is correct |
4 |
Correct |
17 ms |
3572 KB |
Output is correct |
5 |
Correct |
37 ms |
6036 KB |
Output is correct |
6 |
Correct |
35 ms |
6148 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
4 ms |
1508 KB |
Output is correct |
9 |
Correct |
17 ms |
3592 KB |
Output is correct |
10 |
Correct |
31 ms |
6012 KB |
Output is correct |
11 |
Correct |
32 ms |
6040 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
2 ms |
1092 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1096 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
15 ms |
3220 KB |
Output is correct |
12 |
Correct |
28 ms |
5316 KB |
Output is correct |
13 |
Correct |
29 ms |
5336 KB |
Output is correct |
14 |
Correct |
28 ms |
5256 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
4 ms |
1612 KB |
Output is correct |
17 |
Correct |
17 ms |
3620 KB |
Output is correct |
18 |
Correct |
32 ms |
6028 KB |
Output is correct |
19 |
Correct |
37 ms |
6204 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
4 ms |
1612 KB |
Output is correct |
22 |
Correct |
17 ms |
3644 KB |
Output is correct |
23 |
Correct |
30 ms |
5940 KB |
Output is correct |
24 |
Correct |
31 ms |
6012 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1096 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
1 ms |
1100 KB |
Output is correct |
30 |
Correct |
5 ms |
1612 KB |
Output is correct |
31 |
Correct |
5 ms |
1612 KB |
Output is correct |
32 |
Correct |
19 ms |
3464 KB |
Output is correct |
33 |
Correct |
22 ms |
3616 KB |
Output is correct |
34 |
Correct |
42 ms |
6004 KB |
Output is correct |
35 |
Correct |
37 ms |
5868 KB |
Output is correct |
36 |
Correct |
43 ms |
6084 KB |
Output is correct |
37 |
Correct |
43 ms |
6148 KB |
Output is correct |
38 |
Correct |
1 ms |
1020 KB |
Output is correct |
39 |
Correct |
43 ms |
6040 KB |
Output is correct |
40 |
Correct |
37 ms |
6116 KB |
Output is correct |
41 |
Correct |
34 ms |
6124 KB |
Output is correct |
42 |
Correct |
32 ms |
6136 KB |
Output is correct |