#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
23 ms |
2452 KB |
Output is correct |
4 |
Correct |
44 ms |
4036 KB |
Output is correct |
5 |
Correct |
53 ms |
4588 KB |
Output is correct |
6 |
Correct |
41 ms |
3628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
640 KB |
Output is correct |
3 |
Correct |
18 ms |
1744 KB |
Output is correct |
4 |
Correct |
41 ms |
3136 KB |
Output is correct |
5 |
Correct |
38 ms |
3184 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
6 ms |
588 KB |
Output is correct |
4 |
Correct |
19 ms |
1848 KB |
Output is correct |
5 |
Correct |
37 ms |
3148 KB |
Output is correct |
6 |
Correct |
38 ms |
3124 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
5 ms |
580 KB |
Output is correct |
9 |
Correct |
19 ms |
1744 KB |
Output is correct |
10 |
Correct |
42 ms |
3188 KB |
Output is correct |
11 |
Correct |
36 ms |
3156 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
23 ms |
2380 KB |
Output is correct |
12 |
Correct |
43 ms |
3972 KB |
Output is correct |
13 |
Correct |
46 ms |
4548 KB |
Output is correct |
14 |
Correct |
40 ms |
3612 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
588 KB |
Output is correct |
17 |
Correct |
18 ms |
1744 KB |
Output is correct |
18 |
Correct |
37 ms |
3136 KB |
Output is correct |
19 |
Correct |
38 ms |
3248 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
4 ms |
588 KB |
Output is correct |
22 |
Correct |
19 ms |
1824 KB |
Output is correct |
23 |
Correct |
35 ms |
3220 KB |
Output is correct |
24 |
Correct |
37 ms |
3140 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 |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
8 ms |
716 KB |
Output is correct |
31 |
Correct |
9 ms |
680 KB |
Output is correct |
32 |
Correct |
36 ms |
2492 KB |
Output is correct |
33 |
Correct |
38 ms |
2416 KB |
Output is correct |
34 |
Correct |
81 ms |
4596 KB |
Output is correct |
35 |
Correct |
77 ms |
4552 KB |
Output is correct |
36 |
Correct |
82 ms |
4624 KB |
Output is correct |
37 |
Correct |
83 ms |
4548 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
72 ms |
3396 KB |
Output is correct |
40 |
Correct |
53 ms |
3140 KB |
Output is correct |
41 |
Correct |
46 ms |
3212 KB |
Output is correct |
42 |
Correct |
35 ms |
3212 KB |
Output is correct |