#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int mod = 1e9 + 7;
int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int sub(int a, int b) {
return a >= b ? a - b : a + mod - b;
}
int mul(int a, int b) {
return 1ll * a * b % mod;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int i2 = (mod + 1) / 2;
auto sshit = [&](int x) -> int {
return mul(mul(x, x + 1), i2);
};
int n;
cin >> n;
vector<pair<int, int>> st;
vector<int> h(n), w(n);
for (auto& i : h) cin >> i;
for (auto& i : w) cin >> i;
int sum = 0;
int ans = 0;
for (int i = 0; i < n; ++i) {
int cnt = w[i];
while (st.size() && st.back().first >= h[i]) {
cnt = add(cnt, st.back().second);
sum = sub(sum, mul(sshit(st.back().first), st.back().second));
st.pop_back();
}
st.emplace_back(h[i], cnt);
sum = add(sum, mul(sshit(h[i]), cnt));
int cur = mul(sum, w[i]);
cur = sub(cur, mul(sshit(h[i]), mul(w[i], w[i])));
cur = add(cur, mul(sshit(w[i]), sshit(h[i])));
ans = add(ans, cur);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
1372 KB |
Output is correct |
4 |
Correct |
19 ms |
2396 KB |
Output is correct |
5 |
Correct |
21 ms |
2412 KB |
Output is correct |
6 |
Correct |
18 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
13 ms |
604 KB |
Output is correct |
4 |
Correct |
25 ms |
1252 KB |
Output is correct |
5 |
Correct |
24 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
16 ms |
860 KB |
Output is correct |
5 |
Correct |
28 ms |
1228 KB |
Output is correct |
6 |
Correct |
28 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
788 KB |
Output is correct |
9 |
Correct |
12 ms |
2140 KB |
Output is correct |
10 |
Correct |
23 ms |
4100 KB |
Output is correct |
11 |
Correct |
24 ms |
4320 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
472 KB |
Output is correct |
16 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
10 ms |
1228 KB |
Output is correct |
12 |
Correct |
19 ms |
2384 KB |
Output is correct |
13 |
Correct |
19 ms |
2264 KB |
Output is correct |
14 |
Correct |
19 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
600 KB |
Output is correct |
17 |
Correct |
13 ms |
1624 KB |
Output is correct |
18 |
Correct |
27 ms |
3180 KB |
Output is correct |
19 |
Correct |
28 ms |
3300 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
12 ms |
2128 KB |
Output is correct |
23 |
Correct |
25 ms |
4060 KB |
Output is correct |
24 |
Correct |
29 ms |
4304 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
16 ms |
1480 KB |
Output is correct |
33 |
Correct |
13 ms |
1628 KB |
Output is correct |
34 |
Correct |
25 ms |
2776 KB |
Output is correct |
35 |
Correct |
24 ms |
2912 KB |
Output is correct |
36 |
Correct |
33 ms |
2916 KB |
Output is correct |
37 |
Correct |
26 ms |
3192 KB |
Output is correct |
38 |
Correct |
0 ms |
356 KB |
Output is correct |
39 |
Correct |
28 ms |
3028 KB |
Output is correct |
40 |
Correct |
27 ms |
3160 KB |
Output is correct |
41 |
Correct |
26 ms |
3164 KB |
Output is correct |
42 |
Correct |
26 ms |
3940 KB |
Output is correct |