#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAX 101010
#define MOD 1000000007
#define ln '\n'
ll H[MAX], W[MAX];
ll sw[MAX];
ll ans = 0;
ll N;
struct segtree {
ll N, s;
vector<pll> tree;
vector<ll> l, r;
void init(ll x = 1) {
if (x >= s) {
l[x] = r[x] = x - s + 1;
if (x - s + 1 > N) tree[x] = { INT32_MAX, l[x] };
tree[x].second = l[x];
return;
}
init(x * 2);
init(x * 2 + 1);
l[x] = l[x * 2];
r[x] = r[x * 2 + 1];
tree[x] = min(tree[x * 2], tree[x * 2 + 1]);
}
segtree(ll n) {
N = n;
s = 1LL << (ll)ceil(log2(N));
tree.resize(2 * s + 1);
l.resize(2 * s + 1);
r.resize(2 * s + 1);
}
segtree() {}
pll query(ll low, ll high, ll loc = 1) {
if (l[loc] == low && r[loc] == high) return tree[loc];
if (r[loc * 2] >= high) return query(low, high, loc * 2);
if (l[loc * 2 + 1] <= low) return query(low, high, loc * 2 + 1);
return min(query(low, r[loc * 2], loc * 2), query(l[loc * 2 + 1], high, loc * 2 + 1));
}
};
segtree seg;
void get(ll l, ll r) {
if (l > r) return;
ll res = seg.query(l, r).second;
ll a = sw[r] - sw[l - 1];
a %= MOD;
ll b = H[res];
b %= MOD;
ll ls, rs;
ls = sw[res] - sw[l - 1] - W[res];
ls %= MOD;
rs = sw[r] - sw[res];
rs %= MOD;
ll mem = a * (a + 1);
mem %= MOD;
mem -= ls * (ls + 1);
mem %= MOD;
mem -= rs * (rs + 1);
mem %= MOD;
mem *= b;
mem %= MOD;
mem *= (b + 1);
mem %= MOD;
while (mem % 4) mem += MOD;
mem /= 4;
ans += mem;
ans %= MOD;
get(l, res - 1);
get(res + 1, r);
}
signed main() {
ll N;
cin >> N;
ll i;
seg = segtree(N);
for (i = 1; i <= N; i++) cin >> H[i];
for (i = 1; i <= N; i++) cin >> W[i], sw[i] = sw[i - 1] + W[i];
for (i = 1; i <= N; i++) seg.tree[i + seg.s - 1] = { H[i], i };
seg.init();
get(1, N);
ans %= MOD;
if (ans < 0) ans += MOD;
cout << ans << ln;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
48 ms |
5528 KB |
Output is correct |
4 |
Correct |
107 ms |
10804 KB |
Output is correct |
5 |
Correct |
102 ms |
10752 KB |
Output is correct |
6 |
Correct |
94 ms |
10868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
14 ms |
1520 KB |
Output is correct |
3 |
Correct |
69 ms |
5576 KB |
Output is correct |
4 |
Correct |
134 ms |
10864 KB |
Output is correct |
5 |
Correct |
136 ms |
10864 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
13 ms |
1484 KB |
Output is correct |
4 |
Correct |
69 ms |
5696 KB |
Output is correct |
5 |
Correct |
144 ms |
10864 KB |
Output is correct |
6 |
Correct |
143 ms |
10860 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
13 ms |
1560 KB |
Output is correct |
9 |
Correct |
68 ms |
5572 KB |
Output is correct |
10 |
Correct |
130 ms |
10948 KB |
Output is correct |
11 |
Correct |
133 ms |
10788 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
54 ms |
5504 KB |
Output is correct |
12 |
Correct |
115 ms |
10904 KB |
Output is correct |
13 |
Correct |
101 ms |
10808 KB |
Output is correct |
14 |
Correct |
95 ms |
10828 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
14 ms |
1544 KB |
Output is correct |
17 |
Correct |
66 ms |
5504 KB |
Output is correct |
18 |
Correct |
135 ms |
10864 KB |
Output is correct |
19 |
Correct |
154 ms |
10824 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
14 ms |
1484 KB |
Output is correct |
22 |
Correct |
67 ms |
5592 KB |
Output is correct |
23 |
Correct |
129 ms |
10820 KB |
Output is correct |
24 |
Correct |
135 ms |
10856 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
12 ms |
1484 KB |
Output is correct |
31 |
Correct |
12 ms |
1528 KB |
Output is correct |
32 |
Correct |
56 ms |
5544 KB |
Output is correct |
33 |
Correct |
59 ms |
5588 KB |
Output is correct |
34 |
Correct |
110 ms |
10820 KB |
Output is correct |
35 |
Correct |
110 ms |
10948 KB |
Output is correct |
36 |
Correct |
138 ms |
10816 KB |
Output is correct |
37 |
Correct |
144 ms |
10812 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
121 ms |
10820 KB |
Output is correct |
40 |
Correct |
124 ms |
10840 KB |
Output is correct |
41 |
Correct |
132 ms |
11512 KB |
Output is correct |
42 |
Correct |
130 ms |
13916 KB |
Output is correct |