#include <bits/stdc++.h>
#define int long long int
#define ld long double
#define pb push_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N = 1e5 + 5;
const int INF = 1e9 + 500;
const int MOD = 1e9 + 7;
int add(int x, int y) {
if(x + y >= MOD) return x + y - MOD;
return x + y;
}
int mult(int x, int y) {
return (int)((1ll * x * y) % MOD);
}
int c2(int x) {
return (int)((1ll * x * (x - 1) / 2) % MOD);
}
int subt(int x, int y) {
if(x - y < 0) return x - y + MOD;
return x - y;
}
void solve() {
int n;
cin >> n;
vector<int> h(n + 1, 0), w(n + 1, 0), dp(n + 1, 0), p(n + 1, 0);
vector<int> last(n + 1, 0);
int ans = 0;
for(int i = 1; i <= n; i++) {
cin >> h[i];
}
for(int i = 1; i <= n; i++) {
cin >> w[i];
}
for(int i = 1; i <= n; i++) {
p[i] = add(p[i - 1] , w[i]);
}
vector<int> stck;
stck.pb(0);
for(int i = 1; i <= n; i++) {
while(stck.size() && h[i] < h[stck.back()]) {
stck.pop_back();
}
assert(stck.size());
last[i] = stck.back();
stck.pb(i);
}
// for(int i = 1; i <= n; i++) {
// cout << last[i] << " ";
// }
// cout << "\n";
for(int i = 1; i <= n; i++) {
dp[i] = add(dp[last[i]], mult(subt(p[i] , p[last[i]]), c2(h[i] + 1) ));
int cont = mult(dp[last[i]], w[i]);
cont = add(cont, mult( subt(p[i - 1], p[last[i]]), mult(w[i], c2(h[i] + 1) ) ) );
cont = add(cont, mult(c2(w[i] + 1), c2(h[i] + 1)));
ans = add(ans, cont);
// cout << "i:" << i << " dp:" << dp[i] << "\n";
// cout << "cont:" << cont << "\n";
}
cout << ans << "\n";
}
signed main() {
fastio();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
3296 KB |
Output is correct |
4 |
Correct |
16 ms |
6560 KB |
Output is correct |
5 |
Correct |
17 ms |
6200 KB |
Output is correct |
6 |
Correct |
15 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1116 KB |
Output is correct |
3 |
Correct |
10 ms |
3808 KB |
Output is correct |
4 |
Correct |
22 ms |
7380 KB |
Output is correct |
5 |
Correct |
21 ms |
7388 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
12 ms |
3952 KB |
Output is correct |
5 |
Correct |
26 ms |
7448 KB |
Output is correct |
6 |
Correct |
20 ms |
7584 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
1112 KB |
Output is correct |
9 |
Correct |
10 ms |
3956 KB |
Output is correct |
10 |
Correct |
19 ms |
7132 KB |
Output is correct |
11 |
Correct |
22 ms |
7452 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
3296 KB |
Output is correct |
12 |
Correct |
16 ms |
6556 KB |
Output is correct |
13 |
Correct |
16 ms |
6176 KB |
Output is correct |
14 |
Correct |
17 ms |
5768 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
1116 KB |
Output is correct |
17 |
Correct |
11 ms |
3956 KB |
Output is correct |
18 |
Correct |
20 ms |
7380 KB |
Output is correct |
19 |
Correct |
20 ms |
7568 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
10 ms |
4064 KB |
Output is correct |
23 |
Correct |
19 ms |
7124 KB |
Output is correct |
24 |
Correct |
20 ms |
7320 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
976 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
10 ms |
3308 KB |
Output is correct |
33 |
Correct |
10 ms |
3164 KB |
Output is correct |
34 |
Correct |
22 ms |
5976 KB |
Output is correct |
35 |
Correct |
18 ms |
5908 KB |
Output is correct |
36 |
Correct |
20 ms |
6328 KB |
Output is correct |
37 |
Correct |
19 ms |
6232 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
19 ms |
6236 KB |
Output is correct |
40 |
Correct |
19 ms |
6112 KB |
Output is correct |
41 |
Correct |
21 ms |
6428 KB |
Output is correct |
42 |
Correct |
19 ms |
6936 KB |
Output is correct |