#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const MOD = 1e9 + 7;
int n;
vector<ll> h, w;
void read() {
cin >> n;
h.resize(n);
w.resize(n);
for (auto &a : h) cin >> a;
for (auto &a : w) cin >> a;
}
ll modsub(ll a, ll b) {
ll ret = (a - b) % MOD;
if (ret < 0) ret += MOD;
return ret;
}
ll pot(ll a, int k) {
ll ret = 1;
while (k) {
if (k % 2) {
ret *= a;
ret %= MOD;
}
a *= a;
a %= MOD;
k /= 2;
}
return ret;
}
void solve() {
read();
h.push_back(0);
w.push_back(0);
ll ans = 0;
ll inv2 = pot(2, MOD - 2);
vector<pair<ll, ll>> stck;
h.push_back(0);
w.push_back(0);
stck.emplace_back(0, 0);
for (int i = 0; i <= n; ++i) {
ll sumW = 0;
while (stck.back().first > h[i]) {
ll prevH = max(h[i], stck[stck.size() - 2].first);
ll dH = stck.back().first - prevH;
sumW = (sumW + stck.back().second) % MOD;
stck.pop_back();
ans += (dH * (dH + 1) % MOD * inv2 % MOD + dH * prevH % MOD)
* (sumW * (sumW + 1) % MOD * inv2 % MOD) % MOD;
ans %= MOD;
}
if (stck.back().first == h[i]) {
stck.back().second += (sumW + w[i]) % MOD;
stck.back().second %= MOD;
} else {
stck.emplace_back(h[i], (sumW + w[i]) % MOD);
}
}
cout << ans << '\n';
}
int main() {
//freopen("input1.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
11 ms |
2156 KB |
Output is correct |
4 |
Correct |
21 ms |
3820 KB |
Output is correct |
5 |
Correct |
26 ms |
4080 KB |
Output is correct |
6 |
Correct |
23 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
876 KB |
Output is correct |
3 |
Correct |
13 ms |
2540 KB |
Output is correct |
4 |
Correct |
25 ms |
4700 KB |
Output is correct |
5 |
Correct |
28 ms |
4716 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
876 KB |
Output is correct |
4 |
Correct |
16 ms |
2560 KB |
Output is correct |
5 |
Correct |
25 ms |
4700 KB |
Output is correct |
6 |
Correct |
29 ms |
4716 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
3 ms |
1104 KB |
Output is correct |
9 |
Correct |
15 ms |
2908 KB |
Output is correct |
10 |
Correct |
28 ms |
6860 KB |
Output is correct |
11 |
Correct |
29 ms |
6988 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
14 ms |
2156 KB |
Output is correct |
12 |
Correct |
21 ms |
3820 KB |
Output is correct |
13 |
Correct |
21 ms |
3840 KB |
Output is correct |
14 |
Correct |
24 ms |
3868 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
876 KB |
Output is correct |
17 |
Correct |
13 ms |
2540 KB |
Output is correct |
18 |
Correct |
27 ms |
4652 KB |
Output is correct |
19 |
Correct |
29 ms |
4768 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
4 ms |
1104 KB |
Output is correct |
22 |
Correct |
15 ms |
2908 KB |
Output is correct |
23 |
Correct |
28 ms |
6860 KB |
Output is correct |
24 |
Correct |
28 ms |
6988 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
2 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
3 ms |
876 KB |
Output is correct |
31 |
Correct |
3 ms |
876 KB |
Output is correct |
32 |
Correct |
13 ms |
2412 KB |
Output is correct |
33 |
Correct |
16 ms |
2540 KB |
Output is correct |
34 |
Correct |
27 ms |
4460 KB |
Output is correct |
35 |
Correct |
27 ms |
4460 KB |
Output is correct |
36 |
Correct |
30 ms |
4588 KB |
Output is correct |
37 |
Correct |
28 ms |
4700 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
30 ms |
4700 KB |
Output is correct |
40 |
Correct |
27 ms |
4700 KB |
Output is correct |
41 |
Correct |
27 ms |
4700 KB |
Output is correct |
42 |
Correct |
29 ms |
5644 KB |
Output is correct |