/**
* author: NimaAryan
* created: 2023-12-01 09:55:42
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
constexpr i64 M = 1E9 + 7;
void add(i64& x, i64 y) {
if ((x += y) >= M) {
x -= M;
}
}
i64 C(i64 h, i64 w) {
return ((h * (h + 1) / 2) % M) * ((w * (w + 1) / 2) % M) % M;
}
i64 F(i64 h) {
return (h * (h + 1) / 2) % M;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<i64> h(N);
for (int i = 0; i < N; ++i) {
cin >> h[i];
}
vector<i64> w(N);
for (int i = 0; i < N; ++i) {
cin >> w[i];
}
vector<int> L(N);
vector<int> stk;
for (int i = 0; i < N; ++i) {
while (!stk.empty() && h[stk.back()] >= h[i]) {
stk.pop_back();
}
L[i] = stk.empty() ? -1 : stk.back();
stk.push_back(i);
}
vector<i64> sum(N + 1);
for (int i = 0; i < N; ++i) {
sum[i + 1] = sum[i] + w[i];
}
auto range_sum = [&](int l, int r) {
return (sum[r] - sum[l]) % M;
};
vector<i64> mono(N), vain(N);
for (int i = 0; i < N; ++i) {
add(mono[i], C(h[i], w[i]));
add(mono[i], F(h[i]) * w[i] % M * range_sum(L[i] + 1, i) % M);
if (L[i] != -1) {
add(mono[i], w[i] * vain[L[i]] % M);
}
add(vain[i], F(h[i]) * w[i] % M);
add(vain[i], F(h[i]) * range_sum(L[i] + 1, i) % M);
if (L[i] != -1) {
add(vain[i], vain[L[i]]);
}
}
cout << accumulate(mono.begin(), mono.end(), 0LL) % M << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
10 ms |
3144 KB |
Output is correct |
4 |
Correct |
17 ms |
5724 KB |
Output is correct |
5 |
Correct |
17 ms |
5724 KB |
Output is correct |
6 |
Correct |
17 ms |
5720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
10 ms |
2624 KB |
Output is correct |
4 |
Correct |
19 ms |
4700 KB |
Output is correct |
5 |
Correct |
19 ms |
4700 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
10 ms |
2392 KB |
Output is correct |
5 |
Correct |
20 ms |
4948 KB |
Output is correct |
6 |
Correct |
21 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
980 KB |
Output is correct |
9 |
Correct |
11 ms |
3676 KB |
Output is correct |
10 |
Correct |
22 ms |
6868 KB |
Output is correct |
11 |
Correct |
21 ms |
7124 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 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 |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
3164 KB |
Output is correct |
12 |
Correct |
16 ms |
5724 KB |
Output is correct |
13 |
Correct |
19 ms |
5856 KB |
Output is correct |
14 |
Correct |
17 ms |
5724 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
904 KB |
Output is correct |
17 |
Correct |
12 ms |
3420 KB |
Output is correct |
18 |
Correct |
19 ms |
6732 KB |
Output is correct |
19 |
Correct |
25 ms |
6628 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1116 KB |
Output is correct |
22 |
Correct |
12 ms |
3788 KB |
Output is correct |
23 |
Correct |
21 ms |
7064 KB |
Output is correct |
24 |
Correct |
24 ms |
7124 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
11 ms |
3408 KB |
Output is correct |
33 |
Correct |
11 ms |
3420 KB |
Output is correct |
34 |
Correct |
20 ms |
6488 KB |
Output is correct |
35 |
Correct |
21 ms |
6492 KB |
Output is correct |
36 |
Correct |
21 ms |
6480 KB |
Output is correct |
37 |
Correct |
22 ms |
6708 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
21 ms |
6492 KB |
Output is correct |
40 |
Correct |
27 ms |
6612 KB |
Output is correct |
41 |
Correct |
21 ms |
6592 KB |
Output is correct |
42 |
Correct |
22 ms |
7124 KB |
Output is correct |