#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 1e6 + 10;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;
const ll INV2 = 500000004;
ll L[MAXN], H[MAXN], W[MAXN];
set<pll> st;
inline void mkey(ll& a) {
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
}
inline ll C2(ll a) {
a %= MOD;
return a * (a - 1) % MOD * INV2 % MOD;;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> v;
for (int i = 1; i <= n; i++) {
cin >> H[i];
v.push_back(i);
}
L[0] = 1;
ll tot = 0;
for (int i = 1; i <= n; i++) {
cin >> W[i];
L[i] = L[i - 1] + W[i - 1];
tot += W[i];
}
ll T = C2(tot + 1);
st.insert({1, tot});
sort(all(v), [] (int i, int j) {
return H[i] < H[j];
});
ll lst = 0, ans = 0;
for (int e : v) {
mkey(ans += T * (C2(H[e] + 1) - C2(lst + 1) + MOD) % MOD);
ll l = L[e], r = l + W[e] - 1;
auto it = st.upper_bound(make_pair(l, INF));
it--;
ll tl = it -> X, tr = it -> Y;
mkey(T -= C2(tr - tl + 2));
ll m_r = l - 1, m_l = r + 1;
st.erase(it);
if (m_r >= tl) {
mkey(T += C2(m_r - tl + 2));
st.insert({tl, m_r});
}
if (m_l <= tr) {
mkey(T += C2(tr - m_l + 2));
st.insert({m_l, tr});
}
lst = H[e];
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
29 ms |
3076 KB |
Output is correct |
4 |
Correct |
44 ms |
5188 KB |
Output is correct |
5 |
Correct |
48 ms |
5680 KB |
Output is correct |
6 |
Correct |
40 ms |
4760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
592 KB |
Output is correct |
3 |
Correct |
18 ms |
1792 KB |
Output is correct |
4 |
Correct |
36 ms |
3120 KB |
Output is correct |
5 |
Correct |
38 ms |
3140 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
4 ms |
592 KB |
Output is correct |
4 |
Correct |
18 ms |
1744 KB |
Output is correct |
5 |
Correct |
36 ms |
3212 KB |
Output is correct |
6 |
Correct |
38 ms |
3136 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
588 KB |
Output is correct |
9 |
Correct |
19 ms |
1772 KB |
Output is correct |
10 |
Correct |
35 ms |
3184 KB |
Output is correct |
11 |
Correct |
36 ms |
5096 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
24 ms |
3024 KB |
Output is correct |
12 |
Correct |
53 ms |
5188 KB |
Output is correct |
13 |
Correct |
47 ms |
5692 KB |
Output is correct |
14 |
Correct |
40 ms |
4812 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
18 ms |
2768 KB |
Output is correct |
18 |
Correct |
38 ms |
5168 KB |
Output is correct |
19 |
Correct |
43 ms |
5192 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
4 ms |
844 KB |
Output is correct |
22 |
Correct |
19 ms |
2768 KB |
Output is correct |
23 |
Correct |
36 ms |
5024 KB |
Output is correct |
24 |
Correct |
36 ms |
5060 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
7 ms |
972 KB |
Output is correct |
31 |
Correct |
7 ms |
888 KB |
Output is correct |
32 |
Correct |
36 ms |
3340 KB |
Output is correct |
33 |
Correct |
50 ms |
3372 KB |
Output is correct |
34 |
Correct |
80 ms |
6396 KB |
Output is correct |
35 |
Correct |
73 ms |
6388 KB |
Output is correct |
36 |
Correct |
92 ms |
6472 KB |
Output is correct |
37 |
Correct |
84 ms |
6584 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
72 ms |
5256 KB |
Output is correct |
40 |
Correct |
53 ms |
5060 KB |
Output is correct |
41 |
Correct |
45 ms |
5140 KB |
Output is correct |
42 |
Correct |
36 ms |
5152 KB |
Output is correct |