#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
using namespace std;
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;
ll Add(ll a, ll b) {
a += b;
if (a >= mod) a -= mod;
return a;
}
ll Sub(ll a, ll b) {
a -= b;
if (a < 0) a += mod;
return a;
}
ll Md(ll x) {
while (x < 0) x += mod;
while (x >= mod) x -= mod;
return x;
}
ll Mul(ll a, ll b) {
return (a * b) % mod;
}
ll Exp(ll x, ll k) {
ll ans = 1;
for (; k; k >>= 1) {
x = Mul(x, x);
if (k & 1) ans = Mul(ans, x);
}
return ans;
}
ll Div(ll a, ll b) {
return Mul(a, Exp(b, mod - 2));
}
ll G(ll x) {
ll y = x * (x + 1) / 2;
y %= mod;
return y;
}
ll F(ll H, ll W) {
H = G(H);
W = G(W);
H %= mod;
W %= mod;
return Mul(H, W);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<ll> h(n), w(n), p(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
for (int i = 0; i < n; i++) {
cin >> w[i];
p[i] = w[i];
if (i > 0) p[i] = Add(p[i], p[i - 1]);
}
ll ans = 0;
stack<int> s;
for (int i = 0; i < n; i++) {
while (!s.empty() && h[s.top()] > h[i]) {
int j = s.top();
s.pop();
int k = -1;
if (!s.empty()) k = s.top();
ll mx = h[i];
if (k != -1) mx = max(mx, h[k]);
ll H = h[j] - mx;
ll W = p[i - 1];
if (k != -1) W = Sub(W, p[k]);
ans = Add(ans, F(H, W));
ans = Add(ans, Mul(G(W), Mul(H, mx)));
}
s.push(i);
}
while (!s.empty()) {
int j = s.top();
s.pop();
int k = -1;
if (!s.empty()) k = s.top();
ll mx = 0;
if (k != -1) mx = max(mx, h[k]);
ll H = h[j] - mx;
ll W = p[n - 1];
if (k != -1) W = Sub(W, p[k]);
ans = Add(ans, F(H, W));
ans = Add(ans, Mul(G(W), Mul(H, mx)));
}
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
12 ms |
2136 KB |
Output is correct |
4 |
Correct |
19 ms |
4076 KB |
Output is correct |
5 |
Correct |
19 ms |
3956 KB |
Output is correct |
6 |
Correct |
18 ms |
3804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
748 KB |
Output is correct |
3 |
Correct |
12 ms |
2636 KB |
Output is correct |
4 |
Correct |
23 ms |
4956 KB |
Output is correct |
5 |
Correct |
27 ms |
5068 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
712 KB |
Output is correct |
4 |
Correct |
13 ms |
2592 KB |
Output is correct |
5 |
Correct |
25 ms |
4968 KB |
Output is correct |
6 |
Correct |
34 ms |
5100 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
14 ms |
2644 KB |
Output is correct |
10 |
Correct |
26 ms |
4956 KB |
Output is correct |
11 |
Correct |
23 ms |
4984 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
9 ms |
2132 KB |
Output is correct |
12 |
Correct |
22 ms |
4072 KB |
Output is correct |
13 |
Correct |
18 ms |
3932 KB |
Output is correct |
14 |
Correct |
19 ms |
3828 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
13 ms |
2640 KB |
Output is correct |
18 |
Correct |
24 ms |
4972 KB |
Output is correct |
19 |
Correct |
25 ms |
5088 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
748 KB |
Output is correct |
22 |
Correct |
12 ms |
2640 KB |
Output is correct |
23 |
Correct |
22 ms |
4808 KB |
Output is correct |
24 |
Correct |
31 ms |
4948 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
3 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
724 KB |
Output is correct |
32 |
Correct |
12 ms |
2260 KB |
Output is correct |
33 |
Correct |
13 ms |
2388 KB |
Output is correct |
34 |
Correct |
22 ms |
4300 KB |
Output is correct |
35 |
Correct |
22 ms |
4328 KB |
Output is correct |
36 |
Correct |
23 ms |
4592 KB |
Output is correct |
37 |
Correct |
29 ms |
4584 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
24 ms |
4592 KB |
Output is correct |
40 |
Correct |
25 ms |
4820 KB |
Output is correct |
41 |
Correct |
23 ms |
4584 KB |
Output is correct |
42 |
Correct |
24 ms |
4708 KB |
Output is correct |