#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 |
3148 KB |
Output is correct |
4 |
Correct |
28 ms |
5344 KB |
Output is correct |
5 |
Correct |
29 ms |
5316 KB |
Output is correct |
6 |
Correct |
28 ms |
5364 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 |
16 ms |
3532 KB |
Output is correct |
4 |
Correct |
37 ms |
6044 KB |
Output is correct |
5 |
Correct |
36 ms |
6140 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 |
1588 KB |
Output is correct |
4 |
Correct |
17 ms |
3616 KB |
Output is correct |
5 |
Correct |
32 ms |
6208 KB |
Output is correct |
6 |
Correct |
35 ms |
6248 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1612 KB |
Output is correct |
9 |
Correct |
18 ms |
3572 KB |
Output is correct |
10 |
Correct |
31 ms |
6108 KB |
Output is correct |
11 |
Correct |
31 ms |
6096 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 |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1064 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 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 |
1100 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 |
3148 KB |
Output is correct |
12 |
Correct |
28 ms |
5296 KB |
Output is correct |
13 |
Correct |
30 ms |
5344 KB |
Output is correct |
14 |
Correct |
32 ms |
5264 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
6 ms |
1592 KB |
Output is correct |
17 |
Correct |
18 ms |
3536 KB |
Output is correct |
18 |
Correct |
32 ms |
6040 KB |
Output is correct |
19 |
Correct |
44 ms |
6144 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
5 ms |
1612 KB |
Output is correct |
22 |
Correct |
18 ms |
3620 KB |
Output is correct |
23 |
Correct |
30 ms |
5992 KB |
Output is correct |
24 |
Correct |
31 ms |
6112 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
1 ms |
1180 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
5 ms |
1504 KB |
Output is correct |
31 |
Correct |
5 ms |
1612 KB |
Output is correct |
32 |
Correct |
18 ms |
3532 KB |
Output is correct |
33 |
Correct |
22 ms |
3520 KB |
Output is correct |
34 |
Correct |
42 ms |
5836 KB |
Output is correct |
35 |
Correct |
37 ms |
5940 KB |
Output is correct |
36 |
Correct |
42 ms |
6056 KB |
Output is correct |
37 |
Correct |
43 ms |
6144 KB |
Output is correct |
38 |
Correct |
1 ms |
1100 KB |
Output is correct |
39 |
Correct |
42 ms |
6216 KB |
Output is correct |
40 |
Correct |
38 ms |
6088 KB |
Output is correct |
41 |
Correct |
36 ms |
6084 KB |
Output is correct |
42 |
Correct |
37 ms |
6040 KB |
Output is correct |